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)
136 //the octopuses and the ship may have moved from their original positions,
137 //so the list of free slots must be adjusted to exclude their current positions
139 QList<QPointF> temporarilyReservedSlots;
141 foreach (QGraphicsItem* pItem, movingItems_)
145 // qDebug() << "NULL item in movingItems_";
149 //round x and y down to fit the slot size
159 QPointF position (x,y);
161 //remove the tiles (potentially) occupied by the item from free slots and place in temp list if was in the list before
163 if (freeTiles_.removeOne(position))
164 temporarilyReservedSlots.append(position);
169 if (freeTiles_.removeOne(position))
170 temporarilyReservedSlots.append(position);
174 if (freeTiles_.removeOne(position))
175 temporarilyReservedSlots.append(position);
179 if (freeTiles_.removeOne(position))
180 temporarilyReservedSlots.append(position);
185 //spread ghosts in random free slots
187 for (int i=0; i < ghosts; i++)
189 QPointF * pPosition = findRandomFreeSlot();
191 //If there was no room no point to continue
192 if (pPosition == NULL)
195 QPixmap ghostPixmap(":/pix/aave.png");
196 QGraphicsPixmapItem * pGhost = addPixmap(ghostPixmap);
197 pGhost->setData(0,"ghost");
198 pGhost->setPos(*pPosition);
202 //return the slots occupied by moving items to free slots
203 freeTiles_.append(temporarilyReservedSlots);
205 //clear temp for the next round
206 temporarilyReservedSlots.clear();
209 QPointF* SeaScene::findRandomFreeSlot()
211 if (freeTiles_.isEmpty())
214 int index = qrand()%freeTiles_.size();
216 qDebug() << index << " index";
217 return new QPointF (freeTiles_.takeAt(index));
221 void SeaScene::removeGhost(QGraphicsItem *pGhost)
223 removeItem(pGhost); //remove the item from scene
224 freeTiles_.append(pGhost->scenePos()); //add the item's position to free slots
227 if (ghostsLeft_ == 0)
229 //here whatever happens when a level is complete / a signal
230 qDebug() << "All ghosts picked!";
234 void SeaScene::ghostsDropped(int ghosts)
236 ghostsLeft_ += ghosts;
238 spreadGhosts(ghosts);