void SeaScene::setupMap(int ghosts, int rocks, int octopuses)
{
+ //empty the map
+
+ clear();
+
//empty the list of free slots
freeTiles_.clear();
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;
//spread the ghosts
+ ghostsLeft_ = ghosts;
spreadGhosts(ghosts);
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;
}
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();
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()
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);
+}