2 #include "timercontrolledtursas.h"
4 #include <QGraphicsPixmapItem>
9 const QString ghostImageFilename_ = ":/pix/aave.png";
10 const QString rockImageFilename_ =":/pix/kari.png";
11 const QString octopusImageFilename_= ":/pix/tursas.png";
14 SeaScene::SeaScene(QObject *parent) :
15 QGraphicsScene(parent)
19 QPixmap waves (":/pix/meri.png");
21 setBackgroundBrush(QBrush(waves));
25 qsrand(QTime::currentTime().msec()+2); //+2 to avoid setting it to 1
29 void SeaScene::setupMap(int ghosts, int rocks, int octopuses)
35 //empty the list of moving items
39 //empty the list of free slots
42 //fill the list of free slots
44 int numberOfXTiles = width() / 40;
45 int numberOfYTiles = height() /40;
47 qDebug() << numberOfXTiles << " slots in x direction";
48 qDebug() << numberOfYTiles << " slots in y rirection";
50 for (int i = 0; i < numberOfXTiles; i++ )
52 for (int j = 0; j < numberOfYTiles; j++)
54 freeTiles_.append(QPointF(i*40,j*40));
61 for (int i = 0; i < rocks; i++)
63 QPointF * pPosition = findRandomFreeSlot();
65 qDebug() << "Found a place for a rock";
67 //If there was no room no point to continue
68 if (pPosition == NULL)
71 QPixmap rockPixmap (":/pix/kari.png");
72 QGraphicsPixmapItem * pRock = addPixmap(rockPixmap);
73 pRock->setData(0,"rock");
74 pRock->setPos(*pPosition);
86 //spread the octopuses
89 for (int i=0; i < octopuses; i++)
91 QPointF * pPosition = findRandomFreeSlot();
93 //If there was no room no point to continue
94 if (pPosition == NULL)
97 QPixmap octopusPixmap (":/pix/tursas.png");
98 TimerControlledTursas * pOctopus = new TimerControlledTursas (octopusPixmap,100);
99 pOctopus->setData(0,"octopus");
100 pOctopus->setPos(*pPosition);
102 pOctopus->startMoving();
103 movingItems_.append(pOctopus);
111 QPointF * pPosition = findRandomFreeSlot();
112 if (pPosition == NULL)
114 // Game cannot begin without a free position for ship, so give an error message and return
116 QMessageBox::critical(NULL,"Error! Too many objects on screen","No free space to place the ship. The game cannot start. Please choose another level.");
120 Ship * pShip = new Ship (QPixmap(":/pix/laiva.png"));
121 pShip->setData(0,"ship");
122 pShip->setPos(*pPosition);
124 connect(pShip,SIGNAL(pickingGhost(QGraphicsItem*)),this, SLOT(removeGhost(QGraphicsItem*)) );
125 connect(pShip,SIGNAL(droppingGhosts(int)),this,SLOT(ghostsDropped(int)));
126 pShip->startMoving();
127 movingItems_.append(pShip);
132 void SeaScene::spreadGhosts(int ghosts)
135 qDebug() << "Preparing to spread ghosts";
137 //the octopuses and the ship may have moved from their original positions,
138 //so the list of free slots must be adjusted to exclude their current positions
140 QList<QPointF> temporarilyReservedSlots;
142 foreach (QGraphicsItem* pItem, movingItems_)
146 // qDebug() << "NULL item in movingItems_";
150 //round x and y down to fit the slot size
160 QPointF position (x,y);
162 //remove the tiles (potentially) occupied by the item from free slots and place in temp list if was in the list before
164 if (freeTiles_.removeOne(position))
165 temporarilyReservedSlots.append(position);
170 if (freeTiles_.removeOne(position))
171 temporarilyReservedSlots.append(position);
175 if (freeTiles_.removeOne(position))
176 temporarilyReservedSlots.append(position);
180 if (freeTiles_.removeOne(position))
181 temporarilyReservedSlots.append(position);
186 //spread ghosts in random free slots
188 for (int i=0; i < ghosts; i++)
190 QPointF * pPosition = findRandomFreeSlot();
192 //If there was no room no point to continue
193 if (pPosition == NULL)
196 QPixmap ghostPixmap(":/pix/aave.png");
197 QGraphicsPixmapItem * pGhost = addPixmap(ghostPixmap);
198 pGhost->setData(0,"ghost");
199 pGhost->setPos(*pPosition);
203 //return the slots occupied by moving items to free slots
204 freeTiles_.append(temporarilyReservedSlots);
206 //clear temp for the next round
207 temporarilyReservedSlots.clear();
210 QPointF* SeaScene::findRandomFreeSlot()
212 if (freeTiles_.isEmpty())
215 int index = qrand()%freeTiles_.size();
217 qDebug() << index << " index";
218 return new QPointF (freeTiles_.takeAt(index));
222 void SeaScene::removeGhost(QGraphicsItem *pGhost)
224 removeItem(pGhost); //remove the item from scene
225 freeTiles_.append(pGhost->scenePos()); //add the item's position to free slots
228 if (ghostsLeft_ == 0)
230 //here whatever happens when a level is complete / a signal
231 qDebug() << "All ghosts picked!";
235 void SeaScene::ghostsDropped(int ghosts)
237 ghostsLeft_ += ghosts;
239 spreadGhosts(ghosts);