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 QList<QPixmap> shipImages;
121 shipImages.append(QPixmap(":/pix/laiva.png"));
122 shipImages.append(QPixmap(":/pix/laiva_1aave.png"));
123 shipImages.append(QPixmap(":/pix/laiva_2aave.png"));
124 shipImages.append(QPixmap(":/pix/laiva_3aave.png"));
125 shipImages.append(QPixmap(":/pix/laiva_4aave.png"));
126 shipImages.append(QPixmap(":/pix/laiva_5aave.png"));
127 shipImages.append(QPixmap(":/pix/laiva_6aave.png"));
128 shipImages.append(QPixmap(":/pix/laiva_7aave.png"));
129 shipImages.append(QPixmap(":/pix/laiva_8aave.png"));
130 shipImages.append(QPixmap(":/pix/laiva_9aave.png"));
131 shipImages.append(QPixmap(":/pix/laiva_10aave.png"));
133 Ship * pShip = new Ship (shipImages);
134 pShip->setData(0,"ship");
135 pShip->setPos(*pPosition);
137 connect(pShip,SIGNAL(pickingGhost(QGraphicsItem*)),this, SLOT(removeGhost(QGraphicsItem*)) );
138 connect(pShip,SIGNAL(droppingGhosts(int)),this,SLOT(ghostsDropped(int)));
139 pShip->startMoving();
140 movingItems_.append(pShip);
145 void SeaScene::spreadGhosts(int ghosts)
149 //the octopuses and the ship may have moved from their original positions,
150 //so the list of free slots must be adjusted to exclude their current positions
152 QList<QPointF> temporarilyReservedSlots;
154 foreach (QGraphicsItem* pItem, movingItems_)
158 // qDebug() << "NULL item in movingItems_";
162 //round x and y down to fit the slot size
172 QPointF position (x,y);
174 //remove the tiles (potentially) occupied by the item from free slots and place in temp list if was in the list before
176 if (freeTiles_.removeOne(position))
177 temporarilyReservedSlots.append(position);
182 if (freeTiles_.removeOne(position))
183 temporarilyReservedSlots.append(position);
187 if (freeTiles_.removeOne(position))
188 temporarilyReservedSlots.append(position);
192 if (freeTiles_.removeOne(position))
193 temporarilyReservedSlots.append(position);
198 //spread ghosts in random free slots
200 for (int i=0; i < ghosts; i++)
202 QPointF * pPosition = findRandomFreeSlot();
204 //If there was no room no point to continue
205 if (pPosition == NULL)
208 QPixmap ghostPixmap(":/pix/aave.png");
209 QGraphicsPixmapItem * pGhost = addPixmap(ghostPixmap);
210 pGhost->setData(0,"ghost");
211 pGhost->setPos(*pPosition);
215 //return the slots occupied by moving items to free slots
216 freeTiles_.append(temporarilyReservedSlots);
218 //clear temp for the next round
219 temporarilyReservedSlots.clear();
222 QPointF* SeaScene::findRandomFreeSlot()
224 if (freeTiles_.isEmpty())
227 int index = qrand()%freeTiles_.size();
229 qDebug() << index << " index";
230 return new QPointF (freeTiles_.takeAt(index));
234 void SeaScene::removeGhost(QGraphicsItem *pGhost)
236 removeItem(pGhost); //remove the item from scene
237 freeTiles_.append(pGhost->scenePos()); //add the item's position to free slots
240 if (ghostsLeft_ == 0)
242 emit allGhostsPicked();
243 qDebug() << "All ghosts picked!";
247 void SeaScene::ghostsDropped(int ghosts)
249 ghostsLeft_ += ghosts;
251 spreadGhosts(ghosts);