X-Git-Url: http://vcs.maemo.org/git/?a=blobdiff_plain;ds=sidebyside;f=seascene.cpp;h=0e4ff6cba0a8b6108035eb1dd7da0e4957fec807;hb=refs%2Ftags%2Fv0.1.0;hp=b33053251d3f3ba8e6bdd1e21e206d60e5859615;hpb=12868c14365387679c441440b751b0e922a2857c;p=ghostsoverboard diff --git a/seascene.cpp b/seascene.cpp index b330532..0e4ff6c 100644 --- a/seascene.cpp +++ b/seascene.cpp @@ -4,6 +4,7 @@ #include #include #include +#include const QString ghostImageFilename_ = ":/pix/aave.png"; const QString rockImageFilename_ =":/pix/kari.png"; @@ -13,12 +14,33 @@ const QString octopusImageFilename_= ":/pix/tursas.png"; SeaScene::SeaScene(QObject *parent) : QGraphicsScene(parent) { + paused_ = false; + screenLitKeeper_.keepScreenLit(true); + + //set background + + QPixmap waves (":/pix/meri.png"); + waves.scaled(20,20); + setBackgroundBrush(QBrush(waves)); + + //set random seed + + qsrand(QTime::currentTime().msec()+2); //+2 to avoid setting it to 1 + } void SeaScene::setupMap(int ghosts, int rocks, int octopuses) { + //empty the map + + clear(); + + //empty the list of moving items + + movingItems_.clear(); + //empty the list of free slots freeTiles_.clear(); @@ -27,8 +49,8 @@ void SeaScene::setupMap(int ghosts, int rocks, int octopuses) int numberOfXTiles = width() / 40; int numberOfYTiles = height() /40; - qDebug() << numberOfXTiles << " slots in x direction"; - qDebug() << numberOfYTiles << " slots in y rirection"; +// qDebug() << numberOfXTiles << " slots in x direction"; +// qDebug() << numberOfYTiles << " slots in y rirection"; for (int i = 0; i < numberOfXTiles; i++ ) { @@ -49,7 +71,8 @@ void SeaScene::setupMap(int ghosts, int rocks, int octopuses) if (pPosition == NULL) break; - QGraphicsPixmapItem * pRock = addPixmap(QPixmap(":/pix/kari.png")); + QPixmap rockPixmap (":/pix/kari.png"); + QGraphicsPixmapItem * pRock = addPixmap(rockPixmap); pRock->setData(0,"rock"); pRock->setPos(*pPosition); delete pPosition; @@ -58,6 +81,7 @@ void SeaScene::setupMap(int ghosts, int rocks, int octopuses) //spread the ghosts + ghostsLeft_ = ghosts; spreadGhosts(ghosts); @@ -73,11 +97,15 @@ void SeaScene::setupMap(int ghosts, int rocks, int octopuses) if (pPosition == NULL) break; - TimerControlledTursas * pOctopus = new TimerControlledTursas (QPixmap(":/pix/tursas.png"),100); + QPixmap octopusPixmap (":/pix/tursas.png"); + TimerControlledTursas * pOctopus = new TimerControlledTursas (octopusPixmap,100); pOctopus->setData(0,"octopus"); pOctopus->setPos(*pPosition); addItem(pOctopus); pOctopus->startMoving(); + movingItems_.append(pOctopus); + connect(this,SIGNAL(pauseOn()),pOctopus,SLOT(stopMoving())); + connect(this,SIGNAL(pauseOff()),pOctopus,SLOT(startMoving())); delete pPosition; } @@ -94,17 +122,88 @@ void SeaScene::setupMap(int ghosts, int rocks, int octopuses) return; } - Ship * pShip = new Ship (QPixmap(":/pix/laiva.png")); + 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(); + movingItems_.append(pShip); + connect(this,SIGNAL(pauseOn()),pShip,SLOT(stopMoving())); + connect(this,SIGNAL(pauseOff()),pShip,SLOT(startMoving())); delete pPosition; } void SeaScene::spreadGhosts(int ghosts) { + + + //the octopuses and the ship may have moved from their original positions, + //so the list of free slots must be adjusted to exclude their current positions + + QList temporarilyReservedSlots; + + foreach (QGraphicsItem* pItem, movingItems_) + { + if (pItem == NULL) + { + // qDebug() << "NULL item in movingItems_"; + continue; + } + + //round x and y down to fit the slot size + int x = pItem->x(); + x = x/40; + x = x*40; + + int y = pItem->y(); + y = y/40; + y=y*40; + + + QPointF position (x,y); + + //remove the tiles (potentially) occupied by the item from free slots and place in temp list if was in the list before + + if (freeTiles_.removeOne(position)) + temporarilyReservedSlots.append(position); + + + position.setX(x+40); + + if (freeTiles_.removeOne(position)) + temporarilyReservedSlots.append(position); + + position.setY(y+40); + + if (freeTiles_.removeOne(position)) + temporarilyReservedSlots.append(position); + + position.setX(x); + + if (freeTiles_.removeOne(position)) + temporarilyReservedSlots.append(position); + + } + + + //spread ghosts in random free slots + for (int i=0; i < ghosts; i++) { QPointF * pPosition = findRandomFreeSlot(); @@ -113,11 +212,18 @@ void SeaScene::spreadGhosts(int ghosts) if (pPosition == NULL) return; - QGraphicsPixmapItem * pGhost = addPixmap(QPixmap(":/pix/aave.png")); + QPixmap ghostPixmap(":/pix/aave.png"); + QGraphicsPixmapItem * pGhost = addPixmap(ghostPixmap); pGhost->setData(0,"ghost"); pGhost->setPos(*pPosition); delete pPosition; } + + //return the slots occupied by moving items to free slots + freeTiles_.append(temporarilyReservedSlots); + + //clear temp for the next round + temporarilyReservedSlots.clear(); } QPointF* SeaScene::findRandomFreeSlot() @@ -127,7 +233,50 @@ QPointF* SeaScene::findRandomFreeSlot() int index = qrand()%freeTiles_.size(); - qDebug() << index << " index"; +// qDebug() << index << " 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) + { + emit allGhostsPicked(); + // qDebug() << "All ghosts picked!"; + } +} + +void SeaScene::ghostsDropped(int ghosts) +{ + ghostsLeft_ += ghosts; + + spreadGhosts(ghosts); +} + +void SeaScene::pause(bool paused) +{ + // qDebug() << "pause pressed " << paused; + if (paused_ == paused) + return; + + paused_ = paused; + + if (paused == false) + { + // qDebug() << "starting to move again"; + emit pauseOff(); + screenLitKeeper_.keepScreenLit(true); + } + + else + { + // qDebug("about to stop movement"); + emit pauseOn(); + screenLitKeeper_.keepScreenLit(false); + } +}