2 #include "timercontrolledtursas.h"
4 #include <QGraphicsPixmapItem>
8 const QString ghostImageFilename_ = ":/pix/aave.png";
9 const QString rockImageFilename_ =":/pix/kari.png";
10 const QString octopusImageFilename_= ":/pix/tursas.png";
13 SeaScene::SeaScene(QObject *parent) :
14 QGraphicsScene(parent)
20 void SeaScene::setupMap(int ghosts, int rocks, int octopuses)
22 //empty the list of free slots
25 //fill the list of free slots
27 int numberOfXTiles = width() / 40;
28 int numberOfYTiles = height() /40;
30 qDebug() << numberOfXTiles << " slots in x direction";
31 qDebug() << numberOfYTiles << " slots in y rirection";
33 for (int i = 0; i < numberOfXTiles; i++ )
35 for (int j = 0; j < numberOfYTiles; j++)
37 freeTiles_.append(QPointF(i*40,j*40));
44 for (int i = 0; i < rocks; i++)
46 QPointF * pPosition = findRandomFreeSlot();
48 //If there was no room no point to continue
49 if (pPosition == NULL)
52 QGraphicsPixmapItem * pRock = addPixmap(QPixmap(":/pix/kari.png"));
53 pRock->setData(0,"rock");
54 pRock->setPos(*pPosition);
65 //spread the octopuses
68 for (int i=0; i < octopuses; i++)
70 QPointF * pPosition = findRandomFreeSlot();
72 //If there was no room no point to continue
73 if (pPosition == NULL)
76 TimerControlledTursas * pOctopus = new TimerControlledTursas (QPixmap(":/pix/tursas.png"),100);
77 pOctopus->setData(0,"octopus");
78 pOctopus->setPos(*pPosition);
80 pOctopus->startMoving();
88 QPointF * pPosition = findRandomFreeSlot();
89 if (pPosition == NULL)
91 // Game cannot begin without a free position for ship, so give an error message and return
93 QMessageBox::critical(NULL,"Error! Too many objects on screen","No free space to place the ship. The game cannot start. Please choose another level.");
97 Ship * pShip = new Ship (QPixmap(":/pix/laiva.png"));
98 pShip->setData(0,"ship");
99 pShip->setPos(*pPosition);
101 connect(pShip,SIGNAL(pickingGhost(QGraphicsItem*)),this, SLOT(removeGhost(QGraphicsItem*)) );
102 pShip->startMoving();
107 void SeaScene::spreadGhosts(int ghosts)
109 for (int i=0; i < ghosts; i++)
111 QPointF * pPosition = findRandomFreeSlot();
113 //If there was no room no point to continue
114 if (pPosition == NULL)
117 QGraphicsPixmapItem * pGhost = addPixmap(QPixmap(":/pix/aave.png"));
118 pGhost->setData(0,"ghost");
119 pGhost->setPos(*pPosition);
124 QPointF* SeaScene::findRandomFreeSlot()
126 if (freeTiles_.isEmpty())
129 int index = qrand()%freeTiles_.size();
131 qDebug() << index << " index";
132 return new QPointF (freeTiles_.takeAt(index));
136 void SeaScene::removeGhost(QGraphicsItem *pGhost)
138 removeItem(pGhost); //remove the item from scene
139 freeTiles_.append(pGhost->scenePos()); //add the item's position to free slots