Changed the map creation to create each pixmap just once per round
[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 list of free slots
23     freeTiles_.clear();
24
25     //fill the list of free slots
26
27     int numberOfXTiles  = width() / 40;
28     int numberOfYTiles = height() /40;
29
30     qDebug() << numberOfXTiles << " slots in x direction";
31     qDebug() << numberOfYTiles << " slots in y rirection";
32
33     for (int i = 0; i < numberOfXTiles; i++ )
34     {
35         for (int j = 0; j < numberOfYTiles; j++)
36         {
37             freeTiles_.append(QPointF(i*40,j*40));
38         }
39     }
40
41
42     //spread the rocks
43
44     for (int i = 0; i < rocks; i++)
45     {
46         QPointF * pPosition = findRandomFreeSlot();
47
48         //If there was no room no point to continue
49         if (pPosition == NULL)
50             break;
51
52         QPixmap rockPixmap (":/pix/kari.png");
53         QGraphicsPixmapItem * pRock = addPixmap(rockPixmap);
54         pRock->setData(0,"rock");
55         pRock->setPos(*pPosition);
56         delete pPosition;
57
58     }
59
60     //spread the ghosts
61
62     ghostsLeft_ = ghosts;
63     spreadGhosts(ghosts);
64
65
66
67     //spread the octopuses
68
69
70     for (int i=0; i < octopuses; i++)
71     {
72         QPointF * pPosition = findRandomFreeSlot();
73
74         //If there was no room no point to continue
75         if (pPosition == NULL)
76             break;
77
78     QPixmap octopusPixmap (":/pix/tursas.png");
79     TimerControlledTursas * pOctopus = new TimerControlledTursas (octopusPixmap,100);
80     pOctopus->setData(0,"octopus");
81     pOctopus->setPos(*pPosition);
82     addItem(pOctopus);
83     pOctopus->startMoving();
84     delete pPosition;
85
86     }
87
88
89     //place the ship
90
91     QPointF * pPosition = findRandomFreeSlot();
92     if (pPosition == NULL)
93     {
94         // Game cannot begin without a free position for ship, so give an error message and return
95
96         QMessageBox::critical(NULL,"Error! Too many objects on screen","No free space to place the ship. The game cannot start. Please choose another level.");
97         return;
98     }
99
100     Ship * pShip = new Ship (QPixmap(":/pix/laiva.png"));
101     pShip->setData(0,"ship");
102     pShip->setPos(*pPosition);
103     addItem(pShip);
104     connect(pShip,SIGNAL(pickingGhost(QGraphicsItem*)),this, SLOT(removeGhost(QGraphicsItem*)) );
105     connect(pShip,SIGNAL(droppingGhosts(int)),this,SLOT(ghostsDropped(int)));
106     pShip->startMoving();
107     delete pPosition;
108 }
109
110
111 void SeaScene::spreadGhosts(int ghosts)
112 {
113     for (int i=0; i < ghosts; i++)
114     {
115         QPointF * pPosition = findRandomFreeSlot();
116
117         //If there was no room no point to continue
118         if (pPosition == NULL)
119             return;
120
121         QPixmap ghostPixmap(":/pix/aave.png");
122         QGraphicsPixmapItem * pGhost = addPixmap(ghostPixmap);
123         pGhost->setData(0,"ghost");
124         pGhost->setPos(*pPosition);
125         delete pPosition;
126     }
127 }
128
129 QPointF* SeaScene::findRandomFreeSlot()
130 {
131     if (freeTiles_.isEmpty())
132         return NULL;
133
134     int index = qrand()%freeTiles_.size();
135
136     qDebug()  << index << " index";
137     return new QPointF (freeTiles_.takeAt(index));
138
139 }
140
141 void SeaScene::removeGhost(QGraphicsItem *pGhost)
142 {
143     removeItem(pGhost);  //remove the item from scene
144     freeTiles_.append(pGhost->scenePos()); //add the item's position to free slots
145     delete pGhost;
146     ghostsLeft_--;
147     if (ghostsLeft_ == 0)
148     {
149         //here whatever happens when a level is complete / a signal
150         qDebug() << "All ghosts picked!";
151     }
152 }
153
154 void SeaScene::ghostsDropped(int ghosts)
155 {
156     ghostsLeft_ += ghosts;
157
158     spreadGhosts(ghosts);
159 }