#include "seascene.h"
+#include "timercontrolledtursas.h"
+#include "ship.h"
#include <QGraphicsPixmapItem>
#include <QDebug>
+#include <QMessageBox>
const QString ghostImageFilename_ = ":/pix/aave.png";
const QString rockImageFilename_ =":/pix/kari.png";
freeTiles_.append(QPointF(i*40,j*40));
}
}
+
+
+ //spread the rocks
+
+ for (int i = 0; i < rocks; i++)
+ {
+ QPointF * pPosition = findRandomFreeSlot();
+
+ //If there was no room no point to continue
+ if (pPosition == NULL)
+ break;
+
+ QGraphicsPixmapItem * pRock = addPixmap(QPixmap(":/pix/kari.png"));
+ pRock->setData(0,"rock");
+ pRock->setPos(*pPosition);
+ delete pPosition;
+
+ }
+
+ //spread the ghosts
+
spreadGhosts(ghosts);
+
+
+
+ //spread the octopuses
+
+
+ for (int i=0; i < octopuses; i++)
+ {
+ QPointF * pPosition = findRandomFreeSlot();
+
+ //If there was no room no point to continue
+ if (pPosition == NULL)
+ break;
+
+ TimerControlledTursas * pOctopus = new TimerControlledTursas (QPixmap(":/pix/tursas.png"),100);
+ pOctopus->setData(0,"octopus");
+ pOctopus->setPos(*pPosition);
+ addItem(pOctopus);
+ pOctopus->startMoving();
+ delete pPosition;
+
+ }
+
+
+ //place the ship
+
+ QPointF * pPosition = findRandomFreeSlot();
+ if (pPosition == NULL)
+ {
+ // Game cannot begin without a free position for ship, so give an error message and return
+
+ QMessageBox::critical(NULL,"Error! Too many objects on screen","No free space to place the ship. The game cannot start. Please choose another level.");
+ return;
+ }
+
+ Ship * pShip = new Ship (QPixmap(":/pix/laiva.png"));
+ pShip->setData(0,"ship");
+ pShip->setPos(*pPosition);
+ addItem(pShip);
+ connect(pShip,SIGNAL(pickingGhost(QGraphicsItem*)),this, SLOT(removeGhost(QGraphicsItem*)) );
+ pShip->startMoving();
+ delete pPosition;
}
+
void SeaScene::spreadGhosts(int ghosts)
{
for (int i=0; i < ghosts; i++)
{
- QPointF position = findRandomFreeSlot();
+ QPointF * pPosition = findRandomFreeSlot();
+
+ //If there was no room no point to continue
+ if (pPosition == NULL)
+ return;
QGraphicsPixmapItem * pGhost = addPixmap(QPixmap(":/pix/aave.png"));
pGhost->setData(0,"ghost");
- pGhost->setPos(position);
+ pGhost->setPos(*pPosition);
+ delete pPosition;
}
}
-QPointF SeaScene::findRandomFreeSlot()
+QPointF* SeaScene::findRandomFreeSlot()
{
+ if (freeTiles_.isEmpty())
+ return NULL;
+
int index = qrand()%freeTiles_.size();
qDebug() << index << " index";
- return freeTiles_.takeAt(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;
}