-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathCore.cpp
More file actions
209 lines (180 loc) · 4.25 KB
/
Core.cpp
File metadata and controls
209 lines (180 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include "Core.hpp"
Core::Core(gdl::GameClock *clock)
{
this->_firstMusicBomb = false;
this->_map = new CoreMap();
this->gameClock_ = clock;
this->_timeInGame = 0;
}
Core::~Core()
{
delete(_map);
while (!this->_players.empty())
{
delete this->_players.front();
this->_players.pop_front();
}
while (!this->_bombs.empty())
{
delete this->_bombs.front();
this->_bombs.pop_front();
}
}
void Core::setMusic(Music * mus)
{
this->_musicBomb = mus;
}
void Core::checkBomb()
{
std::list<Bomb*>::iterator it;
if (_bombs.size() > 0)
it = _bombs.begin();
for (it = _bombs.begin(); _bombs.size() > 0 && it != _bombs.end(); it++)
{
if ((*it)->isExpired(gameClock_->getTotalGameTime()) == true)
{
this->_musicBomb->playSong("bombS");
(*it)->explose(this);
this->getPlayerById((*it)->getIdPlayer())->getCaracteristique().delBomb();
delete *it;
it = _bombs.erase(it);
}
}
}
bool Core::hasBombOnPos(const unsigned int &x, const unsigned int &y)
{
std::list<Bomb*>::iterator it = _bombs.begin();
for (; it != _bombs.end(); it++)
{
if ((unsigned int)(*it)->getX() == x && (unsigned int)(*it)->getY() == y)
return true;
}
return false;
}
void Core::setMap(CoreMap* map)
{
this->_map = map;
}
void Core::setPlayers(std::list<APlayer*> players)
{
this->_players = players;
}
Position* Core::genPositionPlayers(const Option& opt)
{
unsigned int x;
unsigned int y;
bool findPlace = true;
std::list<APlayer*>::iterator it;
while (findPlace)
{
findPlace = false;
x = 1 + (rand() % (opt.getXMap() - 1));
y = 1 + (rand() % (opt.getYMap() - 1));
if (x % 2 == 0)
x < (opt.getXMap() / 2)? x++: x--;
if (y % 2 == 0)
y < (opt.getXMap() / 2)? y++: y--;
it = this->_players.begin();
while (it != this->_players.end())
{
if ((unsigned int)(*it)->getX() == x && (unsigned int)(*it)->getY() == y)
findPlace = true;
it++;
}
}
float posX = (float)x + 0.5;
float posY = (float)y + 0.5;
Position *ret = new Position(posX, posY);
return ret;
}
void Core::genPlayers(const Option & opt)
{
unsigned int id = 0;
unsigned int nb = opt.getNbPlayer() + opt.getNbIa();
unsigned int nb_tmp = 0;
double x = 1.5;
double y = 1.5;
Caracteristique *carac;
APlayer *player;
while (nb_tmp < nb)
{
carac = new Caracteristique(5);
if (id < opt.getNbPlayer())
player = new APlayer(id, this->genPositionPlayers(opt), carac, false);
else
player = new APlayer(id, genPositionPlayers(opt), carac, true);
player->initialize();
this->_players.push_back(player);
nb_tmp++;
id++;
}
}
APlayer* Core::getPlayerById(const unsigned int& id) const
{
std::list<APlayer*>::iterator it;
std::list<APlayer*> pl = this->getPlayer();
for (it = pl.begin(); it != pl.end() && (*it)->getId() != id; it++)
;
if ((*it)->getId() != id)
return NULL;
return (*it);
}
CoreMap* Core::getMap() const
{
return _map;
}
std::list<APlayer*> Core::getPlayer() const
{
return _players;
}
void Core::isPlayer(unsigned int x, unsigned int y)
{
std::list<APlayer*>::iterator it;
for (it = _players.begin(); it != _players.end(); it++)
{
if ((int)(*it)->getX() == x && (int)(*it)->getY() == y)
(*it)->takeDamage(1, this);
}
}
void Core::setBombs(std::list<Bomb*> bombs)
{
this->_bombs = bombs;
}
unsigned int Core::isBomb(const unsigned int& x, const unsigned int& y, const unsigned int &prevRet)
{
std::list<Bomb*>::iterator it;
for (it = this->_bombs.begin(); it != this->_bombs.end(); it++)
{
if ((unsigned int)(*it)->getX() == x && (unsigned int)(*it)->getY() == y)
return INDES;
}
return prevRet;
}
unsigned int Core::getXMax(void) const
{
return _map->getXMax();
}
unsigned int Core::getYMax(void) const
{
return _map->getYMax();
}
void Core::addBomb(Bomb* newBomb)
{
this->_bombs.push_back(newBomb);
}
std::list<Bomb*> Core::getBomb() const
{
return _bombs;
}
gdl::GameClock *Core::getGameClock() const
{
return gameClock_;
}
float Core::getTime(void) const
{
return this->_timeInGame;
}
void Core::setTime(const float & time)
{
this->_timeInGame = time;
}