All menu items in place. Still do nothing.
[ghostsoverboard] / seascene.cpp
1 /**************************************************************************
2         Ghosts Overboard - a game for Maemo 5
3
4         Copyright (C) 2011  Heli Hyvättinen
5
6         This file is part of Ghosts Overboard
7
8         Ghosts Overboard is free software: you can redistribute it and/or modify
9         it under the terms of the GNU General Public License as published by
10         the Free Software Foundation, either version 2 of the License, or
11         (at your option) any later version.
12
13         This program is distributed in the hope that it will be useful,
14         but WITHOUT ANY WARRANTY; without even the implied warranty of
15         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16         GNU General Public License for more details.
17
18         You should have received a copy of the GNU General Public License
19         along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21 **************************************************************************/
22
23 #include "seascene.h"
24 #include "octopus.h"
25 #include "ship.h"
26 #include <QGraphicsPixmapItem>
27 #include <QDebug>
28 #include <QMessageBox>
29 #include <QTime>
30
31 const QString ghostImageFilename_ = ":/pix/aave.png";
32 const QString rockImageFilename_ =":/pix/kari.png";
33 const QString octopusImageFilename_= ":/pix/tursas.png";
34
35
36 SeaScene::SeaScene(QObject *parent) :
37     QGraphicsScene(parent)
38 {
39     paused_ = false;
40     screenLitKeeper_.keepScreenLit(true);
41
42     //set background
43
44     QPixmap waves (":/pix/meri.png");
45     waves.scaled(20,20);
46     setBackgroundBrush(QBrush(waves));
47
48     //set random seed
49
50     qsrand(QTime::currentTime().msec()+2);  //+2 to avoid setting it to 1
51
52     //connect selecting to menu handling (only menu items selectable)
53
54     connect(this,SIGNAL(selectionChanged()),this,SLOT(menuClicked()));
55
56
57
58
59
60
61
62
63
64 }
65
66 void SeaScene::setupMap(int ghosts, int rocks, int octopuses, int octopusSpeed)
67 {
68     //empty the map
69
70     clear();
71
72     createMenuItems();
73
74     //empty the list of moving items
75
76     movingItems_.clear();
77
78     //empty the list of free slots
79     freeTiles_.clear();
80
81     //fill the list of free slots
82
83     int numberOfXTiles  = width() / 40;
84     int numberOfYTiles = height() /40;
85
86 //    qDebug() << numberOfXTiles << " slots in x direction";
87 //    qDebug() << numberOfYTiles << " slots in y rirection";
88
89     for (int i = 0; i < numberOfXTiles; i++ )
90     {
91         for (int j = 0; j < numberOfYTiles; j++)
92         {
93             freeTiles_.append(QPointF(i*40,j*40));
94         }
95     }
96
97
98     //spread the rocks
99
100     for (int i = 0; i < rocks; i++)
101     {
102         QPointF * pPosition = findRandomFreeSlot();
103
104         //If there was no room no point to continue
105         if (pPosition == NULL)
106             break;
107
108         QPixmap rockPixmap (":/pix/kari.png");
109         QGraphicsPixmapItem * pRock = addPixmap(rockPixmap);
110         pRock->setData(0,"rock");
111         pRock->setPos(*pPosition);
112         delete pPosition;
113
114     }
115
116     //spread the ghosts
117
118     ghostsLeft_ = ghosts;
119     spreadGhosts(ghosts);
120
121
122
123     //spread the octopuses
124
125     QList <Octopus*> octopusList;
126
127     for (int i=0; i < octopuses; i++)
128     {
129         QPointF * pPosition = findRandomFreeSlot();
130
131         //If there was no room no point to continue
132         if (pPosition == NULL)
133             break;
134
135     QPixmap octopusPixmap (":/pix/tursas.png");
136     Octopus * pOctopus = new Octopus(octopusPixmap,octopusSpeed);
137     pOctopus->setData(0,"octopus");
138     pOctopus->setPos(*pPosition);
139     addItem(pOctopus);
140     pOctopus->startMoving();
141     movingItems_.append(pOctopus);
142     connect(this,SIGNAL(pauseOn()),pOctopus,SLOT(stopMoving()));
143     connect(this,SIGNAL(pauseOff()),pOctopus,SLOT(startMoving()));
144     octopusList.append(pOctopus);
145     delete pPosition;
146
147     }
148
149
150     //place the ship
151
152     QPointF * pPosition = findRandomFreeSlot();
153     if (pPosition == NULL)
154     {
155         // Game cannot begin without a free position for ship, so give an error message and return
156
157         QMessageBox::critical(NULL,"Error! Too many objects on screen","No free space to place the ship. The game cannot start. Please choose another level.");
158         return;
159     }
160
161     QList<QPixmap> shipImages;
162     shipImages.append(QPixmap(":/pix/laiva.png"));
163     shipImages.append(QPixmap(":/pix/laiva_1aave.png"));
164     shipImages.append(QPixmap(":/pix/laiva_2aave.png"));
165     shipImages.append(QPixmap(":/pix/laiva_3aave.png"));
166     shipImages.append(QPixmap(":/pix/laiva_4aave.png"));
167     shipImages.append(QPixmap(":/pix/laiva_5aave.png"));
168     shipImages.append(QPixmap(":/pix/laiva_6aave.png"));
169     shipImages.append(QPixmap(":/pix/laiva_7aave.png"));
170     shipImages.append(QPixmap(":/pix/laiva_8aave.png"));
171     shipImages.append(QPixmap(":/pix/laiva_9aave.png"));
172     shipImages.append(QPixmap(":/pix/laiva_10aave.png"));
173
174     Ship * pShip = new Ship (shipImages);
175     pShip->setData(0,"ship");
176     pShip->setPos(*pPosition);
177     addItem(pShip);
178     connect(pShip,SIGNAL(pickingGhost(QGraphicsItem*)),this, SLOT(removeGhost(QGraphicsItem*)) );
179     connect(pShip,SIGNAL(droppingGhosts(int)),this,SLOT(ghostsDropped(int)));
180     connect(this,SIGNAL(vibrationActivated(bool)),pShip,SLOT(setVibrationActivate(bool)));
181     pShip->startMoving();
182     movingItems_.append(pShip);
183     connect(this,SIGNAL(pauseOn()),pShip,SLOT(stopMoving()));
184     connect(this,SIGNAL(pauseOff()),pShip,SLOT(startMoving()));
185     foreach (Octopus* pOctopus, octopusList)
186     {
187         connect(pOctopus,SIGNAL(droppingGhosts()),pShip,SLOT(dropAllGhosts()));
188     }
189     delete pPosition;
190 }
191
192 void SeaScene::setupMap(Level level)
193 {
194     setupMap(level.getNumberOfGhosts(),level.getNumberOfRocks(),level.getNumberOfOctopuses(),level.getOctopusSpeed());
195 }
196
197 void SeaScene::spreadGhosts(int ghosts)
198 {
199
200
201     //the octopuses and the ship may have moved from their original positions,
202     //so the list of free slots must be adjusted to exclude their current positions
203
204     QList<QPointF> temporarilyReservedSlots;
205
206     foreach (QGraphicsItem* pItem, movingItems_)
207     {
208         if (pItem == NULL)
209         {
210  //           qDebug() << "NULL item in movingItems_";
211             continue;
212         }
213
214         //round x and y down to fit the slot size
215         int x = pItem->x();
216         x = x/40;
217         x = x*40;
218
219         int y = pItem->y();
220         y = y/40;
221         y=y*40;
222
223
224         QPointF position (x,y);
225
226         //remove the tiles (potentially) occupied by the item from free slots and place in temp list if was in the list before
227
228         if (freeTiles_.removeOne(position))
229             temporarilyReservedSlots.append(position);
230
231
232         position.setX(x+40);
233
234         if (freeTiles_.removeOne(position))
235             temporarilyReservedSlots.append(position);
236
237         position.setY(y+40);
238
239         if (freeTiles_.removeOne(position))
240             temporarilyReservedSlots.append(position);
241
242         position.setX(x);
243
244         if (freeTiles_.removeOne(position))
245             temporarilyReservedSlots.append(position);
246
247     }
248
249
250     //spread ghosts in random free slots
251
252     for (int i=0; i < ghosts; i++)
253     {
254         QPointF * pPosition = findRandomFreeSlot();
255
256         //If there was no room no point to continue
257         if (pPosition == NULL)
258             return;
259
260         QPixmap ghostPixmap(":/pix/aave.png");
261         QGraphicsPixmapItem * pGhost = addPixmap(ghostPixmap);
262         pGhost->setData(0,"ghost");
263         pGhost->setPos(*pPosition);
264         delete pPosition;
265     }
266
267     //return the slots occupied by moving items to free slots
268     freeTiles_.append(temporarilyReservedSlots);
269
270     //clear temp for the next round
271     temporarilyReservedSlots.clear();
272 }
273
274 QPointF* SeaScene::findRandomFreeSlot()
275 {
276     if (freeTiles_.isEmpty())
277         return NULL;
278
279     int index = qrand()%freeTiles_.size();
280
281 //    qDebug()  << index << " index";
282     return new QPointF (freeTiles_.takeAt(index));
283
284 }
285
286 void SeaScene::removeGhost(QGraphicsItem *pGhost)
287 {
288     removeItem(pGhost);  //remove the item from scene
289     freeTiles_.append(pGhost->scenePos()); //add the item's position to free slots
290     delete pGhost;
291     ghostsLeft_--;
292     if (ghostsLeft_ == 0)
293     {
294         emit allGhostsPicked();
295  //       qDebug() << "All ghosts picked!";
296     }
297 }
298
299 void SeaScene::ghostsDropped(int ghosts)
300 {
301     ghostsLeft_ += ghosts;
302
303     spreadGhosts(ghosts);
304 }
305
306 void SeaScene::pause(bool paused)
307 {
308     //    qDebug() << "pause pressed " << paused;
309         if (paused_ == paused)
310                 return;
311
312         paused_ = paused;
313
314         if (paused == false)
315         {
316      //       qDebug() << "starting to move again";
317             emit pauseOff();
318             screenLitKeeper_.keepScreenLit(true);
319             if (pPausetextItem_)
320                 pPausetextItem_->hide();
321         }
322
323         else
324         {
325          qDebug("about to stop movement");
326             emit pauseOn();
327             screenLitKeeper_.keepScreenLit(false);
328             if (pPausetextItem_ != NULL)
329             {
330                 qDebug() << "about to show the pause text";
331                 pPausetextItem_->show();
332                 qDebug() << "showing pause text";
333             }
334                 else qDebug() << "No pause text available";
335         }
336 }
337
338 void SeaScene::vibrationActivate(bool on)
339 {
340     emit vibrationActivated(on);
341 }
342
343 void SeaScene::menuClicked()
344 {
345     QList<QGraphicsItem *> items = selectedItems();
346
347     //if nothing selected (selection was removed) do nothing
348
349     if (items.isEmpty())
350         return;
351
352     //Tapping the screen unpaused the game, pause it again!
353
354
355
356     //Menu functions
357
358     QGraphicsItem* pItem = items.at(0);
359
360
361     if (pItem == pRestartGameItem_)
362     {
363         qDebug() << "game restart requested";
364     }
365
366     else if (pItem == pRestartLevelItem_)
367     {
368         qDebug() << "Level restart requested";
369
370     }
371
372     else if (pItem == pSettingsItem_)
373     {
374
375     }
376
377     else if (pItem == pAboutItem_)
378     {
379
380     }
381
382
383     //Selection is just used to get notice of being clicked, removed after use
384
385     clearSelection();
386
387 }
388
389
390
391 void SeaScene::createMenuItems()
392 {
393
394     pPausetextItem_ =  addSimpleText("Game paused. Tap to continue.");
395     pPausetextItem_->setZValue(1000);
396     pPausetextItem_->setPos(250,50);
397     pPausetextItem_->hide();
398
399     menuItemCount_ = 0;
400
401     pRestartGameItem_ = new QGraphicsSimpleTextItem("Restart game");
402     prepareForMenu(pRestartGameItem_);
403
404     pRestartLevelItem_ = new QGraphicsSimpleTextItem("Restart level");
405     prepareForMenu(pRestartLevelItem_);
406
407     pSettingsItem_ = new QGraphicsSimpleTextItem("Settings");
408     prepareForMenu(pSettingsItem_);
409
410     pAboutItem_ = new QGraphicsSimpleTextItem("About");
411     prepareForMenu(pAboutItem_);
412
413 }
414
415 void SeaScene::prepareForMenu(QGraphicsItem * pItem)
416 {
417
418     //Menu items have pause text item as their parent and are thus added to scene automatically
419     //They are also shown and hidden with it, resulting in the menu being visble when the game is paused
420     //Their coordinates are given relative to the parent.
421
422     pItem->setParentItem(pPausetextItem_);
423     pItem->setZValue(1000);
424     pItem->setFlag(QGraphicsItem::ItemIsSelectable);
425     pItem->setY(150);
426     pItem->setX(menuItemCount_++*150-250);
427  }
428