From 0fbb8077e72fd28bd5d89fbac989fd0e0d2c45b8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Heli=20Hyv=C3=A4ttinen?= Date: Mon, 6 Jun 2011 20:26:05 +0300 Subject: [PATCH] Ship can now pick ghosts --- seascene.cpp | 8 ++++++++ seascene.h | 2 ++ ship.cpp | 10 ++++++++++ ship.h | 5 +++++ 4 files changed, 25 insertions(+) diff --git a/seascene.cpp b/seascene.cpp index b330532..cfa78ec 100644 --- a/seascene.cpp +++ b/seascene.cpp @@ -98,6 +98,7 @@ void SeaScene::setupMap(int ghosts, int rocks, int octopuses) pShip->setData(0,"ship"); pShip->setPos(*pPosition); addItem(pShip); + connect(pShip,SIGNAL(pickingGhost(QGraphicsItem*)),this, SLOT(removeGhost(QGraphicsItem*)) ); pShip->startMoving(); delete pPosition; } @@ -131,3 +132,10 @@ QPointF* SeaScene::findRandomFreeSlot() 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; +} diff --git a/seascene.h b/seascene.h index bb97911..1d0c1fc 100644 --- a/seascene.h +++ b/seascene.h @@ -23,6 +23,8 @@ public slots: void spreadGhosts(int ghosts); + void removeGhost(QGraphicsItem * pGhost); + protected: /*! Gives a pointer to a random position if a free one is found. Otherwise returns NULL. diff --git a/ship.cpp b/ship.cpp index 6250522..2c26b45 100644 --- a/ship.cpp +++ b/ship.cpp @@ -4,6 +4,7 @@ Ship::Ship(QPixmap pixmap, QGraphicsItem *parent) : OrientationControlledGraphicsPixmapObject(pixmap,parent) { + ghostsAboard_ = 0; } @@ -16,8 +17,11 @@ bool Ship::handleCollisions() else { + //since the game logic does not leave items to collide with each other we can take just the topmost one + //and trust it is the only one QString type = collidesList.at(0)->data(0).toString(); qDebug() << type; + if (type == "rock" || type == "octopus") { // dropGhosts(); @@ -26,6 +30,12 @@ bool Ship::handleCollisions() else if (type == "ghost") { + ghostsAboard_++; + + qDebug() << ghostsAboard_ << " ghosts aboard"; + + emit pickingGhost(collidesList.at(0)); + return true; } diff --git a/ship.h b/ship.h index 38138a2..42ec29a 100644 --- a/ship.h +++ b/ship.h @@ -11,6 +11,9 @@ public: signals: + /*! Emitted when a ghost is hit */ + void pickingGhost(QGraphicsItem* pGhost); + public slots: protected: @@ -18,6 +21,8 @@ protected: protected: bool handleCollisions(); + int ghostsAboard_; + }; -- 1.7.9.5