db31d78e4058a0df2fa06bfb46f53740fa99264f
[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         restartGame();
398     }
399
400     else if (pItem == pRestartLevelItem_)
401     {
402         qDebug() << "Level restart requested";
403         restartLevel();
404
405     }
406
407     else if (pItem == pSettingsItem_)
408     {
409     //Temporary code for settings, likely to be turned into a QML dialog
410
411           QMessageBox::StandardButton buttonpressed = QMessageBox::question(NULL,"Settings","Do you wish to have vibration effects enabled?", QMessageBox::Yes | QMessageBox::No);
412
413           if (buttonpressed == QMessageBox::Yes)
414               pVibrateAction_->setChecked(true);
415           if (buttonpressed == QMessageBox::No)
416               pVibrateAction_->setChecked(false);
417     }
418
419     else if (pItem == pAboutItem_)
420     {
421         about();
422     }
423
424
425     //Selection is just used to get notice of a menu item being clicked, removed after use
426
427     clearSelection();
428
429 }
430
431
432
433 void SeaScene::createMenuItems()
434 {
435
436     pPausetextItem_ =  addSimpleText("Game paused. Tap to continue.");
437     pPausetextItem_->setZValue(1000);
438     pPausetextItem_->setPos(250,50);
439     pPausetextItem_->hide();
440
441     menuItemCount_ = 0;
442
443     pRestartGameItem_ = new QGraphicsSimpleTextItem("Restart game");
444     prepareForMenu(pRestartGameItem_);
445
446     pRestartLevelItem_ = new QGraphicsSimpleTextItem("Restart level");
447     prepareForMenu(pRestartLevelItem_);
448
449     pSettingsItem_ = new QGraphicsSimpleTextItem("Settings");
450     prepareForMenu(pSettingsItem_);
451
452     pAboutItem_ = new QGraphicsSimpleTextItem("About");
453     prepareForMenu(pAboutItem_);
454
455 }
456
457 void SeaScene::prepareForMenu(QGraphicsItem * pItem)
458 {
459
460     //Menu items have pause text item as their parent and are thus added to scene automatically
461     //They are also shown and hidden with it, resulting in the menu being visble when the game is paused
462     //Their coordinates are given relative to the parent.
463
464     pItem->setParentItem(pPausetextItem_);
465     pItem->setZValue(1000);
466     pItem->setFlag(QGraphicsItem::ItemIsSelectable);
467     pItem->setY(150);
468     pItem->setX(menuItemCount_++*150-250);
469  }
470
471
472 void SeaScene::about()
473 {
474     QMessageBox::about(NULL, tr("About %1").arg(QApplication::applicationName()),
475                        tr("Version %1"
476                           "<p>Copyright 2011 Heli Hyv&auml;ttinen"
477                           "<p>License: General Public License v2"
478                           "<p>Bug Reports: https://bugs.maemo.org/ "
479                           "enter_bug.cgi?product=Ghosts%20Overboard"
480                           ).arg(QApplication::applicationVersion()));
481
482
483
484 }
485
486
487 void SeaScene::restartLevel()
488 {
489     setupMap(levelList_.value(currentLevel_));  //value() returns default constructor Level if index is invalid, so no risk of crash
490     vibrationActivate(pVibrateAction_->isChecked());  //Vibration effects are lost without this
491    // qDebug() << pVibrateAction_->isChecked();
492 }
493
494
495
496 void SeaScene::nextLevel()
497 {
498
499     currentLevel_++;
500
501     if (levelList_.empty())
502         setupMap(Level());
503
504
505     if ( currentLevel_ < levelList_.size() )
506     {
507        restartLevel();
508     }
509
510     else //Victory!
511     {
512
513        QDialog* pVictoryDialog = new QDialog();
514        pVictoryDialog->setWindowTitle(tr("You won!"));
515
516
517        QPushButton* pPlayAgainButton = new QPushButton(tr("Play again"));
518 //       QPushButton* pQuitButton = new QPushButton(tr("Quit game"));
519
520        QPixmap victoryIcon (":/pix/aavesaari.png");
521        QLabel* pVictoryLabel = new QLabel();
522        pVictoryLabel->setPixmap(victoryIcon);
523
524        QLabel* pTextLabel = new QLabel(tr("Congratulations! <p>You have saved all the ghosts."));
525
526
527        QVBoxLayout* pMainLayout = new QVBoxLayout;
528
529        QHBoxLayout* pTopLayout = new QHBoxLayout;
530        pMainLayout->addLayout(pTopLayout);
531
532        pTopLayout->addWidget(pVictoryLabel);
533        pTopLayout->addWidget(pTextLabel);
534
535
536
537        QHBoxLayout* pButtonLayout = new QHBoxLayout();
538        pMainLayout->addLayout(pButtonLayout);
539
540  //      pButtonLayout->addWidget(pQuitButton);
541        pButtonLayout->addWidget(pPlayAgainButton);
542
543
544
545        pVictoryDialog->setLayout(pMainLayout);
546
547        connect(pPlayAgainButton, SIGNAL(clicked()),pVictoryDialog,SLOT(accept()));
548
549        pVictoryDialog->exec();
550
551         //Never mind if the user cancels the dialog: restart the game anyway
552
553        restartGame();
554     }
555 }
556
557
558 void SeaScene::restartGame()
559 {
560     currentLevel_ = 0;
561     restartLevel();
562 }
563
564
565 void SeaScene::forcePause()
566 {
567     //Pause without setting the pause action state
568     pause(true);
569 }
570
571 void::SeaScene::softContinue()
572 {
573     //Continue if not being paused by the user
574     // Reverts forcePause()
575
576     pause(pPauseAction_->isChecked());
577 }