X-Git-Url: http://vcs.maemo.org/git/?a=blobdiff_plain;f=seascene.cpp;h=e4162be91670c3292d40868c4debd3cad5334296;hb=1c8f72e574cf69c9f590ac50b064af716ef1bce2;hp=eaaef92be9422487e6b363c02cfbc00ecfcaa80c;hpb=f8775657da3dd7f37ecd3bd4f8c53a17cee0aebe;p=ghostsoverboard diff --git a/seascene.cpp b/seascene.cpp index eaaef92..e4162be 100644 --- a/seascene.cpp +++ b/seascene.cpp @@ -1,6 +1,9 @@ #include "seascene.h" +#include "timercontrolledtursas.h" +#include "ship.h" #include #include +#include const QString ghostImageFilename_ = ":/pix/aave.png"; const QString rockImageFilename_ =":/pix/kari.png"; @@ -16,6 +19,10 @@ SeaScene::SeaScene(QObject *parent) : void SeaScene::setupMap(int ghosts, int rocks, int octopuses) { + //empty the map + + clear(); + //empty the list of free slots freeTiles_.clear(); @@ -34,26 +41,136 @@ void SeaScene::setupMap(int ghosts, int rocks, int octopuses) freeTiles_.append(QPointF(i*40,j*40)); } } + + + //spread the rocks + + for (int i = 0; i < rocks; i++) + { + QPointF * pPosition = findRandomFreeSlot(); + + //If there was no room no point to continue + if (pPosition == NULL) + break; + + QPixmap rockPixmap (":/pix/kari.png"); + QGraphicsPixmapItem * pRock = addPixmap(rockPixmap); + pRock->setData(0,"rock"); + pRock->setPos(*pPosition); + delete pPosition; + + } + + //spread the ghosts + + ghostsLeft_ = ghosts; spreadGhosts(ghosts); + + + + //spread the octopuses + + + for (int i=0; i < octopuses; i++) + { + QPointF * pPosition = findRandomFreeSlot(); + + //If there was no room no point to continue + if (pPosition == NULL) + break; + + QPixmap octopusPixmap (":/pix/tursas.png"); + TimerControlledTursas * pOctopus = new TimerControlledTursas (octopusPixmap,100); + pOctopus->setData(0,"octopus"); + pOctopus->setPos(*pPosition); + addItem(pOctopus); + pOctopus->startMoving(); + delete pPosition; + + } + + + //place the ship + + QPointF * pPosition = findRandomFreeSlot(); + if (pPosition == NULL) + { + // Game cannot begin without a free position for ship, so give an error message and return + + QMessageBox::critical(NULL,"Error! Too many objects on screen","No free space to place the ship. The game cannot start. Please choose another level."); + return; + } + + QList shipImages; + shipImages.append(QPixmap(":/pix/laiva.png")); + shipImages.append(QPixmap(":/pix/laiva_1aave.png")); + shipImages.append(QPixmap(":/pix/laiva_2aave.png")); + shipImages.append(QPixmap(":/pix/laiva_3aave.png")); + shipImages.append(QPixmap(":/pix/laiva_4aave.png")); + shipImages.append(QPixmap(":/pix/laiva_5aave.png")); + shipImages.append(QPixmap(":/pix/laiva_6aave.png")); + shipImages.append(QPixmap(":/pix/laiva_7aave.png")); + shipImages.append(QPixmap(":/pix/laiva_8aave.png")); + shipImages.append(QPixmap(":/pix/laiva_9aave.png")); + shipImages.append(QPixmap(":/pix/laiva_10aave.png")); + + Ship * pShip = new Ship (shipImages); + pShip->setData(0,"ship"); + pShip->setPos(*pPosition); + addItem(pShip); + connect(pShip,SIGNAL(pickingGhost(QGraphicsItem*)),this, SLOT(removeGhost(QGraphicsItem*)) ); + connect(pShip,SIGNAL(droppingGhosts(int)),this,SLOT(ghostsDropped(int))); + pShip->startMoving(); + delete pPosition; } + void SeaScene::spreadGhosts(int ghosts) { for (int i=0; i < ghosts; i++) { - QPointF position = findRandomFreeSlot(); + QPointF * pPosition = findRandomFreeSlot(); - QGraphicsPixmapItem * pGhost = addPixmap(QPixmap(":/pix/aave.png")); + //If there was no room no point to continue + if (pPosition == NULL) + return; + + QPixmap ghostPixmap(":/pix/aave.png"); + QGraphicsPixmapItem * pGhost = addPixmap(ghostPixmap); pGhost->setData(0,"ghost"); - pGhost->setPos(position); + pGhost->setPos(*pPosition); + delete pPosition; } } -QPointF SeaScene::findRandomFreeSlot() +QPointF* SeaScene::findRandomFreeSlot() { + if (freeTiles_.isEmpty()) + return NULL; + int index = qrand()%freeTiles_.size(); qDebug() << index << " index"; - return freeTiles_.takeAt(index); + return new QPointF (freeTiles_.takeAt(index)); } + +void SeaScene::removeGhost(QGraphicsItem *pGhost) +{ + removeItem(pGhost); //remove the item from scene + freeTiles_.append(pGhost->scenePos()); //add the item's position to free slots + delete pGhost; + ghostsLeft_--; + if (ghostsLeft_ == 0) + { + //here whatever happens when a level is complete / a signal + qDebug() << "All ghosts picked!"; + } +} + +void SeaScene::ghostsDropped(int ghosts) +{ + ghostsLeft_ += ghosts; + + spreadGhosts(ghosts); +}