Ship can now pick ghosts
authorHeli Hyvättinen <heli.hyvattinen@kymp.net>
Mon, 6 Jun 2011 17:26:05 +0000 (20:26 +0300)
committerHeli Hyvättinen <heli.hyvattinen@kymp.net>
Mon, 6 Jun 2011 17:26:05 +0000 (20:26 +0300)
seascene.cpp
seascene.h
ship.cpp
ship.h

index b330532..cfa78ec 100644 (file)
@@ -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;
+}
index bb97911..1d0c1fc 100644 (file)
@@ -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.
index 6250522..2c26b45 100644 (file)
--- 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 (file)
--- 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_;
+
 
 };