c0bda9af801a9ecc95e1a1b023cb630c1c879e2e
[ghostsoverboard] / seascene.cpp
1 #include "seascene.h"
2 #include "timercontrolledtursas.h"
3 #include "ship.h"
4 #include <QGraphicsPixmapItem>
5 #include <QDebug>
6 #include <QMessageBox>
7
8 const QString ghostImageFilename_ = ":/pix/aave.png";
9 const QString rockImageFilename_ =":/pix/kari.png";
10 const QString octopusImageFilename_= ":/pix/tursas.png";
11
12
13 SeaScene::SeaScene(QObject *parent) :
14     QGraphicsScene(parent)
15 {
16
17
18 }
19
20 void SeaScene::setupMap(int ghosts, int rocks, int octopuses)
21 {
22     //empty the map
23
24     clear();
25
26     //empty the list of free slots
27     freeTiles_.clear();
28
29     //fill the list of free slots
30
31     int numberOfXTiles  = width() / 40;
32     int numberOfYTiles = height() /40;
33
34     qDebug() << numberOfXTiles << " slots in x direction";
35     qDebug() << numberOfYTiles << " slots in y rirection";
36
37     for (int i = 0; i < numberOfXTiles; i++ )
38     {
39         for (int j = 0; j < numberOfYTiles; j++)
40         {
41             freeTiles_.append(QPointF(i*40,j*40));
42         }
43     }
44
45
46     //spread the rocks
47
48     for (int i = 0; i < rocks; i++)
49     {
50         QPointF * pPosition = findRandomFreeSlot();
51
52         //If there was no room no point to continue
53         if (pPosition == NULL)
54             break;
55
56         QPixmap rockPixmap (":/pix/kari.png");
57         QGraphicsPixmapItem * pRock = addPixmap(rockPixmap);
58         pRock->setData(0,"rock");
59         pRock->setPos(*pPosition);
60         delete pPosition;
61
62     }
63
64     //spread the ghosts
65
66     ghostsLeft_ = ghosts;
67     spreadGhosts(ghosts);
68
69
70
71     //spread the octopuses
72
73
74     for (int i=0; i < octopuses; i++)
75     {
76         QPointF * pPosition = findRandomFreeSlot();
77
78         //If there was no room no point to continue
79         if (pPosition == NULL)
80             break;
81
82     QPixmap octopusPixmap (":/pix/tursas.png");
83     TimerControlledTursas * pOctopus = new TimerControlledTursas (octopusPixmap,100);
84     pOctopus->setData(0,"octopus");
85     pOctopus->setPos(*pPosition);
86     addItem(pOctopus);
87     pOctopus->startMoving();
88     movingItems_.append(pOctopus);
89     delete pPosition;
90
91     }
92
93
94     //place the ship
95
96     QPointF * pPosition = findRandomFreeSlot();
97     if (pPosition == NULL)
98     {
99         // Game cannot begin without a free position for ship, so give an error message and return
100
101         QMessageBox::critical(NULL,"Error! Too many objects on screen","No free space to place the ship. The game cannot start. Please choose another level.");
102         return;
103     }
104
105     Ship * pShip = new Ship (QPixmap(":/pix/laiva.png"));
106     pShip->setData(0,"ship");
107     pShip->setPos(*pPosition);
108     addItem(pShip);
109     connect(pShip,SIGNAL(pickingGhost(QGraphicsItem*)),this, SLOT(removeGhost(QGraphicsItem*)) );
110     connect(pShip,SIGNAL(droppingGhosts(int)),this,SLOT(ghostsDropped(int)));
111     pShip->startMoving();
112     movingItems_.append(pShip);
113     delete pPosition;
114 }
115
116
117 void SeaScene::spreadGhosts(int ghosts)
118 {
119
120     //the octopuses and the ship may have moved from their original positions,
121     //so the list of free slots must be adjusted to exclude their current positions
122
123     QList<QPointF> temporarilyReservedSlots;
124
125     foreach (QGraphicsItem* pItem, movingItems_)
126     {
127     //TODO
128         //round x and y down to fit the slot size
129         int x = pItem->x();
130         x = x/40;
131         x = x*40;
132         int y = pItem->y();
133         y = y/40;
134         y=y*40;
135
136
137         QPointF position (x,y);
138
139         //remove the tiles (potentially) occupied by the item from free slots and place in temp list if was in the list before
140
141         if (freeTiles_.removeOne(position))
142             temporarilyReservedSlots.append(position);
143
144
145         position.setX(x+40);
146
147         if (freeTiles_.removeOne(position))
148             temporarilyReservedSlots.append(position);
149
150         position.setY(y+40);
151
152         if (freeTiles_.removeOne(position))
153             temporarilyReservedSlots.append(position);
154
155         position.setX(x);
156
157         if (freeTiles_.removeOne(position))
158             temporarilyReservedSlots.append(position);
159
160     }
161
162     //spread ghosts in random free slots
163
164     for (int i=0; i < ghosts; i++)
165     {
166         QPointF * pPosition = findRandomFreeSlot();
167
168         //If there was no room no point to continue
169         if (pPosition == NULL)
170             return;
171
172         QPixmap ghostPixmap(":/pix/aave.png");
173         QGraphicsPixmapItem * pGhost = addPixmap(ghostPixmap);
174         pGhost->setData(0,"ghost");
175         pGhost->setPos(*pPosition);
176         delete pPosition;
177     }
178
179     //return the slots occupied by moving items to free slots
180     freeTiles_.append(temporarilyReservedSlots);
181
182     //clear temp for the next round
183     temporarilyReservedSlots.clear();
184 }
185
186 QPointF* SeaScene::findRandomFreeSlot()
187 {
188     if (freeTiles_.isEmpty())
189         return NULL;
190
191     int index = qrand()%freeTiles_.size();
192
193     qDebug()  << index << " index";
194     return new QPointF (freeTiles_.takeAt(index));
195
196 }
197
198 void SeaScene::removeGhost(QGraphicsItem *pGhost)
199 {
200     removeItem(pGhost);  //remove the item from scene
201     freeTiles_.append(pGhost->scenePos()); //add the item's position to free slots
202     delete pGhost;
203     ghostsLeft_--;
204     if (ghostsLeft_ == 0)
205     {
206         //here whatever happens when a level is complete / a signal
207         qDebug() << "All ghosts picked!";
208     }
209 }
210
211 void SeaScene::ghostsDropped(int ghosts)
212 {
213     ghostsLeft_ += ghosts;
214
215     spreadGhosts(ghosts);
216 }