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)
26 //empty the list of moving items
30 //empty the list of free slots
33 //fill the list of free slots
35 int numberOfXTiles = width() / 40;
36 int numberOfYTiles = height() /40;
38 qDebug() << numberOfXTiles << " slots in x direction";
39 qDebug() << numberOfYTiles << " slots in y rirection";
41 for (int i = 0; i < numberOfXTiles; i++ )
43 for (int j = 0; j < numberOfYTiles; j++)
45 freeTiles_.append(QPointF(i*40,j*40));
52 for (int i = 0; i < rocks; i++)
54 QPointF * pPosition = findRandomFreeSlot();
56 qDebug() << "Found a place for a rock";
58 //If there was no room no point to continue
59 if (pPosition == NULL)
62 QPixmap rockPixmap (":/pix/kari.png");
63 QGraphicsPixmapItem * pRock = addPixmap(rockPixmap);
64 pRock->setData(0,"rock");
65 pRock->setPos(*pPosition);
77 //spread the octopuses
80 for (int i=0; i < octopuses; i++)
82 QPointF * pPosition = findRandomFreeSlot();
84 //If there was no room no point to continue
85 if (pPosition == NULL)
88 QPixmap octopusPixmap (":/pix/tursas.png");
89 TimerControlledTursas * pOctopus = new TimerControlledTursas (octopusPixmap,100);
90 pOctopus->setData(0,"octopus");
91 pOctopus->setPos(*pPosition);
93 pOctopus->startMoving();
94 movingItems_.append(pOctopus);
102 QPointF * pPosition = findRandomFreeSlot();
103 if (pPosition == NULL)
105 // Game cannot begin without a free position for ship, so give an error message and return
107 QMessageBox::critical(NULL,"Error! Too many objects on screen","No free space to place the ship. The game cannot start. Please choose another level.");
111 Ship * pShip = new Ship (QPixmap(":/pix/laiva.png"));
112 pShip->setData(0,"ship");
113 pShip->setPos(*pPosition);
115 connect(pShip,SIGNAL(pickingGhost(QGraphicsItem*)),this, SLOT(removeGhost(QGraphicsItem*)) );
116 connect(pShip,SIGNAL(droppingGhosts(int)),this,SLOT(ghostsDropped(int)));
117 pShip->startMoving();
118 movingItems_.append(pShip);
123 void SeaScene::spreadGhosts(int ghosts)
126 qDebug() << "Preparing to spread ghosts";
128 //the octopuses and the ship may have moved from their original positions,
129 //so the list of free slots must be adjusted to exclude their current positions
131 QList<QPointF> temporarilyReservedSlots;
133 foreach (QGraphicsItem* pItem, movingItems_)
137 // qDebug() << "NULL item in movingItems_";
141 //round x and y down to fit the slot size
151 QPointF position (x,y);
153 //remove the tiles (potentially) occupied by the item from free slots and place in temp list if was in the list before
155 if (freeTiles_.removeOne(position))
156 temporarilyReservedSlots.append(position);
161 if (freeTiles_.removeOne(position))
162 temporarilyReservedSlots.append(position);
166 if (freeTiles_.removeOne(position))
167 temporarilyReservedSlots.append(position);
171 if (freeTiles_.removeOne(position))
172 temporarilyReservedSlots.append(position);
177 //spread ghosts in random free slots
179 for (int i=0; i < ghosts; i++)
181 QPointF * pPosition = findRandomFreeSlot();
183 //If there was no room no point to continue
184 if (pPosition == NULL)
187 QPixmap ghostPixmap(":/pix/aave.png");
188 QGraphicsPixmapItem * pGhost = addPixmap(ghostPixmap);
189 pGhost->setData(0,"ghost");
190 pGhost->setPos(*pPosition);
194 //return the slots occupied by moving items to free slots
195 freeTiles_.append(temporarilyReservedSlots);
197 //clear temp for the next round
198 temporarilyReservedSlots.clear();
201 QPointF* SeaScene::findRandomFreeSlot()
203 if (freeTiles_.isEmpty())
206 int index = qrand()%freeTiles_.size();
208 qDebug() << index << " index";
209 return new QPointF (freeTiles_.takeAt(index));
213 void SeaScene::removeGhost(QGraphicsItem *pGhost)
215 removeItem(pGhost); //remove the item from scene
216 freeTiles_.append(pGhost->scenePos()); //add the item's position to free slots
219 if (ghostsLeft_ == 0)
221 //here whatever happens when a level is complete / a signal
222 qDebug() << "All ghosts picked!";
226 void SeaScene::ghostsDropped(int ghosts)
228 ghostsLeft_ += ghosts;
230 spreadGhosts(ghosts);