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);
66 //spread the octopuses
69 for (int i=0; i < octopuses; i++)
71 QPointF * pPosition = findRandomFreeSlot();
73 //If there was no room no point to continue
74 if (pPosition == NULL)
77 TimerControlledTursas * pOctopus = new TimerControlledTursas (QPixmap(":/pix/tursas.png"),100);
78 pOctopus->setData(0,"octopus");
79 pOctopus->setPos(*pPosition);
81 pOctopus->startMoving();
89 QPointF * pPosition = findRandomFreeSlot();
90 if (pPosition == NULL)
92 // Game cannot begin without a free position for ship, so give an error message and return
94 QMessageBox::critical(NULL,"Error! Too many objects on screen","No free space to place the ship. The game cannot start. Please choose another level.");
98 Ship * pShip = new Ship (QPixmap(":/pix/laiva.png"));
99 pShip->setData(0,"ship");
100 pShip->setPos(*pPosition);
102 connect(pShip,SIGNAL(pickingGhost(QGraphicsItem*)),this, SLOT(removeGhost(QGraphicsItem*)) );
103 connect(pShip,SIGNAL(droppingGhosts(int)),this,SLOT(ghostsDropped(int)));
104 pShip->startMoving();
109 void SeaScene::spreadGhosts(int ghosts)
111 for (int i=0; i < ghosts; i++)
113 QPointF * pPosition = findRandomFreeSlot();
115 //If there was no room no point to continue
116 if (pPosition == NULL)
119 QGraphicsPixmapItem * pGhost = addPixmap(QPixmap(":/pix/aave.png"));
120 pGhost->setData(0,"ghost");
121 pGhost->setPos(*pPosition);
126 QPointF* SeaScene::findRandomFreeSlot()
128 if (freeTiles_.isEmpty())
131 int index = qrand()%freeTiles_.size();
133 qDebug() << index << " index";
134 return new QPointF (freeTiles_.takeAt(index));
138 void SeaScene::removeGhost(QGraphicsItem *pGhost)
140 removeItem(pGhost); //remove the item from scene
141 freeTiles_.append(pGhost->scenePos()); //add the item's position to free slots
144 if (ghostsLeft_ == 0)
146 //here whatever happens when a level is complete / a signal
147 qDebug() << "All ghosts picked!";
151 void SeaScene::ghostsDropped(int ghosts)
153 ghostsLeft_ += ghosts;
155 spreadGhosts(ghosts);