1 /**************************************************************************
2 Ghosts Overboard - a game for Maemo 5
4 Copyright (C) 2011 Heli Hyvättinen
6 This file is part of Ghosts Overboard
8 Ghosts Overboard is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 **************************************************************************/
26 #include <QGraphicsPixmapItem>
28 #include <QMessageBox>
31 const QString ghostImageFilename_ = ":/pix/aave.png";
32 const QString rockImageFilename_ =":/pix/kari.png";
33 const QString octopusImageFilename_= ":/pix/tursas.png";
36 SeaScene::SeaScene(QObject *parent) :
37 QGraphicsScene(parent)
40 screenLitKeeper_.keepScreenLit(true);
44 QPixmap waves (":/pix/meri.png");
46 setBackgroundBrush(QBrush(waves));
50 qsrand(QTime::currentTime().msec()+2); //+2 to avoid setting it to 1
56 void SeaScene::setupMap(int ghosts, int rocks, int octopuses, int octopusSpeed)
62 //empty the list of moving items
66 //empty the list of free slots
69 //fill the list of free slots
71 int numberOfXTiles = width() / 40;
72 int numberOfYTiles = height() /40;
74 // qDebug() << numberOfXTiles << " slots in x direction";
75 // qDebug() << numberOfYTiles << " slots in y rirection";
77 for (int i = 0; i < numberOfXTiles; i++ )
79 for (int j = 0; j < numberOfYTiles; j++)
81 freeTiles_.append(QPointF(i*40,j*40));
88 for (int i = 0; i < rocks; i++)
90 QPointF * pPosition = findRandomFreeSlot();
92 //If there was no room no point to continue
93 if (pPosition == NULL)
96 QPixmap rockPixmap (":/pix/kari.png");
97 QGraphicsPixmapItem * pRock = addPixmap(rockPixmap);
98 pRock->setData(0,"rock");
99 pRock->setPos(*pPosition);
106 ghostsLeft_ = ghosts;
107 spreadGhosts(ghosts);
111 //spread the octopuses
113 QList <Octopus*> octopusList;
115 for (int i=0; i < octopuses; i++)
117 QPointF * pPosition = findRandomFreeSlot();
119 //If there was no room no point to continue
120 if (pPosition == NULL)
123 QPixmap octopusPixmap (":/pix/tursas.png");
124 Octopus * pOctopus = new Octopus(octopusPixmap,octopusSpeed);
125 pOctopus->setData(0,"octopus");
126 pOctopus->setPos(*pPosition);
128 pOctopus->startMoving();
129 movingItems_.append(pOctopus);
130 connect(this,SIGNAL(pauseOn()),pOctopus,SLOT(stopMoving()));
131 connect(this,SIGNAL(pauseOff()),pOctopus,SLOT(startMoving()));
132 octopusList.append(pOctopus);
140 QPointF * pPosition = findRandomFreeSlot();
141 if (pPosition == NULL)
143 // Game cannot begin without a free position for ship, so give an error message and return
145 QMessageBox::critical(NULL,"Error! Too many objects on screen","No free space to place the ship. The game cannot start. Please choose another level.");
149 QList<QPixmap> shipImages;
150 shipImages.append(QPixmap(":/pix/laiva.png"));
151 shipImages.append(QPixmap(":/pix/laiva_1aave.png"));
152 shipImages.append(QPixmap(":/pix/laiva_2aave.png"));
153 shipImages.append(QPixmap(":/pix/laiva_3aave.png"));
154 shipImages.append(QPixmap(":/pix/laiva_4aave.png"));
155 shipImages.append(QPixmap(":/pix/laiva_5aave.png"));
156 shipImages.append(QPixmap(":/pix/laiva_6aave.png"));
157 shipImages.append(QPixmap(":/pix/laiva_7aave.png"));
158 shipImages.append(QPixmap(":/pix/laiva_8aave.png"));
159 shipImages.append(QPixmap(":/pix/laiva_9aave.png"));
160 shipImages.append(QPixmap(":/pix/laiva_10aave.png"));
162 Ship * pShip = new Ship (shipImages);
163 pShip->setData(0,"ship");
164 pShip->setPos(*pPosition);
166 connect(pShip,SIGNAL(pickingGhost(QGraphicsItem*)),this, SLOT(removeGhost(QGraphicsItem*)) );
167 connect(pShip,SIGNAL(droppingGhosts(int)),this,SLOT(ghostsDropped(int)));
168 connect(this,SIGNAL(vibrationActivated(bool)),pShip,SLOT(setVibrationActivate(bool)));
169 pShip->startMoving();
170 movingItems_.append(pShip);
171 connect(this,SIGNAL(pauseOn()),pShip,SLOT(stopMoving()));
172 connect(this,SIGNAL(pauseOff()),pShip,SLOT(startMoving()));
173 foreach (Octopus* pOctopus, octopusList)
175 connect(pOctopus,SIGNAL(droppingGhosts()),pShip,SLOT(dropAllGhosts()));
180 void SeaScene::setupMap(Level level)
182 setupMap(level.getNumberOfGhosts(),level.getNumberOfRocks(),level.getNumberOfOctopuses(),level.getOctopusSpeed());
185 void SeaScene::spreadGhosts(int ghosts)
189 //the octopuses and the ship may have moved from their original positions,
190 //so the list of free slots must be adjusted to exclude their current positions
192 QList<QPointF> temporarilyReservedSlots;
194 foreach (QGraphicsItem* pItem, movingItems_)
198 // qDebug() << "NULL item in movingItems_";
202 //round x and y down to fit the slot size
212 QPointF position (x,y);
214 //remove the tiles (potentially) occupied by the item from free slots and place in temp list if was in the list before
216 if (freeTiles_.removeOne(position))
217 temporarilyReservedSlots.append(position);
222 if (freeTiles_.removeOne(position))
223 temporarilyReservedSlots.append(position);
227 if (freeTiles_.removeOne(position))
228 temporarilyReservedSlots.append(position);
232 if (freeTiles_.removeOne(position))
233 temporarilyReservedSlots.append(position);
238 //spread ghosts in random free slots
240 for (int i=0; i < ghosts; i++)
242 QPointF * pPosition = findRandomFreeSlot();
244 //If there was no room no point to continue
245 if (pPosition == NULL)
248 QPixmap ghostPixmap(":/pix/aave.png");
249 QGraphicsPixmapItem * pGhost = addPixmap(ghostPixmap);
250 pGhost->setData(0,"ghost");
251 pGhost->setPos(*pPosition);
255 //return the slots occupied by moving items to free slots
256 freeTiles_.append(temporarilyReservedSlots);
258 //clear temp for the next round
259 temporarilyReservedSlots.clear();
262 QPointF* SeaScene::findRandomFreeSlot()
264 if (freeTiles_.isEmpty())
267 int index = qrand()%freeTiles_.size();
269 // qDebug() << index << " index";
270 return new QPointF (freeTiles_.takeAt(index));
274 void SeaScene::removeGhost(QGraphicsItem *pGhost)
276 removeItem(pGhost); //remove the item from scene
277 freeTiles_.append(pGhost->scenePos()); //add the item's position to free slots
280 if (ghostsLeft_ == 0)
282 emit allGhostsPicked();
283 // qDebug() << "All ghosts picked!";
287 void SeaScene::ghostsDropped(int ghosts)
289 ghostsLeft_ += ghosts;
291 spreadGhosts(ghosts);
294 void SeaScene::pause(bool paused)
296 // qDebug() << "pause pressed " << paused;
297 if (paused_ == paused)
304 // qDebug() << "starting to move again";
306 screenLitKeeper_.keepScreenLit(true);
311 // qDebug("about to stop movement");
313 screenLitKeeper_.keepScreenLit(false);
317 void SeaScene::vibrationActivate(bool on)
319 emit vibrationActivated(on);