X-Git-Url: http://vcs.maemo.org/git/?a=blobdiff_plain;f=seascene.cpp;h=0238cf686604e5cba047926b956cc4e6eb2c0d2f;hb=84b9d0406289da22178b3d5eef4c12e6554ade58;hp=7ca5d636df67bee86be8d14b527efefd5296afa4;hpb=83b995c5223795e85deb4fb372ca432fb765d37e;p=ghostsoverboard diff --git a/seascene.cpp b/seascene.cpp index 7ca5d63..0238cf6 100644 --- a/seascene.cpp +++ b/seascene.cpp @@ -1,5 +1,5 @@ /************************************************************************** - Ghosts Overboard - a game for Maemo 5 + Ghosts Overboard - a game for 'Meego 1.2 Harmattan' Copyright (C) 2011 Heli Hyvättinen @@ -32,6 +32,9 @@ #include #include #include +#include +#include + const QString ghostImageFilename_ = ":/pix/aave.png"; const QString rockImageFilename_ =":/pix/kari.png"; @@ -41,6 +44,9 @@ const QString octopusImageFilename_= ":/pix/tursas.png"; SeaScene::SeaScene(QObject *parent) : QGraphicsScene(parent) { + + setItemPointersNull(); + paused_ = false; screenLitKeeper_.keepScreenLit(true); @@ -54,29 +60,59 @@ SeaScene::SeaScene(QObject *parent) : qsrand(QTime::currentTime().msec()+2); //+2 to avoid setting it to 1 + + //Setup the level list + QList levelList; Level level1(5,10); - levelList_.append(level1); + levelList.append(level1); Level level2(5,10,2,50); - levelList_.append(level2); + levelList.append(level2); Level level3(5,15,2,50); - levelList_.append(level3); + levelList.append(level3); Level level4(5,15,4,50); - levelList_.append(level4); + levelList.append(level4); Level level5(5,15,5,100); - levelList_.append(level5); + levelList.append(level5); + + Levelset set ("Original",levelList); + levelset_ = set; + availableLevelsets_.append(set); currentLevel_ = 0; + totalScore_ = 0; + + //Create another set of levels and place it in the available levelsets list + levelList.clear(); + Level set2level1(8,15,4,50); + levelList.append(set2level1); + Level set2level2(8,20,4,50); + levelList.append(set2level2); + Level set2level3(8,20,5,100); + levelList.append(set2level3); + Level set2level4(8,20,6,150); + levelList.append(set2level4); + Level set2level5(8,25,8,200); + levelList.append(set2level5); + + Levelset set2("Difficult",levelList); + availableLevelsets_.append(set2); + + //This ensures that nextlevel will not be called until its safe to delete the Ship object. //Leaving out Qt::QueuedConnection or calling nextlevel directly instead of emitting the signal will CRASH connect(this,SIGNAL(allGhostsPicked()),this,SLOT(nextLevel()),Qt::QueuedConnection); + + pVibrateAction_ = new QAction(tr("Vibration effects"),this); pVibrateAction_->setCheckable(true); connect(pVibrateAction_,SIGNAL(toggled(bool)),this,SLOT(vibrationActivate(bool))); + QSettings settings; + pVibrateAction_->setChecked(settings.value("vibration",false).toBool()); pPauseAction_ = new QAction(tr("Pause"),this); @@ -84,6 +120,25 @@ SeaScene::SeaScene(QObject *parent) : connect(pPauseAction_,SIGNAL(toggled(bool)),this,SLOT(pause(bool))); + autopauseTimer.setSingleShot(true); + autopauseTimer.setInterval(15*60*1000); + connect(&autopauseTimer,SIGNAL(timeout()),this,SLOT(turnPauseOn())); + + vibrationAllowed_ = false; + pResourceSet_ = new ResourcePolicy::ResourceSet("game",this); + ResourcePolicy::VibraResource * pVibraResource = new ResourcePolicy::VibraResource(); + pVibraResource->setOptional(false); //The only resource of the set, so no sense for it to be optional + pResourceSet_->addResourceObject(pVibraResource); + + connect(pResourceSet_,SIGNAL(resourcesGranted(const QList< ResourcePolicy::ResourceType >)),this,SLOT(resourcesAvailable())); + connect(pResourceSet_,SIGNAL(lostResources()),this,SLOT(resourcesLost())); + + //To test whether resources were succesfully asked but not given + connect(pResourceSet_,SIGNAL(resourcesDenied()),this,SLOT(resourcesLost())); + pResourceSet_->setAlwaysReply(); + + pResourceSet_->acquire(); + } void SeaScene::setupMap(int ghosts, int rocks, int octopuses, int octopusSpeed) @@ -92,8 +147,19 @@ void SeaScene::setupMap(int ghosts, int rocks, int octopuses, int octopusSpeed) clear(); + setItemPointersNull(); + createMenuItems(); + createAboutBoxItems(); + + createSelectLevelsetFromListItems(); + + createVictoryItems(); + + createLevelCompletedItems(); + + //empty the list of moving items movingItems_.clear(); @@ -201,6 +267,10 @@ void SeaScene::setupMap(int ghosts, int rocks, int octopuses, int octopusSpeed) connect(pShip,SIGNAL(pickingGhost(QGraphicsItem*)),this, SLOT(removeGhost(QGraphicsItem*)) ); connect(pShip,SIGNAL(droppingGhosts(int)),this,SLOT(ghostsDropped(int))); connect(this,SIGNAL(vibrationActivated(bool)),pShip,SLOT(setVibrationActivate(bool))); + if (vibrationAllowed_) + pShip->allowVibration(); //Vibration is disallowed by default so only allowing needs to be done explicitly + connect(pResourceSet_,SIGNAL(resourcesGranted(const QList< ResourcePolicy::ResourceType >)),pShip,SLOT(allowVibration())); + connect(pResourceSet_,SIGNAL(lostResources()),pShip,SLOT(disallowVibration())); pShip->startMoving(); movingItems_.append(pShip); connect(this,SIGNAL(pauseOn()),pShip,SLOT(stopMoving())); @@ -337,24 +407,33 @@ void SeaScene::pause(bool paused) if (paused == false) { // qDebug() << "starting to move again"; +// emit fullscreenRequested(); fremantle specific (since no "show statusbar" action in harmattan version) emit pauseOff(); screenLitKeeper_.keepScreenLit(true); if (pPausetextItem_) pPausetextItem_->hide(); + + scoreCounter_.start(); + + autopauseTimer.start(); //Start counting towards autopause } else { - qDebug("about to stop movement"); +// qDebug("about to stop movement"); emit pauseOn(); screenLitKeeper_.keepScreenLit(false); if (pPausetextItem_ != NULL) { - qDebug() << "about to show the pause text"; +// qDebug() << "about to show the pause text"; pPausetextItem_->show(); - qDebug() << "showing pause text"; +// qDebug() << "showing pause text"; } - else qDebug() << "No pause text available"; +// else qDebug() << "No pause text available"; + + levelScore_ += scoreCounter_.elapsed(); + + autopauseTimer.stop(); //No need to count toward autopause when already paused } } @@ -373,7 +452,45 @@ void SeaScene::handleScreenTapped() return; } - //If the game is paused, chacl if menu item was selected + //If the game is paused and about box is shown, close it and show the pause text and menu again + + if(pAboutBoxItem_) + { + if(pAboutBoxItem_->isVisible()) + { + pAboutBoxItem_->hide(); + pPausetextItem_->show(); + return; + } + } + + //If the game is paused, check if the level completed item is shown + + if (pLevelCompletedItem_) + { + if (pLevelCompletedItem_->isVisible()) + { + pLevelCompletedItem_->hide(); + restartLevel(); //Current level has already been set to the next one before showing the level completed item + pPauseAction_->setChecked(false); //unpause + return; + } + } + + //If the game is paused, check if the victory item is being shown + if(pVictoryCongratulationsItem_) + { + if (pVictoryCongratulationsItem_->isVisibleTo(NULL)) //returns visibility to scene + { + pVictoryCongratulationsItem_->hide(); + restartGame(); + pPauseAction_->setChecked(false); // unpause + return; + } + } + + + //If the game is paused and no victory or about box, check if menu item was selected QList items = selectedItems(); @@ -381,7 +498,9 @@ void SeaScene::handleScreenTapped() if (items.isEmpty()) { + pSelectLevelsetFromListItem_->hide(); pPauseAction_->setChecked(false); + return; } @@ -394,30 +513,43 @@ void SeaScene::handleScreenTapped() QGraphicsItem* pItem = items.at(0); //Selecting an item brings here, thus only selecting one item should be possible //... so we can just take the first one + //Selection is just used to get notice of a menu item being clicked, removed after use + + clearSelection(); + + + //Act upon the selected item + if (pItem == pRestartGameItem_) { - qDebug() << "game restart requested"; +// qDebug() << "game restart requested"; restartGame(); + pPauseAction_->setChecked(false); //unpause game + } else if (pItem == pRestartLevelItem_) { - qDebug() << "Level restart requested"; +// qDebug() << "Level restart requested"; restartLevel(); + pPauseAction_->setChecked(false); //unpause game } else if (pItem == pSettingsItem_) { - //Temporary code for settings, likely to be turned into a QML dialog + pVibrateAction_->toggle(); - QMessageBox::StandardButton buttonpressed = QMessageBox::question(NULL,"Settings","Do you wish to have vibration effects enabled?", QMessageBox::Yes | QMessageBox::No); + QSettings settings; + settings.setValue("vibration",pVibrateAction_->isChecked()); - if (buttonpressed == QMessageBox::Yes) - pVibrateAction_->setChecked(true); - if (buttonpressed == QMessageBox::No) - pVibrateAction_->setChecked(false); + QString text = pSettingsItem_->toHtml(); + if (pVibrateAction_->isChecked()) + text.replace(" on"," off"); //don't remove spaces or you get vibratioff... + else + text.replace(" off"," on"); + pSettingsItem_->setHtml(text); } else if (pItem == pAboutItem_) @@ -430,14 +562,29 @@ void SeaScene::handleScreenTapped() qApp->quit(); } + else if (pItem == pChooseLevelsetItem_) + { + pPausetextItem_->hide(); + pSelectLevelsetFromListItem_->show(); + } - //Selection is just used to get notice of a menu item being clicked, removed after use - - clearSelection(); - - //The user propably went to paused state just to access menu, so unpause - - pPauseAction_->setChecked(false); + else + { + foreach (QGraphicsItem* pLevelItem, levelsetItems_) + { + if (pItem == pLevelItem) + { + QVariant variant = pLevelItem->data(0); + + if (variant.canConvert()) + { + levelset_ = variant.value(); + restartGame(); + pPauseAction_->setChecked(false); //unpause game + } + } + } + } } @@ -483,8 +630,23 @@ void SeaScene::createMenuItems() pRestartLevelItem_->setHtml(tr("Restart
level").prepend(menufonthtml)); prepareForMenu(pRestartLevelItem_); + pChooseLevelsetItem_ = new QGraphicsTextItem; + pChooseLevelsetItem_->setHtml(tr("Choose
levelset").prepend(menufonthtml)); + prepareForMenu(pChooseLevelsetItem_); + pSettingsItem_ = new QGraphicsTextItem; - pSettingsItem_->setHtml(tr("Vibration
effects").prepend(menufonthtml)); + QString vibraText(tr("Turn vibration
effects ")); + QString statusText; + if (pVibrateAction_->isChecked()) + { + statusText = "off"; + } + else + { + statusText = "on"; + } + vibraText.append(statusText); + pSettingsItem_->setHtml(vibraText.prepend(menufonthtml)); prepareForMenu(pSettingsItem_); pAboutItem_ = new QGraphicsTextItem; @@ -504,35 +666,45 @@ void SeaScene::prepareForMenu(QGraphicsItem * pItem) //They are also shown and hidden with it, resulting in the menu being visble when the game is paused //Their coordinates are given relative to the parent. + int itemsPerRow = 3; pItem->setParentItem(pPausetextItem_); pItem->setZValue(1000); pItem->setFlag(QGraphicsItem::ItemIsSelectable); - pItem->setY(150); - pItem->setX(menuItemCount_++*160-150); - } + if (menuItemCount_< itemsPerRow) + { + pItem->setY(120); + } + else pItem->setY(240); -void SeaScene::about() -{ - QMessageBox::about(NULL, tr("About %1").arg(QApplication::applicationName()), - tr("Version %1" - "

