Separated actual collision detection into subclass Ship
[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     spreadGhosts(ghosts);
62
63
64
65     //spread the octopuses
66
67
68     for (int i=0; i < octopuses; i++)
69     {
70         QPointF * pPosition = findRandomFreeSlot();
71
72         //If there was no room no point to continue
73         if (pPosition == NULL)
74             break;
75
76     TimerControlledTursas * pOctopus = new TimerControlledTursas (QPixmap(":/pix/tursas.png"),100);
77     pOctopus->setData(0,"octopus");
78     pOctopus->setPos(*pPosition);
79     addItem(pOctopus);
80     pOctopus->startMoving();
81     delete pPosition;
82
83     }
84
85
86     //place the ship
87
88     QPointF * pPosition = findRandomFreeSlot();
89     if (pPosition == NULL)
90     {
91         // Game cannot begin without a free position for ship, so give an error message and return
92
93         QMessageBox::critical(NULL,"Error! Too many objects on screen","No free space to place the ship. The game cannot start. Please choose another level.");
94         return;
95     }
96
97     Ship * pShip = new Ship (QPixmap(":/pix/laiva.png"));
98     pShip->setData(0,"ship");
99     pShip->setPos(*pPosition);
100     addItem(pShip);
101     pShip->startMoving();
102     delete pPosition;
103 }
104
105
106 void SeaScene::spreadGhosts(int ghosts)
107 {
108     for (int i=0; i < ghosts; i++)
109     {
110         QPointF * pPosition = findRandomFreeSlot();
111
112         //If there was no room no point to continue
113         if (pPosition == NULL)
114             return;
115
116         QGraphicsPixmapItem * pGhost = addPixmap(QPixmap(":/pix/aave.png"));
117         pGhost->setData(0,"ghost");
118         pGhost->setPos(*pPosition);
119         delete pPosition;
120     }
121 }
122
123 QPointF* SeaScene::findRandomFreeSlot()
124 {
125     if (freeTiles_.isEmpty())
126         return NULL;
127
128     int index = qrand()%freeTiles_.size();
129
130     qDebug()  << index << " index";
131     return new QPointF (freeTiles_.takeAt(index));
132
133 }