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)
21 QPixmap waves (":/pix/meri.png");
23 setBackgroundBrush(QBrush(waves));
27 qsrand(QTime::currentTime().msec()+2); //+2 to avoid setting it to 1
31 void SeaScene::setupMap(int ghosts, int rocks, int octopuses)
37 //empty the list of moving items
41 //empty the list of free slots
44 //fill the list of free slots
46 int numberOfXTiles = width() / 40;
47 int numberOfYTiles = height() /40;
49 qDebug() << numberOfXTiles << " slots in x direction";
50 qDebug() << numberOfYTiles << " slots in y rirection";
52 for (int i = 0; i < numberOfXTiles; i++ )
54 for (int j = 0; j < numberOfYTiles; j++)
56 freeTiles_.append(QPointF(i*40,j*40));
63 for (int i = 0; i < rocks; i++)
65 QPointF * pPosition = findRandomFreeSlot();
67 qDebug() << "Found a place for a rock";
69 //If there was no room no point to continue
70 if (pPosition == NULL)
73 QPixmap rockPixmap (":/pix/kari.png");
74 QGraphicsPixmapItem * pRock = addPixmap(rockPixmap);
75 pRock->setData(0,"rock");
76 pRock->setPos(*pPosition);
88 //spread the octopuses
91 for (int i=0; i < octopuses; i++)
93 QPointF * pPosition = findRandomFreeSlot();
95 //If there was no room no point to continue
96 if (pPosition == NULL)
99 QPixmap octopusPixmap (":/pix/tursas.png");
100 TimerControlledTursas * pOctopus = new TimerControlledTursas (octopusPixmap,100);
101 pOctopus->setData(0,"octopus");
102 pOctopus->setPos(*pPosition);
104 pOctopus->startMoving();
105 movingItems_.append(pOctopus);
106 connect(this,SIGNAL(pauseOn()),pOctopus,SLOT(stopMoving()));
107 connect(this,SIGNAL(pauseOff()),pOctopus,SLOT(startMoving()));
115 QPointF * pPosition = findRandomFreeSlot();
116 if (pPosition == NULL)
118 // Game cannot begin without a free position for ship, so give an error message and return
120 QMessageBox::critical(NULL,"Error! Too many objects on screen","No free space to place the ship. The game cannot start. Please choose another level.");
124 QList<QPixmap> shipImages;
125 shipImages.append(QPixmap(":/pix/laiva.png"));
126 shipImages.append(QPixmap(":/pix/laiva_1aave.png"));
127 shipImages.append(QPixmap(":/pix/laiva_2aave.png"));
128 shipImages.append(QPixmap(":/pix/laiva_3aave.png"));
129 shipImages.append(QPixmap(":/pix/laiva_4aave.png"));
130 shipImages.append(QPixmap(":/pix/laiva_5aave.png"));
131 shipImages.append(QPixmap(":/pix/laiva_6aave.png"));
132 shipImages.append(QPixmap(":/pix/laiva_7aave.png"));
133 shipImages.append(QPixmap(":/pix/laiva_8aave.png"));
134 shipImages.append(QPixmap(":/pix/laiva_9aave.png"));
135 shipImages.append(QPixmap(":/pix/laiva_10aave.png"));
137 Ship * pShip = new Ship (shipImages);
138 pShip->setData(0,"ship");
139 pShip->setPos(*pPosition);
141 connect(pShip,SIGNAL(pickingGhost(QGraphicsItem*)),this, SLOT(removeGhost(QGraphicsItem*)) );
142 connect(pShip,SIGNAL(droppingGhosts(int)),this,SLOT(ghostsDropped(int)));
143 pShip->startMoving();
144 movingItems_.append(pShip);
145 connect(this,SIGNAL(pauseOn()),pShip,SLOT(stopMoving()));
146 connect(this,SIGNAL(pauseOff()),pShip,SLOT(startMoving()));
151 void SeaScene::spreadGhosts(int ghosts)
155 //the octopuses and the ship may have moved from their original positions,
156 //so the list of free slots must be adjusted to exclude their current positions
158 QList<QPointF> temporarilyReservedSlots;
160 foreach (QGraphicsItem* pItem, movingItems_)
164 // qDebug() << "NULL item in movingItems_";
168 //round x and y down to fit the slot size
178 QPointF position (x,y);
180 //remove the tiles (potentially) occupied by the item from free slots and place in temp list if was in the list before
182 if (freeTiles_.removeOne(position))
183 temporarilyReservedSlots.append(position);
188 if (freeTiles_.removeOne(position))
189 temporarilyReservedSlots.append(position);
193 if (freeTiles_.removeOne(position))
194 temporarilyReservedSlots.append(position);
198 if (freeTiles_.removeOne(position))
199 temporarilyReservedSlots.append(position);
204 //spread ghosts in random free slots
206 for (int i=0; i < ghosts; i++)
208 QPointF * pPosition = findRandomFreeSlot();
210 //If there was no room no point to continue
211 if (pPosition == NULL)
214 QPixmap ghostPixmap(":/pix/aave.png");
215 QGraphicsPixmapItem * pGhost = addPixmap(ghostPixmap);
216 pGhost->setData(0,"ghost");
217 pGhost->setPos(*pPosition);
221 //return the slots occupied by moving items to free slots
222 freeTiles_.append(temporarilyReservedSlots);
224 //clear temp for the next round
225 temporarilyReservedSlots.clear();
228 QPointF* SeaScene::findRandomFreeSlot()
230 if (freeTiles_.isEmpty())
233 int index = qrand()%freeTiles_.size();
235 qDebug() << index << " index";
236 return new QPointF (freeTiles_.takeAt(index));
240 void SeaScene::removeGhost(QGraphicsItem *pGhost)
242 removeItem(pGhost); //remove the item from scene
243 freeTiles_.append(pGhost->scenePos()); //add the item's position to free slots
246 if (ghostsLeft_ == 0)
248 emit allGhostsPicked();
249 qDebug() << "All ghosts picked!";
253 void SeaScene::ghostsDropped(int ghosts)
255 ghostsLeft_ += ghosts;
257 spreadGhosts(ghosts);
260 void SeaScene::pause(bool paused)
262 // qDebug() << "pause pressed " << paused;
263 if (paused_ == paused)
270 // qDebug() << "starting to move again";
276 // qDebug("about to stop movement");