Copyright 2011 Heli Hyvättinen" - "

License: General Public License v2" - "

Bug Reports: https://bugs.maemo.org/ " - "enter_bug.cgi?product=Ghosts%20Overboard" - ).arg(QApplication::applicationVersion())); + pItem->setX((menuItemCount_++%itemsPerRow)*180-10); + } +void SeaScene::about() +{ + pPausetextItem_->hide(); + pAboutBoxItem_->show(); } void SeaScene::restartLevel() { - setupMap(levelList_.value(currentLevel_)); //value() returns default constructor Level if index is invalid, so no risk of crash + + levelScore_ = 0; + + setupMap(levelset_.getLevel(currentLevel_)); //getLevel() returns default constructor Level if index is invalid, so no risk of crash + + scoreCounter_.start(); + vibrationActivate(pVibrateAction_->isChecked()); //Vibration effects are lost without this // qDebug() << pVibrateAction_->isChecked(); + + autopauseTimer.start(); //reset counting towards autopause + + } @@ -540,82 +712,233 @@ void SeaScene::restartLevel() void SeaScene::nextLevel() { - currentLevel_++; + //get score for previous level + levelScore_ += scoreCounter_.elapsed(); + totalScore_ += levelScore_; + int highscore = levelset_.getLevelHighScore(currentLevel_); - if (levelList_.empty()) - setupMap(Level()); + qDebug() << highscore; + QString scoretext; - if ( currentLevel_ < levelList_.size() ) + if (levelScore_ >= highscore) { - restartLevel(); + scoretext = tr("Your time: %1.%2 s
Best time: %3.%4 s").arg(levelScore_/1000).arg((levelScore_%1000)/100).arg(highscore/1000).arg((highscore%1000)/100); } - else //Victory! + else //New high score! + { + scoretext = tr("Your time %1.%2 s is
the new best time!").arg(levelScore_/1000).arg((levelScore_%1000)/100); + levelset_.setLevelHighScore(currentLevel_,levelScore_); + } - QDialog* pVictoryDialog = new QDialog(); - pVictoryDialog->setWindowTitle(tr("You won!")); + //pause to show the highscore or victory screen + turnPauseOn(); + pPausetextItem_->hide(); - QPushButton* pPlayAgainButton = new QPushButton(tr("Play again")); -// QPushButton* pQuitButton = new QPushButton(tr("Quit game")); - QPixmap victoryIcon (":/pix/aavesaari.png"); - QLabel* pVictoryLabel = new QLabel(); - pVictoryLabel->setPixmap(victoryIcon); + //Go to the next level if available + currentLevel_++; + + if ( currentLevel_ < levelset_.numberOfLevels() ) + { + + pLevelCompletedItem_->setHtml(scoretext); + pLevelCompletedItem_->show(); +// restartLevel(); + } + + else //Victory! + { + int totalHighsore = levelset_.getTotalHighScore(); + if (totalScore_ >= totalHighsore) + { + scoretext.append(tr("
Your total time: %1.%2 s
Best total time: %3.%4 s").arg(totalScore_/1000).arg((totalScore_%1000)/100).arg(totalHighsore/1000).arg((totalHighsore%1000)/100)); + } + else //new total high score + { + scoretext.append(tr("
Your total time %1.%2 s is
the new best time").arg(totalScore_/1000).arg((totalScore_%1000)/100)); + levelset_.setTotalHighScore(totalScore_); + } - QLabel* pTextLabel = new QLabel(tr("Congratulations!

