1 /**************************************************************************
2 Ghosts Overboard - a game for Maemo 5
4 Copyright (C) 2011 Heli Hyvättinen
6 This file is part of Ghosts Overboard
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.
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.
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/>.
21 **************************************************************************/
26 #include <QGraphicsPixmapItem>
28 #include <QMessageBox>
30 #include <QApplication>
32 #include <QPushButton>
34 #include <QVBoxLayout>
36 const QString ghostImageFilename_ = ":/pix/aave.png";
37 const QString rockImageFilename_ =":/pix/kari.png";
38 const QString octopusImageFilename_= ":/pix/tursas.png";
41 SeaScene::SeaScene(QObject *parent) :
42 QGraphicsScene(parent)
45 screenLitKeeper_.keepScreenLit(true);
49 QPixmap waves (":/pix/meri.png");
51 setBackgroundBrush(QBrush(waves));
55 qsrand(QTime::currentTime().msec()+2); //+2 to avoid setting it to 1
57 //Setup the level list
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);
73 pVibrateAction_ = new QAction(tr("Vibration effects"),this);
74 pVibrateAction_->setCheckable(true);
75 connect(pVibrateAction_,SIGNAL(toggled(bool)),this,SLOT(vibrationActivate(bool)));
78 pPauseAction_ = new QAction(tr("Pause"),this);
79 pPauseAction_->setCheckable(true);
80 connect(pPauseAction_,SIGNAL(toggled(bool)),this,SLOT(pause(bool)));
85 void SeaScene::setupMap(int ghosts, int rocks, int octopuses, int octopusSpeed)
93 //empty the list of moving items
97 //empty the list of free slots
100 //fill the list of free slots
102 int numberOfXTiles = width() / 40;
103 int numberOfYTiles = height() /40;
105 // qDebug() << numberOfXTiles << " slots in x direction";
106 // qDebug() << numberOfYTiles << " slots in y rirection";
108 for (int i = 0; i < numberOfXTiles; i++ )
110 for (int j = 0; j < numberOfYTiles; j++)
112 freeTiles_.append(QPointF(i*40,j*40));
119 for (int i = 0; i < rocks; i++)
121 QPointF * pPosition = findRandomFreeSlot();
123 //If there was no room no point to continue
124 if (pPosition == NULL)
127 QPixmap rockPixmap (":/pix/kari.png");
128 QGraphicsPixmapItem * pRock = addPixmap(rockPixmap);
129 pRock->setData(0,"rock");
130 pRock->setPos(*pPosition);
137 ghostsLeft_ = ghosts;
138 spreadGhosts(ghosts);
142 //spread the octopuses
144 QList <Octopus*> octopusList;
146 for (int i=0; i < octopuses; i++)
148 QPointF * pPosition = findRandomFreeSlot();
150 //If there was no room no point to continue
151 if (pPosition == NULL)
154 QPixmap octopusPixmap (":/pix/tursas.png");
155 Octopus * pOctopus = new Octopus(octopusPixmap,octopusSpeed);
156 pOctopus->setData(0,"octopus");
157 pOctopus->setPos(*pPosition);
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);
171 QPointF * pPosition = findRandomFreeSlot();
172 if (pPosition == NULL)
174 // Game cannot begin without a free position for ship, so give an error message and return
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.");
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"));
193 Ship * pShip = new Ship (shipImages);
194 pShip->setData(0,"ship");
195 pShip->setPos(*pPosition);
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)
206 connect(pOctopus,SIGNAL(droppingGhosts()),pShip,SLOT(dropAllGhosts()));
211 void SeaScene::setupMap(Level level)
213 setupMap(level.getNumberOfGhosts(),level.getNumberOfRocks(),level.getNumberOfOctopuses(),level.getOctopusSpeed());
216 void SeaScene::spreadGhosts(int ghosts)
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
223 QList<QPointF> temporarilyReservedSlots;
225 foreach (QGraphicsItem* pItem, movingItems_)
229 // qDebug() << "NULL item in movingItems_";
233 //round x and y down to fit the slot size
243 QPointF position (x,y);
245 //remove the tiles (potentially) occupied by the item from free slots and place in temp list if was in the list before
247 if (freeTiles_.removeOne(position))
248 temporarilyReservedSlots.append(position);
253 if (freeTiles_.removeOne(position))
254 temporarilyReservedSlots.append(position);
258 if (freeTiles_.removeOne(position))
259 temporarilyReservedSlots.append(position);
263 if (freeTiles_.removeOne(position))
264 temporarilyReservedSlots.append(position);
269 //spread ghosts in random free slots
271 for (int i=0; i < ghosts; i++)
273 QPointF * pPosition = findRandomFreeSlot();
275 //If there was no room no point to continue
276 if (pPosition == NULL)
279 QPixmap ghostPixmap(":/pix/aave.png");
280 QGraphicsPixmapItem * pGhost = addPixmap(ghostPixmap);
281 pGhost->setData(0,"ghost");
282 pGhost->setPos(*pPosition);
286 //return the slots occupied by moving items to free slots
287 freeTiles_.append(temporarilyReservedSlots);
289 //clear temp for the next round
290 temporarilyReservedSlots.clear();
293 QPointF* SeaScene::findRandomFreeSlot()
295 if (freeTiles_.isEmpty())
298 int index = qrand()%freeTiles_.size();
300 // qDebug() << index << " index";
301 return new QPointF (freeTiles_.takeAt(index));
305 void SeaScene::removeGhost(QGraphicsItem *pGhost)
307 removeItem(pGhost); //remove the item from scene
308 freeTiles_.append(pGhost->scenePos()); //add the item's position to free slots
311 if (ghostsLeft_ == 0)
313 emit allGhostsPicked();
314 // qDebug() << "All ghosts picked!";
318 void SeaScene::ghostsDropped(int ghosts)
320 ghostsLeft_ += ghosts;
322 spreadGhosts(ghosts);
325 void SeaScene::pause(bool paused)
327 // qDebug() << "pause pressed " << paused;
328 if (paused_ == paused)
335 // qDebug() << "starting to move again";
337 screenLitKeeper_.keepScreenLit(true);
339 pPausetextItem_->hide();
344 qDebug("about to stop movement");
346 screenLitKeeper_.keepScreenLit(false);
347 if (pPausetextItem_ != NULL)
349 qDebug() << "about to show the pause text";
350 pPausetextItem_->show();
351 qDebug() << "showing pause text";
353 else qDebug() << "No pause text available";
357 void SeaScene::vibrationActivate(bool on)
359 emit vibrationActivated(on);
362 void SeaScene::handleScreenTapped()
365 //If the game is going just pause it
368 pPauseAction_->setChecked(true);
372 //If the game is paused, chacl if menu item was selected
374 QList<QGraphicsItem *> items = selectedItems();
376 //if nothing selected resume play
380 pPauseAction_->setChecked(false);
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)
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
394 if (pItem == pRestartGameItem_)
396 qDebug() << "game restart requested";
399 else if (pItem == pRestartLevelItem_)
401 qDebug() << "Level restart requested";
405 else if (pItem == pSettingsItem_)
410 else if (pItem == pAboutItem_)
416 //Selection is just used to get notice of a menu item being clicked, removed after use
424 void SeaScene::createMenuItems()
427 pPausetextItem_ = addSimpleText("Game paused. Tap to continue.");
428 pPausetextItem_->setZValue(1000);
429 pPausetextItem_->setPos(250,50);
430 pPausetextItem_->hide();
434 pRestartGameItem_ = new QGraphicsSimpleTextItem("Restart game");
435 prepareForMenu(pRestartGameItem_);
437 pRestartLevelItem_ = new QGraphicsSimpleTextItem("Restart level");
438 prepareForMenu(pRestartLevelItem_);
440 pSettingsItem_ = new QGraphicsSimpleTextItem("Settings");
441 prepareForMenu(pSettingsItem_);
443 pAboutItem_ = new QGraphicsSimpleTextItem("About");
444 prepareForMenu(pAboutItem_);
448 void SeaScene::prepareForMenu(QGraphicsItem * pItem)
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.
455 pItem->setParentItem(pPausetextItem_);
456 pItem->setZValue(1000);
457 pItem->setFlag(QGraphicsItem::ItemIsSelectable);
459 pItem->setX(menuItemCount_++*150-250);
463 void SeaScene::about()
465 QMessageBox::about(NULL, tr("About %1").arg(QApplication::applicationName()),
467 "<p>Copyright 2011 Heli Hyvä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()));
478 void SeaScene::restartLevel()
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();
487 void SeaScene::nextLevel()
492 if (levelList_.empty())
496 if ( currentLevel_ < levelList_.size() )
504 QDialog* pVictoryDialog = new QDialog();
505 pVictoryDialog->setWindowTitle(tr("You won!"));
508 QPushButton* pPlayAgainButton = new QPushButton(tr("Play again"));
509 // QPushButton* pQuitButton = new QPushButton(tr("Quit game"));
511 QPixmap victoryIcon (":/pix/aavesaari.png");
512 QLabel* pVictoryLabel = new QLabel();
513 pVictoryLabel->setPixmap(victoryIcon);
515 QLabel* pTextLabel = new QLabel(tr("Congratulations! <p>You have saved all the ghosts."));
518 QVBoxLayout* pMainLayout = new QVBoxLayout;
520 QHBoxLayout* pTopLayout = new QHBoxLayout;
521 pMainLayout->addLayout(pTopLayout);
523 pTopLayout->addWidget(pVictoryLabel);
524 pTopLayout->addWidget(pTextLabel);
528 QHBoxLayout* pButtonLayout = new QHBoxLayout();
529 pMainLayout->addLayout(pButtonLayout);
531 // pButtonLayout->addWidget(pQuitButton);
532 pButtonLayout->addWidget(pPlayAgainButton);
536 pVictoryDialog->setLayout(pMainLayout);
538 connect(pPlayAgainButton, SIGNAL(clicked()),pVictoryDialog,SLOT(accept()));
540 pVictoryDialog->exec();
542 //Never mind if the user cancels the dialog: restart the game anyway
549 void SeaScene::restartGame()
556 void SeaScene::forcePause()
558 //Pause without setting the pause action state
562 void::SeaScene::softContinue()
564 //Continue if not being paused by the user
565 // Reverts forcePause()
567 pause(pPauseAction_->isChecked());