9fed8d79ceeed0303f498ac063983083e182b32c
[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 #include <QApplication>
31 #include <QAction>
32 #include <QPushButton>
33 #include <QLabel>
34 #include <QVBoxLayout>
35
36 const QString ghostImageFilename_ = ":/pix/aave.png";
37 const QString rockImageFilename_ =":/pix/kari.png";
38 const QString octopusImageFilename_= ":/pix/tursas.png";
39
40
41 SeaScene::SeaScene(QObject *parent) :
42     QGraphicsScene(parent)
43 {
44     paused_ = false;
45     screenLitKeeper_.keepScreenLit(true);
46
47     //set background
48
49     QPixmap waves (":/pix/meri.png");
50     waves.scaled(20,20);
51     setBackgroundBrush(QBrush(waves));
52
53     //set random seed
54
55     qsrand(QTime::currentTime().msec()+2);  //+2 to avoid setting it to 1
56
57 //Setup the level list
58
59     Level level1(5,10);
60     levelList_.append(level1);
61     Level level2(5,10,2,50);
62     levelList_.append(level2);
63     Level level3(5,15,2,50);
64     levelList_.append(level3);
65     Level level4(5,15,4,50);
66     levelList_.append(level4);
67     Level level5(5,15,5,100);
68     levelList_.append(level5);
69
70     currentLevel_ = 0;
71
72
73     pVibrateAction_ = new QAction(tr("Vibration effects"),this);
74     pVibrateAction_->setCheckable(true);
75     connect(pVibrateAction_,SIGNAL(toggled(bool)),this,SLOT(vibrationActivate(bool)));
76
77
78     pPauseAction_ = new QAction(tr("Pause"),this);
79     pPauseAction_->setCheckable(true);
80     connect(pPauseAction_,SIGNAL(toggled(bool)),this,SLOT(pause(bool)));
81
82
83 }
84
85 void SeaScene::setupMap(int ghosts, int rocks, int octopuses, int octopusSpeed)
86 {
87     //empty the map
88
89     clear();
90
91     createMenuItems();
92
93     //empty the list of moving items
94
95     movingItems_.clear();
96
97     //empty the list of free slots
98     freeTiles_.clear();
99
100     //fill the list of free slots
101
102     int numberOfXTiles  = width() / 40;
103     int numberOfYTiles = height() /40;
104
105 //    qDebug() << numberOfXTiles << " slots in x direction";
106 //    qDebug() << numberOfYTiles << " slots in y rirection";
107
108     for (int i = 0; i < numberOfXTiles; i++ )
109     {
110         for (int j = 0; j < numberOfYTiles; j++)
111         {
112             freeTiles_.append(QPointF(i*40,j*40));
113         }
114     }
115
116
117     //spread the rocks
118
119     for (int i = 0; i < rocks; i++)
120     {
121         QPointF * pPosition = findRandomFreeSlot();
122
123         //If there was no room no point to continue
124         if (pPosition == NULL)
125             break;
126
127         QPixmap rockPixmap (":/pix/kari.png");
128         QGraphicsPixmapItem * pRock = addPixmap(rockPixmap);
129         pRock->setData(0,"rock");
130         pRock->setPos(*pPosition);
131         delete pPosition;
132
133     }
134
135     //spread the ghosts
136
137     ghostsLeft_ = ghosts;
138     spreadGhosts(ghosts);
139
140
141
142     //spread the octopuses
143
144     QList <Octopus*> octopusList;
145
146     for (int i=0; i < octopuses; i++)
147     {
148         QPointF * pPosition = findRandomFreeSlot();
149
150         //If there was no room no point to continue
151         if (pPosition == NULL)
152             break;
153
154     QPixmap octopusPixmap (":/pix/tursas.png");
155     Octopus * pOctopus = new Octopus(octopusPixmap,octopusSpeed);
156     pOctopus->setData(0,"octopus");
157     pOctopus->setPos(*pPosition);
158     addItem(pOctopus);
159     pOctopus->startMoving();
160     movingItems_.append(pOctopus);
161     connect(this,SIGNAL(pauseOn()),pOctopus,SLOT(stopMoving()));
162     connect(this,SIGNAL(pauseOff()),pOctopus,SLOT(startMoving()));
163     octopusList.append(pOctopus);
164     delete pPosition;
165
166     }
167
168
169     //place the ship
170
171     QPointF * pPosition = findRandomFreeSlot();
172     if (pPosition == NULL)
173     {
174         // Game cannot begin without a free position for ship, so give an error message and return
175
176         QMessageBox::critical(NULL,"Error! Too many objects on screen","No free space to place the ship. The game cannot start. Please choose another level.");
177         return;
178     }
179
180     QList<QPixmap> shipImages;
181     shipImages.append(QPixmap(":/pix/laiva.png"));
182     shipImages.append(QPixmap(":/pix/laiva_1aave.png"));
183     shipImages.append(QPixmap(":/pix/laiva_2aave.png"));
184     shipImages.append(QPixmap(":/pix/laiva_3aave.png"));
185     shipImages.append(QPixmap(":/pix/laiva_4aave.png"));
186     shipImages.append(QPixmap(":/pix/laiva_5aave.png"));
187     shipImages.append(QPixmap(":/pix/laiva_6aave.png"));
188     shipImages.append(QPixmap(":/pix/laiva_7aave.png"));
189     shipImages.append(QPixmap(":/pix/laiva_8aave.png"));
190     shipImages.append(QPixmap(":/pix/laiva_9aave.png"));
191     shipImages.append(QPixmap(":/pix/laiva_10aave.png"));
192
193     Ship * pShip = new Ship (shipImages);
194     pShip->setData(0,"ship");
195     pShip->setPos(*pPosition);
196     addItem(pShip);
197     connect(pShip,SIGNAL(pickingGhost(QGraphicsItem*)),this, SLOT(removeGhost(QGraphicsItem*)) );
198     connect(pShip,SIGNAL(droppingGhosts(int)),this,SLOT(ghostsDropped(int)));
199     connect(this,SIGNAL(vibrationActivated(bool)),pShip,SLOT(setVibrationActivate(bool)));
200     pShip->startMoving();
201     movingItems_.append(pShip);
202     connect(this,SIGNAL(pauseOn()),pShip,SLOT(stopMoving()));
203     connect(this,SIGNAL(pauseOff()),pShip,SLOT(startMoving()));
204     foreach (Octopus* pOctopus, octopusList)
205     {
206         connect(pOctopus,SIGNAL(droppingGhosts()),pShip,SLOT(dropAllGhosts()));
207     }
208     delete pPosition;
209 }
210
211 void SeaScene::setupMap(Level level)
212 {
213     setupMap(level.getNumberOfGhosts(),level.getNumberOfRocks(),level.getNumberOfOctopuses(),level.getOctopusSpeed());
214 }
215
216 void SeaScene::spreadGhosts(int ghosts)
217 {
218
219
220     //the octopuses and the ship may have moved from their original positions,
221     //so the list of free slots must be adjusted to exclude their current positions
222
223     QList<QPointF> temporarilyReservedSlots;
224
225     foreach (QGraphicsItem* pItem, movingItems_)
226     {
227         if (pItem == NULL)
228         {
229  //           qDebug() << "NULL item in movingItems_";
230             continue;
231         }
232
233         //round x and y down to fit the slot size
234         int x = pItem->x();
235         x = x/40;
236         x = x*40;
237
238         int y = pItem->y();
239         y = y/40;
240         y=y*40;
241
242
243         QPointF position (x,y);
244
245         //remove the tiles (potentially) occupied by the item from free slots and place in temp list if was in the list before
246
247         if (freeTiles_.removeOne(position))
248             temporarilyReservedSlots.append(position);
249
250
251         position.setX(x+40);
252
253         if (freeTiles_.removeOne(position))
254             temporarilyReservedSlots.append(position);
255
256         position.setY(y+40);
257
258         if (freeTiles_.removeOne(position))
259             temporarilyReservedSlots.append(position);
260
261         position.setX(x);
262
263         if (freeTiles_.removeOne(position))
264             temporarilyReservedSlots.append(position);
265
266     }
267
268
269     //spread ghosts in random free slots
270
271     for (int i=0; i < ghosts; i++)
272     {
273         QPointF * pPosition = findRandomFreeSlot();
274
275         //If there was no room no point to continue
276         if (pPosition == NULL)
277             return;
278
279         QPixmap ghostPixmap(":/pix/aave.png");
280         QGraphicsPixmapItem * pGhost = addPixmap(ghostPixmap);
281         pGhost->setData(0,"ghost");
282         pGhost->setPos(*pPosition);
283         delete pPosition;
284     }
285
286     //return the slots occupied by moving items to free slots
287     freeTiles_.append(temporarilyReservedSlots);
288
289     //clear temp for the next round
290     temporarilyReservedSlots.clear();
291 }
292
293 QPointF* SeaScene::findRandomFreeSlot()
294 {
295     if (freeTiles_.isEmpty())
296         return NULL;
297
298     int index = qrand()%freeTiles_.size();
299
300 //    qDebug()  << index << " index";
301     return new QPointF (freeTiles_.takeAt(index));
302
303 }
304
305 void SeaScene::removeGhost(QGraphicsItem *pGhost)
306 {
307     removeItem(pGhost);  //remove the item from scene
308     freeTiles_.append(pGhost->scenePos()); //add the item's position to free slots
309     delete pGhost;
310     ghostsLeft_--;
311     if (ghostsLeft_ == 0)
312     {
313         emit allGhostsPicked();
314  //       qDebug() << "All ghosts picked!";
315     }
316 }
317
318 void SeaScene::ghostsDropped(int ghosts)
319 {
320     ghostsLeft_ += ghosts;
321
322     spreadGhosts(ghosts);
323 }
324
325 void SeaScene::pause(bool paused)
326 {
327     //    qDebug() << "pause pressed " << paused;
328         if (paused_ == paused)
329                 return;
330
331         paused_ = paused;
332
333         if (paused == false)
334         {
335      //       qDebug() << "starting to move again";
336             emit pauseOff();
337             screenLitKeeper_.keepScreenLit(true);
338             if (pPausetextItem_)
339                 pPausetextItem_->hide();
340         }
341
342         else
343         {
344          qDebug("about to stop movement");
345             emit pauseOn();
346             screenLitKeeper_.keepScreenLit(false);
347             if (pPausetextItem_ != NULL)
348             {
349                 qDebug() << "about to show the pause text";
350                 pPausetextItem_->show();
351                 qDebug() << "showing pause text";
352             }
353                 else qDebug() << "No pause text available";
354         }
355 }
356
357 void SeaScene::vibrationActivate(bool on)
358 {
359     emit vibrationActivated(on);
360 }
361
362 void SeaScene::handleScreenTapped()
363 {
364
365     //If the game is going just pause it
366     if (!paused_)
367     {
368         pPauseAction_->setChecked(true);
369         return;
370     }
371
372     //If the game is paused, chacl if menu item was selected
373
374     QList<QGraphicsItem *> items = selectedItems();
375
376     //if nothing selected resume play
377
378     if (items.isEmpty())
379     {
380         pPauseAction_->setChecked(false);
381         return;
382
383     }
384
385     //If something was selected check if it was one of the menu items and act on it
386     //(Nothing else should be made selectable anyway)
387
388     //Menu functions
389
390     QGraphicsItem* pItem = items.at(0); //Selecting an item brings here, thus only selecting one item should be possible
391                                        //... so we can just take the first one
392
393
394     if (pItem == pRestartGameItem_)
395     {
396         qDebug() << "game restart requested";
397     }
398
399     else if (pItem == pRestartLevelItem_)
400     {
401         qDebug() << "Level restart requested";
402
403     }
404
405     else if (pItem == pSettingsItem_)
406     {
407
408     }
409
410     else if (pItem == pAboutItem_)
411     {
412         about();
413     }
414
415
416     //Selection is just used to get notice of a menu item being clicked, removed after use
417
418     clearSelection();
419
420 }
421
422
423
424 void SeaScene::createMenuItems()
425 {
426
427     pPausetextItem_ =  addSimpleText("Game paused. Tap to continue.");
428     pPausetextItem_->setZValue(1000);
429     pPausetextItem_->setPos(250,50);
430     pPausetextItem_->hide();
431
432     menuItemCount_ = 0;
433
434     pRestartGameItem_ = new QGraphicsSimpleTextItem("Restart game");
435     prepareForMenu(pRestartGameItem_);
436
437     pRestartLevelItem_ = new QGraphicsSimpleTextItem("Restart level");
438     prepareForMenu(pRestartLevelItem_);
439
440     pSettingsItem_ = new QGraphicsSimpleTextItem("Settings");
441     prepareForMenu(pSettingsItem_);
442
443     pAboutItem_ = new QGraphicsSimpleTextItem("About");
444     prepareForMenu(pAboutItem_);
445
446 }
447
448 void SeaScene::prepareForMenu(QGraphicsItem * pItem)
449 {
450
451     //Menu items have pause text item as their parent and are thus added to scene automatically
452     //They are also shown and hidden with it, resulting in the menu being visble when the game is paused
453     //Their coordinates are given relative to the parent.
454
455     pItem->setParentItem(pPausetextItem_);
456     pItem->setZValue(1000);
457     pItem->setFlag(QGraphicsItem::ItemIsSelectable);
458     pItem->setY(150);
459     pItem->setX(menuItemCount_++*150-250);
460  }
461
462
463 void SeaScene::about()
464 {
465     QMessageBox::about(NULL, tr("About %1").arg(QApplication::applicationName()),
466                        tr("Version %1"
467                           "<p>Copyright 2011 Heli Hyv&auml;ttinen"
468                           "<p>License: General Public License v2"
469                           "<p>Bug Reports: https://bugs.maemo.org/ "
470                           "enter_bug.cgi?product=Ghosts%20Overboard"
471                           ).arg(QApplication::applicationVersion()));
472
473
474
475 }
476
477
478 void SeaScene::restartLevel()
479 {
480     setupMap(levelList_.value(currentLevel_));  //value() returns default constructor Level if index is invalid, so no risk of crash
481     vibrationActivate(pVibrateAction_->isChecked());  //Vibration effects are lost without this
482    // qDebug() << pVibrateAction_->isChecked();
483 }
484
485
486
487 void SeaScene::nextLevel()
488 {
489
490     currentLevel_++;
491
492     if (levelList_.empty())
493         setupMap(Level());
494
495
496     if ( currentLevel_ < levelList_.size() )
497     {
498        restartLevel();
499     }
500
501     else //Victory!
502     {
503
504        QDialog* pVictoryDialog = new QDialog();
505        pVictoryDialog->setWindowTitle(tr("You won!"));
506
507
508        QPushButton* pPlayAgainButton = new QPushButton(tr("Play again"));
509 //       QPushButton* pQuitButton = new QPushButton(tr("Quit game"));
510
511        QPixmap victoryIcon (":/pix/aavesaari.png");
512        QLabel* pVictoryLabel = new QLabel();
513        pVictoryLabel->setPixmap(victoryIcon);
514
515        QLabel* pTextLabel = new QLabel(tr("Congratulations! <p>You have saved all the ghosts."));
516
517
518        QVBoxLayout* pMainLayout = new QVBoxLayout;
519
520        QHBoxLayout* pTopLayout = new QHBoxLayout;
521        pMainLayout->addLayout(pTopLayout);
522
523        pTopLayout->addWidget(pVictoryLabel);
524        pTopLayout->addWidget(pTextLabel);
525
526
527
528        QHBoxLayout* pButtonLayout = new QHBoxLayout();
529        pMainLayout->addLayout(pButtonLayout);
530
531  //      pButtonLayout->addWidget(pQuitButton);
532        pButtonLayout->addWidget(pPlayAgainButton);
533
534
535
536        pVictoryDialog->setLayout(pMainLayout);
537
538        connect(pPlayAgainButton, SIGNAL(clicked()),pVictoryDialog,SLOT(accept()));
539
540        pVictoryDialog->exec();
541
542         //Never mind if the user cancels the dialog: restart the game anyway
543
544        restartGame();
545     }
546 }
547
548
549 void SeaScene::restartGame()
550 {
551     currentLevel_ = 0;
552     restartLevel();
553 }
554
555
556 void SeaScene::forcePause()
557 {
558     //Pause without setting the pause action state
559     pause(true);
560 }
561
562 void::SeaScene::softContinue()
563 {
564     //Continue if not being paused by the user
565     // Reverts forcePause()
566
567     pause(pPauseAction_->isChecked());
568 }