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