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 QPixmap rockPixmap (":/pix/kari.png");
53 QGraphicsPixmapItem * pRock = addPixmap(rockPixmap);
54 pRock->setData(0,"rock");
55 pRock->setPos(*pPosition);
67 //spread the octopuses
70 for (int i=0; i < octopuses; i++)
72 QPointF * pPosition = findRandomFreeSlot();
74 //If there was no room no point to continue
75 if (pPosition == NULL)
78 QPixmap octopusPixmap (":/pix/tursas.png");
79 TimerControlledTursas * pOctopus = new TimerControlledTursas (octopusPixmap,100);
80 pOctopus->setData(0,"octopus");
81 pOctopus->setPos(*pPosition);
83 pOctopus->startMoving();
91 QPointF * pPosition = findRandomFreeSlot();
92 if (pPosition == NULL)
94 // Game cannot begin without a free position for ship, so give an error message and return
96 QMessageBox::critical(NULL,"Error! Too many objects on screen","No free space to place the ship. The game cannot start. Please choose another level.");
100 Ship * pShip = new Ship (QPixmap(":/pix/laiva.png"));
101 pShip->setData(0,"ship");
102 pShip->setPos(*pPosition);
104 connect(pShip,SIGNAL(pickingGhost(QGraphicsItem*)),this, SLOT(removeGhost(QGraphicsItem*)) );
105 connect(pShip,SIGNAL(droppingGhosts(int)),this,SLOT(ghostsDropped(int)));
106 pShip->startMoving();
111 void SeaScene::spreadGhosts(int ghosts)
113 for (int i=0; i < ghosts; i++)
115 QPointF * pPosition = findRandomFreeSlot();
117 //If there was no room no point to continue
118 if (pPosition == NULL)
121 QPixmap ghostPixmap(":/pix/aave.png");
122 QGraphicsPixmapItem * pGhost = addPixmap(ghostPixmap);
123 pGhost->setData(0,"ghost");
124 pGhost->setPos(*pPosition);
129 QPointF* SeaScene::findRandomFreeSlot()
131 if (freeTiles_.isEmpty())
134 int index = qrand()%freeTiles_.size();
136 qDebug() << index << " index";
137 return new QPointF (freeTiles_.takeAt(index));
141 void SeaScene::removeGhost(QGraphicsItem *pGhost)
143 removeItem(pGhost); //remove the item from scene
144 freeTiles_.append(pGhost->scenePos()); //add the item's position to free slots
147 if (ghostsLeft_ == 0)
149 //here whatever happens when a level is complete / a signal
150 qDebug() << "All ghosts picked!";
154 void SeaScene::ghostsDropped(int ghosts)
156 ghostsLeft_ += ghosts;
158 spreadGhosts(ghosts);