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()));
66 void SeaScene::setupMap(int ghosts, int rocks, int octopuses, int octopusSpeed)
74 //empty the list of moving items
78 //empty the list of free slots
81 //fill the list of free slots
83 int numberOfXTiles = width() / 40;
84 int numberOfYTiles = height() /40;
86 // qDebug() << numberOfXTiles << " slots in x direction";
87 // qDebug() << numberOfYTiles << " slots in y rirection";
89 for (int i = 0; i < numberOfXTiles; i++ )
91 for (int j = 0; j < numberOfYTiles; j++)
93 freeTiles_.append(QPointF(i*40,j*40));
100 for (int i = 0; i < rocks; i++)
102 QPointF * pPosition = findRandomFreeSlot();
104 //If there was no room no point to continue
105 if (pPosition == NULL)
108 QPixmap rockPixmap (":/pix/kari.png");
109 QGraphicsPixmapItem * pRock = addPixmap(rockPixmap);
110 pRock->setData(0,"rock");
111 pRock->setPos(*pPosition);
118 ghostsLeft_ = ghosts;
119 spreadGhosts(ghosts);
123 //spread the octopuses
125 QList <Octopus*> octopusList;
127 for (int i=0; i < octopuses; i++)
129 QPointF * pPosition = findRandomFreeSlot();
131 //If there was no room no point to continue
132 if (pPosition == NULL)
135 QPixmap octopusPixmap (":/pix/tursas.png");
136 Octopus * pOctopus = new Octopus(octopusPixmap,octopusSpeed);
137 pOctopus->setData(0,"octopus");
138 pOctopus->setPos(*pPosition);
140 pOctopus->startMoving();
141 movingItems_.append(pOctopus);
142 connect(this,SIGNAL(pauseOn()),pOctopus,SLOT(stopMoving()));
143 connect(this,SIGNAL(pauseOff()),pOctopus,SLOT(startMoving()));
144 octopusList.append(pOctopus);
152 QPointF * pPosition = findRandomFreeSlot();
153 if (pPosition == NULL)
155 // Game cannot begin without a free position for ship, so give an error message and return
157 QMessageBox::critical(NULL,"Error! Too many objects on screen","No free space to place the ship. The game cannot start. Please choose another level.");
161 QList<QPixmap> shipImages;
162 shipImages.append(QPixmap(":/pix/laiva.png"));
163 shipImages.append(QPixmap(":/pix/laiva_1aave.png"));
164 shipImages.append(QPixmap(":/pix/laiva_2aave.png"));
165 shipImages.append(QPixmap(":/pix/laiva_3aave.png"));
166 shipImages.append(QPixmap(":/pix/laiva_4aave.png"));
167 shipImages.append(QPixmap(":/pix/laiva_5aave.png"));
168 shipImages.append(QPixmap(":/pix/laiva_6aave.png"));
169 shipImages.append(QPixmap(":/pix/laiva_7aave.png"));
170 shipImages.append(QPixmap(":/pix/laiva_8aave.png"));
171 shipImages.append(QPixmap(":/pix/laiva_9aave.png"));
172 shipImages.append(QPixmap(":/pix/laiva_10aave.png"));
174 Ship * pShip = new Ship (shipImages);
175 pShip->setData(0,"ship");
176 pShip->setPos(*pPosition);
178 connect(pShip,SIGNAL(pickingGhost(QGraphicsItem*)),this, SLOT(removeGhost(QGraphicsItem*)) );
179 connect(pShip,SIGNAL(droppingGhosts(int)),this,SLOT(ghostsDropped(int)));
180 connect(this,SIGNAL(vibrationActivated(bool)),pShip,SLOT(setVibrationActivate(bool)));
181 pShip->startMoving();
182 movingItems_.append(pShip);
183 connect(this,SIGNAL(pauseOn()),pShip,SLOT(stopMoving()));
184 connect(this,SIGNAL(pauseOff()),pShip,SLOT(startMoving()));
185 foreach (Octopus* pOctopus, octopusList)
187 connect(pOctopus,SIGNAL(droppingGhosts()),pShip,SLOT(dropAllGhosts()));
192 void SeaScene::setupMap(Level level)
194 setupMap(level.getNumberOfGhosts(),level.getNumberOfRocks(),level.getNumberOfOctopuses(),level.getOctopusSpeed());
197 void SeaScene::spreadGhosts(int ghosts)
201 //the octopuses and the ship may have moved from their original positions,
202 //so the list of free slots must be adjusted to exclude their current positions
204 QList<QPointF> temporarilyReservedSlots;
206 foreach (QGraphicsItem* pItem, movingItems_)
210 // qDebug() << "NULL item in movingItems_";
214 //round x and y down to fit the slot size
224 QPointF position (x,y);
226 //remove the tiles (potentially) occupied by the item from free slots and place in temp list if was in the list before
228 if (freeTiles_.removeOne(position))
229 temporarilyReservedSlots.append(position);
234 if (freeTiles_.removeOne(position))
235 temporarilyReservedSlots.append(position);
239 if (freeTiles_.removeOne(position))
240 temporarilyReservedSlots.append(position);
244 if (freeTiles_.removeOne(position))
245 temporarilyReservedSlots.append(position);
250 //spread ghosts in random free slots
252 for (int i=0; i < ghosts; i++)
254 QPointF * pPosition = findRandomFreeSlot();
256 //If there was no room no point to continue
257 if (pPosition == NULL)
260 QPixmap ghostPixmap(":/pix/aave.png");
261 QGraphicsPixmapItem * pGhost = addPixmap(ghostPixmap);
262 pGhost->setData(0,"ghost");
263 pGhost->setPos(*pPosition);
267 //return the slots occupied by moving items to free slots
268 freeTiles_.append(temporarilyReservedSlots);
270 //clear temp for the next round
271 temporarilyReservedSlots.clear();
274 QPointF* SeaScene::findRandomFreeSlot()
276 if (freeTiles_.isEmpty())
279 int index = qrand()%freeTiles_.size();
281 // qDebug() << index << " index";
282 return new QPointF (freeTiles_.takeAt(index));
286 void SeaScene::removeGhost(QGraphicsItem *pGhost)
288 removeItem(pGhost); //remove the item from scene
289 freeTiles_.append(pGhost->scenePos()); //add the item's position to free slots
292 if (ghostsLeft_ == 0)
294 emit allGhostsPicked();
295 // qDebug() << "All ghosts picked!";
299 void SeaScene::ghostsDropped(int ghosts)
301 ghostsLeft_ += ghosts;
303 spreadGhosts(ghosts);
306 void SeaScene::pause(bool paused)
308 // qDebug() << "pause pressed " << paused;
309 if (paused_ == paused)
316 // qDebug() << "starting to move again";
318 screenLitKeeper_.keepScreenLit(true);
320 pPausetextItem_->hide();
325 qDebug("about to stop movement");
327 screenLitKeeper_.keepScreenLit(false);
328 if (pPausetextItem_ != NULL)
330 qDebug() << "about to show the pause text";
331 pPausetextItem_->show();
332 qDebug() << "showing pause text";
334 else qDebug() << "No pause text available";
338 void SeaScene::vibrationActivate(bool on)
340 emit vibrationActivated(on);
343 void SeaScene::menuClicked()
345 QList<QGraphicsItem *> items = selectedItems();
347 //if nothing selected (selection was removed) do nothing
352 //Tapping the screen unpaused the game, pause it again!
358 QGraphicsItem* pItem = items.at(0);
361 if (pItem == pRestartGameItem_)
363 qDebug() << "game restart requested";
366 else if (pItem == pRestartLevelItem_)
368 qDebug() << "Level restart requested";
372 else if (pItem == pSettingsItem_)
377 else if (pItem == pAboutItem_)
383 //Selection is just used to get notice of being clicked, removed after use
391 void SeaScene::createMenuItems()
394 pPausetextItem_ = addSimpleText("Game paused. Tap to continue.");
395 pPausetextItem_->setZValue(1000);
396 pPausetextItem_->setPos(250,50);
397 pPausetextItem_->hide();
401 pRestartGameItem_ = new QGraphicsSimpleTextItem("Restart game");
402 prepareForMenu(pRestartGameItem_);
404 pRestartLevelItem_ = new QGraphicsSimpleTextItem("Restart level");
405 prepareForMenu(pRestartLevelItem_);
407 pSettingsItem_ = new QGraphicsSimpleTextItem("Settings");
408 prepareForMenu(pSettingsItem_);
410 pAboutItem_ = new QGraphicsSimpleTextItem("About");
411 prepareForMenu(pAboutItem_);
415 void SeaScene::prepareForMenu(QGraphicsItem * pItem)
418 //Menu items have pause text item as their parent and are thus added to scene automatically
419 //They are also shown and hidden with it, resulting in the menu being visble when the game is paused
420 //Their coordinates are given relative to the parent.
422 pItem->setParentItem(pPausetextItem_);
423 pItem->setZValue(1000);
424 pItem->setFlag(QGraphicsItem::ItemIsSelectable);
426 pItem->setX(menuItemCount_++*150-250);