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 //If there was no room no point to continue
71 if (pPosition == NULL)
74 QPixmap rockPixmap (":/pix/kari.png");
75 QGraphicsPixmapItem * pRock = addPixmap(rockPixmap);
76 pRock->setData(0,"rock");
77 pRock->setPos(*pPosition);
89 //spread the octopuses
91 QList<Octopus*> octopuses;
93 for (int i=0; i < octopuses; i++)
95 QPointF * pPosition = findRandomFreeSlot();
97 //If there was no room no point to continue
98 if (pPosition == NULL)
101 QPixmap octopusPixmap (":/pix/tursas.png");
102 Octopus * pOctopus = new Octopus(octopusPixmap,100);
103 pOctopus->setData(0,"octopus");
104 pOctopus->setPos(*pPosition);
106 pOctopus->startMoving();
107 movingItems_.append(pOctopus);
108 connect(this,SIGNAL(pauseOn()),pOctopus,SLOT(stopMoving()));
109 connect(this,SIGNAL(pauseOff()),pOctopus,SLOT(startMoving()));
110 octopuses.append(pOctopus);
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()));
150 foreach (Octopus* pOctopus, octopuses)
152 connect(pOctopus,SIGNAL(droppingGhosts()),pShip,SLOT(dropAllGhosts()));
158 void SeaScene::spreadGhosts(int ghosts)
162 //the octopuses and the ship may have moved from their original positions,
163 //so the list of free slots must be adjusted to exclude their current positions
165 QList<QPointF> temporarilyReservedSlots;
167 foreach (QGraphicsItem* pItem, movingItems_)
171 // qDebug() << "NULL item in movingItems_";
175 //round x and y down to fit the slot size
185 QPointF position (x,y);
187 //remove the tiles (potentially) occupied by the item from free slots and place in temp list if was in the list before
189 if (freeTiles_.removeOne(position))
190 temporarilyReservedSlots.append(position);
195 if (freeTiles_.removeOne(position))
196 temporarilyReservedSlots.append(position);
200 if (freeTiles_.removeOne(position))
201 temporarilyReservedSlots.append(position);
205 if (freeTiles_.removeOne(position))
206 temporarilyReservedSlots.append(position);
211 //spread ghosts in random free slots
213 for (int i=0; i < ghosts; i++)
215 QPointF * pPosition = findRandomFreeSlot();
217 //If there was no room no point to continue
218 if (pPosition == NULL)
221 QPixmap ghostPixmap(":/pix/aave.png");
222 QGraphicsPixmapItem * pGhost = addPixmap(ghostPixmap);
223 pGhost->setData(0,"ghost");
224 pGhost->setPos(*pPosition);
228 //return the slots occupied by moving items to free slots
229 freeTiles_.append(temporarilyReservedSlots);
231 //clear temp for the next round
232 temporarilyReservedSlots.clear();
235 QPointF* SeaScene::findRandomFreeSlot()
237 if (freeTiles_.isEmpty())
240 int index = qrand()%freeTiles_.size();
242 // qDebug() << index << " index";
243 return new QPointF (freeTiles_.takeAt(index));
247 void SeaScene::removeGhost(QGraphicsItem *pGhost)
249 removeItem(pGhost); //remove the item from scene
250 freeTiles_.append(pGhost->scenePos()); //add the item's position to free slots
253 if (ghostsLeft_ == 0)
255 emit allGhostsPicked();
256 // qDebug() << "All ghosts picked!";
260 void SeaScene::ghostsDropped(int ghosts)
262 ghostsLeft_ += ghosts;
264 spreadGhosts(ghosts);
267 void SeaScene::pause(bool paused)
269 // qDebug() << "pause pressed " << paused;
270 if (paused_ == paused)
277 // qDebug() << "starting to move again";
279 screenLitKeeper_.keepScreenLit(true);
284 // qDebug("about to stop movement");
286 screenLitKeeper_.keepScreenLit(false);