2 #include "timercontrolledtursas.h"
4 #include <QGraphicsPixmapItem>
8 const QString ghostImageFilename_ = ":/pix/aave.png";
9 const QString rockImageFilename_ =":/pix/kari.png";
10 const QString octopusImageFilename_= ":/pix/tursas.png";
13 SeaScene::SeaScene(QObject *parent) :
14 QGraphicsScene(parent)
20 void SeaScene::setupMap(int ghosts, int rocks, int octopuses)
26 //empty the list of free slots
29 //fill the list of free slots
31 int numberOfXTiles = width() / 40;
32 int numberOfYTiles = height() /40;
34 qDebug() << numberOfXTiles << " slots in x direction";
35 qDebug() << numberOfYTiles << " slots in y rirection";
37 for (int i = 0; i < numberOfXTiles; i++ )
39 for (int j = 0; j < numberOfYTiles; j++)
41 freeTiles_.append(QPointF(i*40,j*40));
48 for (int i = 0; i < rocks; i++)
50 QPointF * pPosition = findRandomFreeSlot();
52 //If there was no room no point to continue
53 if (pPosition == NULL)
56 QPixmap rockPixmap (":/pix/kari.png");
57 QGraphicsPixmapItem * pRock = addPixmap(rockPixmap);
58 pRock->setData(0,"rock");
59 pRock->setPos(*pPosition);
71 //spread the octopuses
74 for (int i=0; i < octopuses; i++)
76 QPointF * pPosition = findRandomFreeSlot();
78 //If there was no room no point to continue
79 if (pPosition == NULL)
82 QPixmap octopusPixmap (":/pix/tursas.png");
83 TimerControlledTursas * pOctopus = new TimerControlledTursas (octopusPixmap,100);
84 pOctopus->setData(0,"octopus");
85 pOctopus->setPos(*pPosition);
87 pOctopus->startMoving();
95 QPointF * pPosition = findRandomFreeSlot();
96 if (pPosition == NULL)
98 // Game cannot begin without a free position for ship, so give an error message and return
100 QMessageBox::critical(NULL,"Error! Too many objects on screen","No free space to place the ship. The game cannot start. Please choose another level.");
104 QList<QPixmap> shipImages;
105 shipImages.append(QPixmap(":/pix/laiva.png"));
106 shipImages.append(QPixmap(":/pix/laiva_1aave.png"));
107 shipImages.append(QPixmap(":/pix/laiva_2aave.png"));
108 shipImages.append(QPixmap(":/pix/laiva_3aave.png"));
109 shipImages.append(QPixmap(":/pix/laiva_4aave.png"));
110 shipImages.append(QPixmap(":/pix/laiva_5aave.png"));
111 shipImages.append(QPixmap(":/pix/laiva_6aave.png"));
112 shipImages.append(QPixmap(":/pix/laiva_7aave.png"));
113 shipImages.append(QPixmap(":/pix/laiva_8aave.png"));
114 shipImages.append(QPixmap(":/pix/laiva_9aave.png"));
115 shipImages.append(QPixmap(":/pix/laiva_10aave.png"));
117 Ship * pShip = new Ship (shipImages);
118 pShip->setData(0,"ship");
119 pShip->setPos(*pPosition);
121 connect(pShip,SIGNAL(pickingGhost(QGraphicsItem*)),this, SLOT(removeGhost(QGraphicsItem*)) );
122 connect(pShip,SIGNAL(droppingGhosts(int)),this,SLOT(ghostsDropped(int)));
123 pShip->startMoving();
128 void SeaScene::spreadGhosts(int ghosts)
130 for (int i=0; i < ghosts; i++)
132 QPointF * pPosition = findRandomFreeSlot();
134 //If there was no room no point to continue
135 if (pPosition == NULL)
138 QPixmap ghostPixmap(":/pix/aave.png");
139 QGraphicsPixmapItem * pGhost = addPixmap(ghostPixmap);
140 pGhost->setData(0,"ghost");
141 pGhost->setPos(*pPosition);
146 QPointF* SeaScene::findRandomFreeSlot()
148 if (freeTiles_.isEmpty())
151 int index = qrand()%freeTiles_.size();
153 qDebug() << index << " index";
154 return new QPointF (freeTiles_.takeAt(index));
158 void SeaScene::removeGhost(QGraphicsItem *pGhost)
160 removeItem(pGhost); //remove the item from scene
161 freeTiles_.append(pGhost->scenePos()); //add the item's position to free slots
164 if (ghostsLeft_ == 0)
166 //here whatever happens when a level is complete / a signal
167 qDebug() << "All ghosts picked!";
171 void SeaScene::ghostsDropped(int ghosts)
173 ghostsLeft_ += ghosts;
175 spreadGhosts(ghosts);