Class restructure for octopus
[ghostsoverboard] / seascene.cpp
1 #include "seascene.h"
2 #include "octopus.h"
3 #include "ship.h"
4 #include <QGraphicsPixmapItem>
5 #include <QDebug>
6 #include <QMessageBox>
7 #include <QTime>
8
9 const QString ghostImageFilename_ = ":/pix/aave.png";
10 const QString rockImageFilename_ =":/pix/kari.png";
11 const QString octopusImageFilename_= ":/pix/tursas.png";
12
13
14 SeaScene::SeaScene(QObject *parent) :
15     QGraphicsScene(parent)
16 {
17     paused_ = false;
18     screenLitKeeper_.keepScreenLit(true);
19
20     //set background
21
22     QPixmap waves (":/pix/meri.png");
23     waves.scaled(20,20);
24     setBackgroundBrush(QBrush(waves));
25
26     //set random seed
27
28     qsrand(QTime::currentTime().msec()+2);  //+2 to avoid setting it to 1
29
30
31
32 }
33
34 void SeaScene::setupMap(int ghosts, int rocks, int octopuses)
35 {
36     //empty the map
37
38     clear();
39
40     //empty the list of moving items
41
42     movingItems_.clear();
43
44     //empty the list of free slots
45     freeTiles_.clear();
46
47     //fill the list of free slots
48
49     int numberOfXTiles  = width() / 40;
50     int numberOfYTiles = height() /40;
51
52 //    qDebug() << numberOfXTiles << " slots in x direction";
53 //    qDebug() << numberOfYTiles << " slots in y rirection";
54
55     for (int i = 0; i < numberOfXTiles; i++ )
56     {
57         for (int j = 0; j < numberOfYTiles; j++)
58         {
59             freeTiles_.append(QPointF(i*40,j*40));
60         }
61     }
62
63
64     //spread the rocks
65
66     for (int i = 0; i < rocks; i++)
67     {
68         QPointF * pPosition = findRandomFreeSlot();
69
70         //If there was no room no point to continue
71         if (pPosition == NULL)
72             break;
73
74         QPixmap rockPixmap (":/pix/kari.png");
75         QGraphicsPixmapItem * pRock = addPixmap(rockPixmap);
76         pRock->setData(0,"rock");
77         pRock->setPos(*pPosition);
78         delete pPosition;
79
80     }
81
82     //spread the ghosts
83
84     ghostsLeft_ = ghosts;
85     spreadGhosts(ghosts);
86
87
88
89     //spread the octopuses
90
91     QList<Octopus*> octopuses;
92
93     for (int i=0; i < octopuses; i++)
94     {
95         QPointF * pPosition = findRandomFreeSlot();
96
97         //If there was no room no point to continue
98         if (pPosition == NULL)
99             break;
100
101     QPixmap octopusPixmap (":/pix/tursas.png");
102     Octopus * pOctopus = new Octopus(octopusPixmap,100);
103     pOctopus->setData(0,"octopus");
104     pOctopus->setPos(*pPosition);
105     addItem(pOctopus);
106     pOctopus->startMoving();
107     movingItems_.append(pOctopus);
108     connect(this,SIGNAL(pauseOn()),pOctopus,SLOT(stopMoving()));
109     connect(this,SIGNAL(pauseOff()),pOctopus,SLOT(startMoving()));
110     octopuses.append(pOctopus);
111     delete pPosition;
112
113     }
114
115
116     //place the ship
117
118     QPointF * pPosition = findRandomFreeSlot();
119     if (pPosition == NULL)
120     {
121         // Game cannot begin without a free position for ship, so give an error message and return
122
123         QMessageBox::critical(NULL,"Error! Too many objects on screen","No free space to place the ship. The game cannot start. Please choose another level.");
124         return;
125     }
126
127     QList<QPixmap> shipImages;
128     shipImages.append(QPixmap(":/pix/laiva.png"));
129     shipImages.append(QPixmap(":/pix/laiva_1aave.png"));
130     shipImages.append(QPixmap(":/pix/laiva_2aave.png"));
131     shipImages.append(QPixmap(":/pix/laiva_3aave.png"));
132     shipImages.append(QPixmap(":/pix/laiva_4aave.png"));
133     shipImages.append(QPixmap(":/pix/laiva_5aave.png"));
134     shipImages.append(QPixmap(":/pix/laiva_6aave.png"));
135     shipImages.append(QPixmap(":/pix/laiva_7aave.png"));
136     shipImages.append(QPixmap(":/pix/laiva_8aave.png"));
137     shipImages.append(QPixmap(":/pix/laiva_9aave.png"));
138     shipImages.append(QPixmap(":/pix/laiva_10aave.png"));
139
140     Ship * pShip = new Ship (shipImages);
141     pShip->setData(0,"ship");
142     pShip->setPos(*pPosition);
143     addItem(pShip);
144     connect(pShip,SIGNAL(pickingGhost(QGraphicsItem*)),this, SLOT(removeGhost(QGraphicsItem*)) );
145     connect(pShip,SIGNAL(droppingGhosts(int)),this,SLOT(ghostsDropped(int)));
146     pShip->startMoving();
147     movingItems_.append(pShip);
148     connect(this,SIGNAL(pauseOn()),pShip,SLOT(stopMoving()));
149     connect(this,SIGNAL(pauseOff()),pShip,SLOT(startMoving()));
150     foreach (Octopus* pOctopus, octopuses)
151     {
152         connect(pOctopus,SIGNAL(droppingGhosts()),pShip,SLOT(dropAllGhosts()));
153     }
154     delete pPosition;
155 }
156
157
158 void SeaScene::spreadGhosts(int ghosts)
159 {
160
161
162     //the octopuses and the ship may have moved from their original positions,
163     //so the list of free slots must be adjusted to exclude their current positions
164
165     QList<QPointF> temporarilyReservedSlots;
166
167     foreach (QGraphicsItem* pItem, movingItems_)
168     {
169         if (pItem == NULL)
170         {
171  //           qDebug() << "NULL item in movingItems_";
172             continue;
173         }
174
175         //round x and y down to fit the slot size
176         int x = pItem->x();
177         x = x/40;
178         x = x*40;
179
180         int y = pItem->y();
181         y = y/40;
182         y=y*40;
183
184
185         QPointF position (x,y);
186
187         //remove the tiles (potentially) occupied by the item from free slots and place in temp list if was in the list before
188
189         if (freeTiles_.removeOne(position))
190             temporarilyReservedSlots.append(position);
191
192
193         position.setX(x+40);
194
195         if (freeTiles_.removeOne(position))
196             temporarilyReservedSlots.append(position);
197
198         position.setY(y+40);
199
200         if (freeTiles_.removeOne(position))
201             temporarilyReservedSlots.append(position);
202
203         position.setX(x);
204
205         if (freeTiles_.removeOne(position))
206             temporarilyReservedSlots.append(position);
207
208     }
209
210
211     //spread ghosts in random free slots
212
213     for (int i=0; i < ghosts; i++)
214     {
215         QPointF * pPosition = findRandomFreeSlot();
216
217         //If there was no room no point to continue
218         if (pPosition == NULL)
219             return;
220
221         QPixmap ghostPixmap(":/pix/aave.png");
222         QGraphicsPixmapItem * pGhost = addPixmap(ghostPixmap);
223         pGhost->setData(0,"ghost");
224         pGhost->setPos(*pPosition);
225         delete pPosition;
226     }
227
228     //return the slots occupied by moving items to free slots
229     freeTiles_.append(temporarilyReservedSlots);
230
231     //clear temp for the next round
232     temporarilyReservedSlots.clear();
233 }
234
235 QPointF* SeaScene::findRandomFreeSlot()
236 {
237     if (freeTiles_.isEmpty())
238         return NULL;
239
240     int index = qrand()%freeTiles_.size();
241
242 //    qDebug()  << index << " index";
243     return new QPointF (freeTiles_.takeAt(index));
244
245 }
246
247 void SeaScene::removeGhost(QGraphicsItem *pGhost)
248 {
249     removeItem(pGhost);  //remove the item from scene
250     freeTiles_.append(pGhost->scenePos()); //add the item's position to free slots
251     delete pGhost;
252     ghostsLeft_--;
253     if (ghostsLeft_ == 0)
254     {
255         emit allGhostsPicked();
256  //       qDebug() << "All ghosts picked!";
257     }
258 }
259
260 void SeaScene::ghostsDropped(int ghosts)
261 {
262     ghostsLeft_ += ghosts;
263
264     spreadGhosts(ghosts);
265 }
266
267 void SeaScene::pause(bool paused)
268 {
269     //    qDebug() << "pause pressed " << paused;
270         if (paused_ == paused)
271                 return;
272
273         paused_ = paused;
274
275         if (paused == false)
276         {
277      //       qDebug() << "starting to move again";
278             emit pauseOff();
279             screenLitKeeper_.keepScreenLit(true);
280         }
281
282         else
283         {
284      //       qDebug("about to stop movement");
285             emit pauseOn();
286             screenLitKeeper_.keepScreenLit(false);
287         }
288 }