Keeps screen lit and pauses when app is backgrounded
[ghostsoverboard] / seascene.cpp
1 #include "seascene.h"
2 #include "timercontrolledtursas.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         qDebug() << "Found a place for a rock";
71
72         //If there was no room no point to continue
73         if (pPosition == NULL)
74             break;
75
76         QPixmap rockPixmap (":/pix/kari.png");
77         QGraphicsPixmapItem * pRock = addPixmap(rockPixmap);
78         pRock->setData(0,"rock");
79         pRock->setPos(*pPosition);
80         delete pPosition;
81
82     }
83
84     //spread the ghosts
85
86     ghostsLeft_ = ghosts;
87     spreadGhosts(ghosts);
88
89
90
91     //spread the octopuses
92
93
94     for (int i=0; i < octopuses; i++)
95     {
96         QPointF * pPosition = findRandomFreeSlot();
97
98         //If there was no room no point to continue
99         if (pPosition == NULL)
100             break;
101
102     QPixmap octopusPixmap (":/pix/tursas.png");
103     TimerControlledTursas * pOctopus = new TimerControlledTursas (octopusPixmap,100);
104     pOctopus->setData(0,"octopus");
105     pOctopus->setPos(*pPosition);
106     addItem(pOctopus);
107     pOctopus->startMoving();
108     movingItems_.append(pOctopus);
109     connect(this,SIGNAL(pauseOn()),pOctopus,SLOT(stopMoving()));
110     connect(this,SIGNAL(pauseOff()),pOctopus,SLOT(startMoving()));
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     delete pPosition;
151 }
152
153
154 void SeaScene::spreadGhosts(int ghosts)
155 {
156
157
158     //the octopuses and the ship may have moved from their original positions,
159     //so the list of free slots must be adjusted to exclude their current positions
160
161     QList<QPointF> temporarilyReservedSlots;
162
163     foreach (QGraphicsItem* pItem, movingItems_)
164     {
165         if (pItem == NULL)
166         {
167  //           qDebug() << "NULL item in movingItems_";
168             continue;
169         }
170
171         //round x and y down to fit the slot size
172         int x = pItem->x();
173         x = x/40;
174         x = x*40;
175
176         int y = pItem->y();
177         y = y/40;
178         y=y*40;
179
180
181         QPointF position (x,y);
182
183         //remove the tiles (potentially) occupied by the item from free slots and place in temp list if was in the list before
184
185         if (freeTiles_.removeOne(position))
186             temporarilyReservedSlots.append(position);
187
188
189         position.setX(x+40);
190
191         if (freeTiles_.removeOne(position))
192             temporarilyReservedSlots.append(position);
193
194         position.setY(y+40);
195
196         if (freeTiles_.removeOne(position))
197             temporarilyReservedSlots.append(position);
198
199         position.setX(x);
200
201         if (freeTiles_.removeOne(position))
202             temporarilyReservedSlots.append(position);
203
204     }
205
206
207     //spread ghosts in random free slots
208
209     for (int i=0; i < ghosts; i++)
210     {
211         QPointF * pPosition = findRandomFreeSlot();
212
213         //If there was no room no point to continue
214         if (pPosition == NULL)
215             return;
216
217         QPixmap ghostPixmap(":/pix/aave.png");
218         QGraphicsPixmapItem * pGhost = addPixmap(ghostPixmap);
219         pGhost->setData(0,"ghost");
220         pGhost->setPos(*pPosition);
221         delete pPosition;
222     }
223
224     //return the slots occupied by moving items to free slots
225     freeTiles_.append(temporarilyReservedSlots);
226
227     //clear temp for the next round
228     temporarilyReservedSlots.clear();
229 }
230
231 QPointF* SeaScene::findRandomFreeSlot()
232 {
233     if (freeTiles_.isEmpty())
234         return NULL;
235
236     int index = qrand()%freeTiles_.size();
237
238     qDebug()  << index << " index";
239     return new QPointF (freeTiles_.takeAt(index));
240
241 }
242
243 void SeaScene::removeGhost(QGraphicsItem *pGhost)
244 {
245     removeItem(pGhost);  //remove the item from scene
246     freeTiles_.append(pGhost->scenePos()); //add the item's position to free slots
247     delete pGhost;
248     ghostsLeft_--;
249     if (ghostsLeft_ == 0)
250     {
251         emit allGhostsPicked();
252         qDebug() << "All ghosts picked!";
253     }
254 }
255
256 void SeaScene::ghostsDropped(int ghosts)
257 {
258     ghostsLeft_ += ghosts;
259
260     spreadGhosts(ghosts);
261 }
262
263 void SeaScene::pause(bool paused)
264 {
265     //    qDebug() << "pause pressed " << paused;
266         if (paused_ == paused)
267                 return;
268
269         paused_ = paused;
270
271         if (paused == false)
272         {
273      //       qDebug() << "starting to move again";
274             emit pauseOff();
275             screenLitKeeper_.keepScreenLit(true);
276         }
277
278         else
279         {
280      //       qDebug("about to stop movement");
281             emit pauseOn();
282             screenLitKeeper_.keepScreenLit(false);
283         }
284 }