Created a web page for the app.
[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         //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
92     for (int i=0; i < octopuses; i++)
93     {
94         QPointF * pPosition = findRandomFreeSlot();
95
96         //If there was no room no point to continue
97         if (pPosition == NULL)
98             break;
99
100     QPixmap octopusPixmap (":/pix/tursas.png");
101     TimerControlledTursas * pOctopus = new TimerControlledTursas (octopusPixmap,100);
102     pOctopus->setData(0,"octopus");
103     pOctopus->setPos(*pPosition);
104     addItem(pOctopus);
105     pOctopus->startMoving();
106     movingItems_.append(pOctopus);
107     connect(this,SIGNAL(pauseOn()),pOctopus,SLOT(stopMoving()));
108     connect(this,SIGNAL(pauseOff()),pOctopus,SLOT(startMoving()));
109     delete pPosition;
110
111     }
112
113
114     //place the ship
115
116     QPointF * pPosition = findRandomFreeSlot();
117     if (pPosition == NULL)
118     {
119         // Game cannot begin without a free position for ship, so give an error message and return
120
121         QMessageBox::critical(NULL,"Error! Too many objects on screen","No free space to place the ship. The game cannot start. Please choose another level.");
122         return;
123     }
124
125     QList<QPixmap> shipImages;
126     shipImages.append(QPixmap(":/pix/laiva.png"));
127     shipImages.append(QPixmap(":/pix/laiva_1aave.png"));
128     shipImages.append(QPixmap(":/pix/laiva_2aave.png"));
129     shipImages.append(QPixmap(":/pix/laiva_3aave.png"));
130     shipImages.append(QPixmap(":/pix/laiva_4aave.png"));
131     shipImages.append(QPixmap(":/pix/laiva_5aave.png"));
132     shipImages.append(QPixmap(":/pix/laiva_6aave.png"));
133     shipImages.append(QPixmap(":/pix/laiva_7aave.png"));
134     shipImages.append(QPixmap(":/pix/laiva_8aave.png"));
135     shipImages.append(QPixmap(":/pix/laiva_9aave.png"));
136     shipImages.append(QPixmap(":/pix/laiva_10aave.png"));
137
138     Ship * pShip = new Ship (shipImages);
139     pShip->setData(0,"ship");
140     pShip->setPos(*pPosition);
141     addItem(pShip);
142     connect(pShip,SIGNAL(pickingGhost(QGraphicsItem*)),this, SLOT(removeGhost(QGraphicsItem*)) );
143     connect(pShip,SIGNAL(droppingGhosts(int)),this,SLOT(ghostsDropped(int)));
144     pShip->startMoving();
145     movingItems_.append(pShip);
146     connect(this,SIGNAL(pauseOn()),pShip,SLOT(stopMoving()));
147     connect(this,SIGNAL(pauseOff()),pShip,SLOT(startMoving()));
148     delete pPosition;
149 }
150
151
152 void SeaScene::spreadGhosts(int ghosts)
153 {
154
155
156     //the octopuses and the ship may have moved from their original positions,
157     //so the list of free slots must be adjusted to exclude their current positions
158
159     QList<QPointF> temporarilyReservedSlots;
160
161     foreach (QGraphicsItem* pItem, movingItems_)
162     {
163         if (pItem == NULL)
164         {
165  //           qDebug() << "NULL item in movingItems_";
166             continue;
167         }
168
169         //round x and y down to fit the slot size
170         int x = pItem->x();
171         x = x/40;
172         x = x*40;
173
174         int y = pItem->y();
175         y = y/40;
176         y=y*40;
177
178
179         QPointF position (x,y);
180
181         //remove the tiles (potentially) occupied by the item from free slots and place in temp list if was in the list before
182
183         if (freeTiles_.removeOne(position))
184             temporarilyReservedSlots.append(position);
185
186
187         position.setX(x+40);
188
189         if (freeTiles_.removeOne(position))
190             temporarilyReservedSlots.append(position);
191
192         position.setY(y+40);
193
194         if (freeTiles_.removeOne(position))
195             temporarilyReservedSlots.append(position);
196
197         position.setX(x);
198
199         if (freeTiles_.removeOne(position))
200             temporarilyReservedSlots.append(position);
201
202     }
203
204
205     //spread ghosts in random free slots
206
207     for (int i=0; i < ghosts; i++)
208     {
209         QPointF * pPosition = findRandomFreeSlot();
210
211         //If there was no room no point to continue
212         if (pPosition == NULL)
213             return;
214
215         QPixmap ghostPixmap(":/pix/aave.png");
216         QGraphicsPixmapItem * pGhost = addPixmap(ghostPixmap);
217         pGhost->setData(0,"ghost");
218         pGhost->setPos(*pPosition);
219         delete pPosition;
220     }
221
222     //return the slots occupied by moving items to free slots
223     freeTiles_.append(temporarilyReservedSlots);
224
225     //clear temp for the next round
226     temporarilyReservedSlots.clear();
227 }
228
229 QPointF* SeaScene::findRandomFreeSlot()
230 {
231     if (freeTiles_.isEmpty())
232         return NULL;
233
234     int index = qrand()%freeTiles_.size();
235
236 //    qDebug()  << index << " index";
237     return new QPointF (freeTiles_.takeAt(index));
238
239 }
240
241 void SeaScene::removeGhost(QGraphicsItem *pGhost)
242 {
243     removeItem(pGhost);  //remove the item from scene
244     freeTiles_.append(pGhost->scenePos()); //add the item's position to free slots
245     delete pGhost;
246     ghostsLeft_--;
247     if (ghostsLeft_ == 0)
248     {
249         emit allGhostsPicked();
250  //       qDebug() << "All ghosts picked!";
251     }
252 }
253
254 void SeaScene::ghostsDropped(int ghosts)
255 {
256     ghostsLeft_ += ghosts;
257
258     spreadGhosts(ghosts);
259 }
260
261 void SeaScene::pause(bool paused)
262 {
263     //    qDebug() << "pause pressed " << paused;
264         if (paused_ == paused)
265                 return;
266
267         paused_ = paused;
268
269         if (paused == false)
270         {
271      //       qDebug() << "starting to move again";
272             emit pauseOff();
273             screenLitKeeper_.keepScreenLit(true);
274         }
275
276         else
277         {
278      //       qDebug("about to stop movement");
279             emit pauseOn();
280             screenLitKeeper_.keepScreenLit(false);
281         }
282 }