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)
18 screenLitKeeper_.keepScreenLit(true);
22 QPixmap waves (":/pix/meri.png");
24 setBackgroundBrush(QBrush(waves));
28 qsrand(QTime::currentTime().msec()+2); //+2 to avoid setting it to 1
34 void SeaScene::setupMap(int ghosts, int rocks, int octopuses)
40 //empty the list of moving items
44 //empty the list of free slots
47 //fill the list of free slots
49 int numberOfXTiles = width() / 40;
50 int numberOfYTiles = height() /40;
52 qDebug() << numberOfXTiles << " slots in x direction";
53 qDebug() << numberOfYTiles << " slots in y rirection";
55 for (int i = 0; i < numberOfXTiles; i++ )
57 for (int j = 0; j < numberOfYTiles; j++)
59 freeTiles_.append(QPointF(i*40,j*40));
66 for (int i = 0; i < rocks; i++)
68 QPointF * pPosition = findRandomFreeSlot();
70 qDebug() << "Found a place for a rock";
72 //If there was no room no point to continue
73 if (pPosition == NULL)
76 QPixmap rockPixmap (":/pix/kari.png");
77 QGraphicsPixmapItem * pRock = addPixmap(rockPixmap);
78 pRock->setData(0,"rock");
79 pRock->setPos(*pPosition);
91 //spread the octopuses
94 for (int i=0; i < octopuses; i++)
96 QPointF * pPosition = findRandomFreeSlot();
98 //If there was no room no point to continue
99 if (pPosition == NULL)
102 QPixmap octopusPixmap (":/pix/tursas.png");
103 TimerControlledTursas * pOctopus = new TimerControlledTursas (octopusPixmap,100);
104 pOctopus->setData(0,"octopus");
105 pOctopus->setPos(*pPosition);
107 pOctopus->startMoving();
108 movingItems_.append(pOctopus);
109 connect(this,SIGNAL(pauseOn()),pOctopus,SLOT(stopMoving()));
110 connect(this,SIGNAL(pauseOff()),pOctopus,SLOT(startMoving()));
118 QPointF * pPosition = findRandomFreeSlot();
119 if (pPosition == NULL)
121 // Game cannot begin without a free position for ship, so give an error message and return
123 QMessageBox::critical(NULL,"Error! Too many objects on screen","No free space to place the ship. The game cannot start. Please choose another level.");
127 QList<QPixmap> shipImages;
128 shipImages.append(QPixmap(":/pix/laiva.png"));
129 shipImages.append(QPixmap(":/pix/laiva_1aave.png"));
130 shipImages.append(QPixmap(":/pix/laiva_2aave.png"));
131 shipImages.append(QPixmap(":/pix/laiva_3aave.png"));
132 shipImages.append(QPixmap(":/pix/laiva_4aave.png"));
133 shipImages.append(QPixmap(":/pix/laiva_5aave.png"));
134 shipImages.append(QPixmap(":/pix/laiva_6aave.png"));
135 shipImages.append(QPixmap(":/pix/laiva_7aave.png"));
136 shipImages.append(QPixmap(":/pix/laiva_8aave.png"));
137 shipImages.append(QPixmap(":/pix/laiva_9aave.png"));
138 shipImages.append(QPixmap(":/pix/laiva_10aave.png"));
140 Ship * pShip = new Ship (shipImages);
141 pShip->setData(0,"ship");
142 pShip->setPos(*pPosition);
144 connect(pShip,SIGNAL(pickingGhost(QGraphicsItem*)),this, SLOT(removeGhost(QGraphicsItem*)) );
145 connect(pShip,SIGNAL(droppingGhosts(int)),this,SLOT(ghostsDropped(int)));
146 pShip->startMoving();
147 movingItems_.append(pShip);
148 connect(this,SIGNAL(pauseOn()),pShip,SLOT(stopMoving()));
149 connect(this,SIGNAL(pauseOff()),pShip,SLOT(startMoving()));
154 void SeaScene::spreadGhosts(int ghosts)
158 //the octopuses and the ship may have moved from their original positions,
159 //so the list of free slots must be adjusted to exclude their current positions
161 QList<QPointF> temporarilyReservedSlots;
163 foreach (QGraphicsItem* pItem, movingItems_)
167 // qDebug() << "NULL item in movingItems_";
171 //round x and y down to fit the slot size
181 QPointF position (x,y);
183 //remove the tiles (potentially) occupied by the item from free slots and place in temp list if was in the list before
185 if (freeTiles_.removeOne(position))
186 temporarilyReservedSlots.append(position);
191 if (freeTiles_.removeOne(position))
192 temporarilyReservedSlots.append(position);
196 if (freeTiles_.removeOne(position))
197 temporarilyReservedSlots.append(position);
201 if (freeTiles_.removeOne(position))
202 temporarilyReservedSlots.append(position);
207 //spread ghosts in random free slots
209 for (int i=0; i < ghosts; i++)
211 QPointF * pPosition = findRandomFreeSlot();
213 //If there was no room no point to continue
214 if (pPosition == NULL)
217 QPixmap ghostPixmap(":/pix/aave.png");
218 QGraphicsPixmapItem * pGhost = addPixmap(ghostPixmap);
219 pGhost->setData(0,"ghost");
220 pGhost->setPos(*pPosition);
224 //return the slots occupied by moving items to free slots
225 freeTiles_.append(temporarilyReservedSlots);
227 //clear temp for the next round
228 temporarilyReservedSlots.clear();
231 QPointF* SeaScene::findRandomFreeSlot()
233 if (freeTiles_.isEmpty())
236 int index = qrand()%freeTiles_.size();
238 qDebug() << index << " index";
239 return new QPointF (freeTiles_.takeAt(index));
243 void SeaScene::removeGhost(QGraphicsItem *pGhost)
245 removeItem(pGhost); //remove the item from scene
246 freeTiles_.append(pGhost->scenePos()); //add the item's position to free slots
249 if (ghostsLeft_ == 0)
251 emit allGhostsPicked();
252 qDebug() << "All ghosts picked!";
256 void SeaScene::ghostsDropped(int ghosts)
258 ghostsLeft_ += ghosts;
260 spreadGhosts(ghosts);
263 void SeaScene::pause(bool paused)
265 // qDebug() << "pause pressed " << paused;
266 if (paused_ == paused)
273 // qDebug() << "starting to move again";
275 screenLitKeeper_.keepScreenLit(true);
280 // qDebug("about to stop movement");
282 screenLitKeeper_.keepScreenLit(false);