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
52 //connect selecting to menu handling (only menu items selectable)
54 connect(this,SIGNAL(selectionChanged()),this,SLOT(menuClicked()));
60 void SeaScene::setupMap(int ghosts, int rocks, int octopuses, int octopusSpeed)
66 //empty the list of moving items
70 //empty the list of free slots
73 //fill the list of free slots
75 int numberOfXTiles = width() / 40;
76 int numberOfYTiles = height() /40;
78 // qDebug() << numberOfXTiles << " slots in x direction";
79 // qDebug() << numberOfYTiles << " slots in y rirection";
81 for (int i = 0; i < numberOfXTiles; i++ )
83 for (int j = 0; j < numberOfYTiles; j++)
85 freeTiles_.append(QPointF(i*40,j*40));
92 for (int i = 0; i < rocks; i++)
94 QPointF * pPosition = findRandomFreeSlot();
96 //If there was no room no point to continue
97 if (pPosition == NULL)
100 QPixmap rockPixmap (":/pix/kari.png");
101 QGraphicsPixmapItem * pRock = addPixmap(rockPixmap);
102 pRock->setData(0,"rock");
103 pRock->setPos(*pPosition);
110 ghostsLeft_ = ghosts;
111 spreadGhosts(ghosts);
115 //spread the octopuses
117 QList <Octopus*> octopusList;
119 for (int i=0; i < octopuses; i++)
121 QPointF * pPosition = findRandomFreeSlot();
123 //If there was no room no point to continue
124 if (pPosition == NULL)
127 QPixmap octopusPixmap (":/pix/tursas.png");
128 Octopus * pOctopus = new Octopus(octopusPixmap,octopusSpeed);
129 pOctopus->setData(0,"octopus");
130 pOctopus->setPos(*pPosition);
132 pOctopus->startMoving();
133 movingItems_.append(pOctopus);
134 connect(this,SIGNAL(pauseOn()),pOctopus,SLOT(stopMoving()));
135 connect(this,SIGNAL(pauseOff()),pOctopus,SLOT(startMoving()));
136 octopusList.append(pOctopus);
144 QPointF * pPosition = findRandomFreeSlot();
145 if (pPosition == NULL)
147 // Game cannot begin without a free position for ship, so give an error message and return
149 QMessageBox::critical(NULL,"Error! Too many objects on screen","No free space to place the ship. The game cannot start. Please choose another level.");
153 QList<QPixmap> shipImages;
154 shipImages.append(QPixmap(":/pix/laiva.png"));
155 shipImages.append(QPixmap(":/pix/laiva_1aave.png"));
156 shipImages.append(QPixmap(":/pix/laiva_2aave.png"));
157 shipImages.append(QPixmap(":/pix/laiva_3aave.png"));
158 shipImages.append(QPixmap(":/pix/laiva_4aave.png"));
159 shipImages.append(QPixmap(":/pix/laiva_5aave.png"));
160 shipImages.append(QPixmap(":/pix/laiva_6aave.png"));
161 shipImages.append(QPixmap(":/pix/laiva_7aave.png"));
162 shipImages.append(QPixmap(":/pix/laiva_8aave.png"));
163 shipImages.append(QPixmap(":/pix/laiva_9aave.png"));
164 shipImages.append(QPixmap(":/pix/laiva_10aave.png"));
166 Ship * pShip = new Ship (shipImages);
167 pShip->setData(0,"ship");
168 pShip->setPos(*pPosition);
170 connect(pShip,SIGNAL(pickingGhost(QGraphicsItem*)),this, SLOT(removeGhost(QGraphicsItem*)) );
171 connect(pShip,SIGNAL(droppingGhosts(int)),this,SLOT(ghostsDropped(int)));
172 connect(this,SIGNAL(vibrationActivated(bool)),pShip,SLOT(setVibrationActivate(bool)));
173 pShip->startMoving();
174 movingItems_.append(pShip);
175 connect(this,SIGNAL(pauseOn()),pShip,SLOT(stopMoving()));
176 connect(this,SIGNAL(pauseOff()),pShip,SLOT(startMoving()));
177 foreach (Octopus* pOctopus, octopusList)
179 connect(pOctopus,SIGNAL(droppingGhosts()),pShip,SLOT(dropAllGhosts()));
184 void SeaScene::setupMap(Level level)
186 setupMap(level.getNumberOfGhosts(),level.getNumberOfRocks(),level.getNumberOfOctopuses(),level.getOctopusSpeed());
189 void SeaScene::spreadGhosts(int ghosts)
193 //the octopuses and the ship may have moved from their original positions,
194 //so the list of free slots must be adjusted to exclude their current positions
196 QList<QPointF> temporarilyReservedSlots;
198 foreach (QGraphicsItem* pItem, movingItems_)
202 // qDebug() << "NULL item in movingItems_";
206 //round x and y down to fit the slot size
216 QPointF position (x,y);
218 //remove the tiles (potentially) occupied by the item from free slots and place in temp list if was in the list before
220 if (freeTiles_.removeOne(position))
221 temporarilyReservedSlots.append(position);
226 if (freeTiles_.removeOne(position))
227 temporarilyReservedSlots.append(position);
231 if (freeTiles_.removeOne(position))
232 temporarilyReservedSlots.append(position);
236 if (freeTiles_.removeOne(position))
237 temporarilyReservedSlots.append(position);
242 //spread ghosts in random free slots
244 for (int i=0; i < ghosts; i++)
246 QPointF * pPosition = findRandomFreeSlot();
248 //If there was no room no point to continue
249 if (pPosition == NULL)
252 QPixmap ghostPixmap(":/pix/aave.png");
253 QGraphicsPixmapItem * pGhost = addPixmap(ghostPixmap);
254 pGhost->setData(0,"ghost");
255 pGhost->setPos(*pPosition);
259 //return the slots occupied by moving items to free slots
260 freeTiles_.append(temporarilyReservedSlots);
262 //clear temp for the next round
263 temporarilyReservedSlots.clear();
266 QPointF* SeaScene::findRandomFreeSlot()
268 if (freeTiles_.isEmpty())
271 int index = qrand()%freeTiles_.size();
273 // qDebug() << index << " index";
274 return new QPointF (freeTiles_.takeAt(index));
278 void SeaScene::removeGhost(QGraphicsItem *pGhost)
280 removeItem(pGhost); //remove the item from scene
281 freeTiles_.append(pGhost->scenePos()); //add the item's position to free slots
284 if (ghostsLeft_ == 0)
286 emit allGhostsPicked();
287 // qDebug() << "All ghosts picked!";
291 void SeaScene::ghostsDropped(int ghosts)
293 ghostsLeft_ += ghosts;
295 spreadGhosts(ghosts);
298 void SeaScene::pause(bool paused)
300 // qDebug() << "pause pressed " << paused;
301 if (paused_ == paused)
308 // qDebug() << "starting to move again";
310 screenLitKeeper_.keepScreenLit(true);
315 // qDebug("about to stop movement");
317 screenLitKeeper_.keepScreenLit(false);
321 void SeaScene::vibrationActivate(bool on)
323 emit vibrationActivated(on);
326 void SeaScene::menuClicked()
328 QList<QGraphicsItem *> items = selectedItems();
330 //if nothing selected (selection was removed) do nothing
337 QString menuitem = items.at(0)->data(0).toString();
339 if (menuitem == "restart game")
344 else if (menuitem == "restart level")
349 else if (menuitem == "vibration effects")
354 else if (menuitem == "about")
360 //Selection is just used to get notice of being clicked, removed after use
366 void SeaScene::showMenu()
371 void::SeaScene::hideMenu()