Spread ghost should no longer be placed on top of the ship and octupuses
[ghostsoverboard] / seascene.cpp
index b330532..c0bda9a 100644 (file)
@@ -19,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();
 
@@ -49,7 +53,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 +63,7 @@ void SeaScene::setupMap(int ghosts, int rocks, int octopuses)
 
     //spread the ghosts
 
+    ghostsLeft_ = ghosts;
     spreadGhosts(ghosts);
 
 
@@ -73,11 +79,13 @@ 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);
     delete pPosition;
 
     }
@@ -98,13 +106,61 @@ 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*)) );
+    connect(pShip,SIGNAL(droppingGhosts(int)),this,SLOT(ghostsDropped(int)));
     pShip->startMoving();
+    movingItems_.append(pShip);
     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<QPointF> temporarilyReservedSlots;
+
+    foreach (QGraphicsItem* pItem, movingItems_)
+    {
+    //TODO
+        //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 +169,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()
@@ -131,3 +194,23 @@ 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;
+    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);
+}