1 | // |
2 | // KBlackBox |
3 | // |
4 | // A simple game inspired by an emacs module |
5 | // |
6 | /*************************************************************************** |
7 | * Copyright (c) 2007, Nicolas Roffet * |
8 | * nicolas-kde@roffet.com * |
9 | * * |
10 | * * |
11 | * This program is free software; you can redistribute it and/or modify * |
12 | * it under the terms of the GNU General Public License as published by * |
13 | * the Free Software Foundation; either version 2 of the License, or * |
14 | * (at your option) any later version. * |
15 | * * |
16 | * This program is distributed in the hope that it will be useful, * |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
19 | * GNU General Public License for more details. * |
20 | * * |
21 | * You should have received a copy of the GNU General Public License * |
22 | * along with this program; if not, write to the * |
23 | * Free Software Foundation, Inc., * |
24 | * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA * |
25 | ***************************************************************************/ |
26 | |
27 | #include "kbbgraphicsitemballrepository.h" |
28 | |
29 | |
30 | |
31 | #include <QGraphicsScene> |
32 | |
33 | |
34 | #include "kbbgraphicsitemonbox.h" |
35 | #include "kbbgraphicsitemset.h" |
36 | #include "kbbscalablegraphicwidget.h" |
37 | |
38 | |
39 | |
40 | // |
41 | // Constructor / Destructor |
42 | // |
43 | |
44 | KBBGraphicsItemBallRepository::KBBGraphicsItemBallRepository(KBBScalableGraphicWidget* parent, KBBThemeManager* themeManager) : KBBGraphicsItem(KBBScalableGraphicWidget::informationBackground, parent->scene(), themeManager) |
45 | { |
46 | m_parent = parent; |
47 | m_themeManager = themeManager; |
48 | m_width = 1; |
49 | m_height = 1; |
50 | m_ballToPlace = 0; |
51 | |
52 | m_random.setSeed(0); |
53 | |
54 | m_ballsOutside = new KBBGraphicsItemSet(parent->scene()); |
55 | } |
56 | |
57 | |
58 | KBBGraphicsItemBallRepository::~KBBGraphicsItemBallRepository() |
59 | { |
60 | delete m_ballsOutside; |
61 | } |
62 | |
63 | |
64 | |
65 | // |
66 | // Public |
67 | // |
68 | |
69 | int KBBGraphicsItemBallRepository::ballToTake() const |
70 | { |
71 | return m_ballsOutside->anyItemPosition(); |
72 | } |
73 | |
74 | |
75 | void KBBGraphicsItemBallRepository::fillBallsOutside(int placed) |
76 | { |
77 | int i = m_columns*m_rows; |
78 | while ((m_ballsOutside->count()+placed)<m_ballToPlace) { |
79 | if (!(m_ballsOutside->containsVisible(i))) { |
80 | KBBGraphicsItemOnBox* b = new KBBGraphicsItemOnBox(KBBScalableGraphicWidget::playerBall, m_parent, m_themeManager, i, m_columns, m_rows); |
81 | m_ballsOutside->insert(b); |
82 | b->setPos(x() + ((i - m_columns*m_rows) / m_height)*KBBScalableGraphicWidget::RATIO, y() + ((i - m_columns*m_rows) % m_height)*KBBScalableGraphicWidget::RATIO); |
83 | } |
84 | i++; |
85 | } |
86 | } |
87 | |
88 | |
89 | void KBBGraphicsItemBallRepository::newGame(int columns, int rows, int balls) |
90 | { |
91 | m_columns = columns; |
92 | m_rows = rows; |
93 | |
94 | m_ballsOutside->clear(); |
95 | |
96 | m_ballToPlace = balls; |
97 | scale(1./m_width, 1./m_height); |
98 | |
99 | |
100 | m_height = (rows/2); |
101 | if (rows % 2 > 0) |
102 | m_height++; |
103 | m_width = balls/m_height; |
104 | if (balls % m_height > 0) |
105 | m_width++; |
106 | |
107 | scale(m_width/1., m_height/1.); |
108 | |
109 | setPos((-(m_width+1)*KBBScalableGraphicWidget::RATIO), KBBScalableGraphicWidget::RATIO); |
110 | |
111 | fillBallsOutside(0); |
112 | } |
113 | |
114 | |
115 | void KBBGraphicsItemBallRepository::removeBall(int outsidePosition) |
116 | { |
117 | m_ballsOutside->remove(outsidePosition); |
118 | } |
119 | |
120 | #include "kbbgraphicsitemballrepository.moc" |
121 | |