You have saved all the ghosts.")); + pVictoryScoreItem_->setHtml(scoretext); + pVictoryCongratulationsItem_->show(); + } +} - QVBoxLayout* pMainLayout = new QVBoxLayout; +void SeaScene::restartGame() +{ + currentLevel_ = 0; + totalScore_ = 0; + restartLevel(); +} - QHBoxLayout* pTopLayout = new QHBoxLayout; - pMainLayout->addLayout(pTopLayout); - pTopLayout->addWidget(pVictoryLabel); - pTopLayout->addWidget(pTextLabel); +void SeaScene::forcePause() +{ + //Pause without setting the pause action state + pause(true); +} +void SeaScene::softContinue() +{ + //Continue if not being paused by the user + // Reverts forcePause() + pause(pPauseAction_->isChecked()); +} - QHBoxLayout* pButtonLayout = new QHBoxLayout(); - pMainLayout->addLayout(pButtonLayout); - // pButtonLayout->addWidget(pQuitButton); - pButtonLayout->addWidget(pPlayAgainButton); +void SeaScene::createVictoryItems() +{ + pVictoryCongratulationsItem_ = new QGraphicsTextItem; + pVictoryCongratulationsItem_->setHtml(" Victory!"); + pVictoryCongratulationsItem_->hide(); + pVictoryCongratulationsItem_->setPos(315,30); + pVictoryCongratulationsItem_->setZValue(1000); + addItem(pVictoryCongratulationsItem_); + //coordinates are relative to the parent - pVictoryDialog->setLayout(pMainLayout); + QGraphicsTextItem * pTextItem = new QGraphicsTextItem(pVictoryCongratulationsItem_); + pTextItem->setHtml(" Congratulations!"); + pTextItem->setPos(-50,80); + pTextItem->setZValue(1000); - connect(pPlayAgainButton, SIGNAL(clicked()),pVictoryDialog,SLOT(accept())); + QGraphicsTextItem * pMiddleTextItem = new QGraphicsTextItem(pVictoryCongratulationsItem_); + pMiddleTextItem->setHtml(" You have saved all the ghosts."); + pMiddleTextItem->setPos(-145,120); + pMiddleTextItem->setZValue(1000); - pVictoryDialog->exec(); - //Never mind if the user cancels the dialog: restart the game anyway + pVictoryScoreItem_ = new QGraphicsTextItem(pVictoryCongratulationsItem_); + pVictoryScoreItem_->setPos(-50,180); + pMiddleTextItem->setZValue(1000); + //Text is set at usetime! - restartGame(); - } + QGraphicsTextItem * pLowestTextItem = new QGraphicsTextItem(pVictoryCongratulationsItem_); + pLowestTextItem->setHtml(" Tap to play again"); + pLowestTextItem->setPos(-50,360); + pLowestTextItem->setZValue(1000); } -void SeaScene::restartGame() +void SeaScene::createAboutBoxItems() { - currentLevel_ = 0; - restartLevel(); -} + pAboutBoxItem_ = new QGraphicsTextItem; + addItem(pAboutBoxItem_); + pAboutBoxItem_->setPos(25,50); + pAboutBoxItem_->setZValue(1000); + pAboutBoxItem_->hide(); + + pAboutBoxItem_->setHtml(tr("" + "%1 Version %2" + "

Copyright 2011 Heli Hyvättinen" + "

License: GNU General Public License v2" + "

The source code of this game is available trough it's web pages." + "

Web: http://ghostsoverboard.garage.maemo.org/
" + "Bug Reports:
https://bugs.maemo.org/" + "enter_bug.cgi?product=Ghosts%20Overboard" + ).arg(QApplication::applicationName(),QApplication::applicationVersion())); +} -void SeaScene::forcePause() +void SeaScene::setItemPointersNull() { - //Pause without setting the pause action state - pause(true); + pPausetextItem_ = NULL; + pRestartLevelItem_ = NULL; + pRestartGameItem_ = NULL; + pSettingsItem_ = NULL; + pAboutItem_ = NULL; + pQuitItem_ = NULL ; +// pMinimizeItem_ = NULL; //Fremantle spesific + pChooseLevelsetItem_ = NULL; + + pAboutBoxItem_ = NULL; + pLevelCompletedItem_ = NULL; + pVictoryCongratulationsItem_ = NULL; + pVictoryScoreItem_ = NULL; + } -void::SeaScene::softContinue() +void SeaScene::turnPauseOn() { - //Continue if not being paused by the user - // Reverts forcePause() + pPauseAction_->setChecked(true); +} - pause(pPauseAction_->isChecked()); + + +void SeaScene::createLevelCompletedItems() +{ + pLevelCompletedItem_ = new QGraphicsTextItem; + addItem(pLevelCompletedItem_); + pLevelCompletedItem_->setPos(240,100); + pLevelCompletedItem_->setZValue(1000); + pLevelCompletedItem_->hide(); + //The text is set at usetime + + QGraphicsTextItem * pTapForNextLevelItem = new QGraphicsTextItem(pLevelCompletedItem_); + pTapForNextLevelItem->setPos(-60,100); + pTapForNextLevelItem->setZValue(1000); + pTapForNextLevelItem->setHtml("Tap to start the next level"); } + +void SeaScene::createSelectLevelsetFromListItems() +{ + + if (availableLevelsets_.isEmpty()) //Something is badly wrong in code if this is true... + return; + + + pSelectLevelsetFromListItem_ = new QGraphicsTextItem; + addItem(pSelectLevelsetFromListItem_); + pSelectLevelsetFromListItem_->setPos(295,60); + pSelectLevelsetFromListItem_->setZValue(1000); + pSelectLevelsetFromListItem_->hide(); + + + QString fontstring (""); + + pSelectLevelsetFromListItem_->setHtml(tr("Choose a levelset").prepend(fontstring)); + + int yPos = 100; + + levelsetItems_.clear(); + + + foreach (Levelset set, availableLevelsets_) + { + QGraphicsTextItem * pItem = new QGraphicsTextItem(pSelectLevelsetFromListItem_); + QString text (fontstring); + if (levelset_.getName() == set.getName()) + text.append(""); + text.append(set.getName()); + pItem->setHtml(text); + pItem->setPos(65,yPos); + yPos+=80; + pItem->setZValue(1000); + pItem->setFlag(QGraphicsItem::ItemIsSelectable); + pItem->setData(0,QVariant::fromValue(set)); + levelsetItems_.append(pItem); + + } + + } + + + void SeaScene::resourcesAvailable() + { + qDebug() << "Resources available"; + vibrationAllowed_ = true; + } + + + void SeaScene::resourcesLost() + { + qDebug() << "Resources lost"; + vibrationAllowed_ = false; + }