-
Notifications
You must be signed in to change notification settings - Fork 0
/
level.cpp
executable file
·288 lines (268 loc) · 9.33 KB
/
level.cpp
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include <sstream>
#include "base/func_tool.h"
#include "base/creator.h"
#include "level.h"
#include "enemies.h"
#include "traps.h"
#include "items.h"
typedef std::map< std::string, std::vector<SDL_Rect> >::iterator tab_iterator;
void Level::load_objects() { __load_objects(m_map->ly_head); }
Level::Level(): Map("maps/runner.tmx"), earthquake(false)
{
setup_creators();
for (int i = 0; i<(int)world_y; ++i)
for (int j=0; j<(int)world_x; ++j)
{
tmx_tile* tile(m_map->tiles[get_tile_nbr(j, i, false)]);
if (!tile)
continue;
if ((17 <= tile->id and tile->id <= 19) or
(39 <= tile->id and tile->id <= 41) or
(61 <= tile->id and tile->id <= 63))
sprites[i][j] = new Poutre(j*tile_w, i*tile_h);
}
}
Level::~Level()
{
Basic_fan::destroy_bubbles();
}
void Level::setup_creators()
{
/* create factory */
ObjectFactory::registre_creator("AngryPig", new AngryPig::Creator);
ObjectFactory::registre_creator("Bunny", new Bunny::Creator);
ObjectFactory::registre_creator("Chicken", new Chicken::Creator);
ObjectFactory::registre_creator("Chameleon", new Chameleon::Creator);
ObjectFactory::registre_creator("Duck", new Duck::Creator);
ObjectFactory::registre_creator("Ghost", new Ghost::Creator);
ObjectFactory::registre_creator("Mushroom", new Mushroom::Creator);
ObjectFactory::registre_creator("Plant", new Plant::Creator);
ObjectFactory::registre_creator("Rino", new Rino::Creator);
ObjectFactory::registre_creator("Slime", new Slime::Creator);
ObjectFactory::registre_creator("Skull", new Skull::Creator);
ObjectFactory::registre_creator("Trunk", new Trunk::Creator);
ObjectFactory::registre_creator("Turtle", new Turtle::Creator);
ObjectFactory::registre_creator("End", new End::Creator);
ObjectFactory::registre_creator("Box", new Box::Creator);
ObjectFactory::registre_creator("Start", new Start::Creator);
ObjectFactory::registre_creator("Fruits", new Fruit::Creator);
ObjectFactory::registre_creator("Checkpoint", new Checkpoint::Creator);
ObjectFactory::registre_creator("Arrow", new Arrow::Creator);
ObjectFactory::registre_creator("Falling platform", new Falling_platform::Creator);
ObjectFactory::registre_creator("Fan", new Fan::Creator);
ObjectFactory::registre_creator("Spike", new Spike::Creator);
/* set up parameters */
ObjectCreator::addToParameters(this, "game map");
ObjectCreator::addToParameters(viewport, "game viewport");
ObjectCreator::addToParameters(&bullets, "bullets group");
}
void Level::add_enemies(GameObject* enemy)
{
enemies.add(enemy);
}
void Level::add_item(GameObject* item)
{
/*
if (item->is("Checkpoints"))
{
item->set_x(((item->get_x())/tile_w)*tile_w);
item->set_y(((item->get_y())/tile_w)*tile_w);
}
*/
items[item->get_ancestor()].add(item);
}
void Level::add_traps(GameObject* trap)
{
traps.add(trap);
}
void Level::delete_sprite_at(int x, int y)
{
x += viewport->x;
y += viewport->y;
GameObject* sprite = enemies.first_sprite_colliding_with(x, y);
if (sprite)
enemies.remove(sprite);
else
{
sprite = traps.first_sprite_colliding_with(x, y);
if (sprite)
traps.remove(sprite);
else
for (std::map<std::string, Group>::iterator it=items.begin();
it!=items.end(); ++it)
{
sprite = it->second.first_sprite_colliding_with(x, y);
if (sprite)
it->second.remove(sprite);
}
}
}
void Level::update()
{
clamp_shift_coords();
enemies.update();
traps.update();
Basic_fan::update_bubbles(this);
dying.update();
bullets.update();
for (std::map<std::string ,Group>::iterator it=items.begin();
it != items.end(); ++it)
it->second.update();
}
void Level::draw(SDL_Surface* screen)
{
for (std::map<std::string ,Group>::iterator it=items.begin();
it != items.end(); ++it)
it->second.draw(screen);
traps.draw(screen);
Basic_fan::draw_bubbles(screen);
Map::draw(screen);
enemies.draw(screen);
dying.draw(screen);
bullets.draw(screen);
}
bool Level::collision_with(GameObject* sprite)
{
if (dying.has(sprite))
{
remove_enemy(sprite);
return false;
}
if (sprite->is("Player"))
{
GameObject* trap = traps.first_sprite_colliding_with(sprite);
GameObject* enemy = enemies.first_sprite_colliding_with(sprite);
GameObject* bullet = bullets.first_sprite_colliding_with(sprite);
GameObject* box = items["Boxes"].first_sprite_colliding_with(sprite);
GameObject* fruit = items["Fruits"].first_sprite_colliding_with(sprite);
GameObject* checkpoint = items["Checkpoints"].first_sprite_colliding_with(sprite);
if (enemy)
{
if (enemy->is("Turtle") and enemy->check_up /* i.e Spikes out */)
sprite->bump("die");
else
{
// le personnage est entrain de tomber
if (sprite->check_down
// le personnage est au-dessus de l'enemie ... primordiale!!!
)// and abs(sprite->get_bottom() - enemy->get_y()) <= 10)
{
sprite->bump();
remove_enemy(enemy);
}
else
sprite->bump("die");
}
}
if (fruit)
fruit->bump();
if (checkpoint)
checkpoint->bump();
if (box)
if (box->is("Boxes") and sprite->check_down)
{
std::stringstream oss;
oss << sprite->get_impulse()[1];
box->bump(oss.str());
viewport->y += 6;
if (!items["Boxes"].has(box))
{
std::string name[] = { "Apple", "Bananas", "Cherries", "Kiwi",
"Melon", "Orange", "Pineapple", "Strawberry" };
int n(randint(1, 5));
for (int i=0; i<n; ++i)
items["Fruits"].add(new Fruit(box->get_x(), box->get_y()-7, this, name[rand()%8], true));
}
sprite->bump("box repulsion");
}
if (bullet)
if (bullet->is("Bullet"))
{
if (sprite->check_down)
bullet->bump("break");
else
{
bullet->bump();
sprite->bump("die");
}
}
if (trap)
{
if (trap->is("Spike"))
sprite->bump("die");
if (sprite->check_down)
{
if (trap->is("Arrow"))
sprite->bump("arrow repulsion");
else if (trap->is("Falling Platform"))
sprite->bump("platform repulsion");
trap->bump();
}
}
}
else if (!sprite->is("Plant") and !sprite->is("Trunk"))
if (enemies.has(sprite))
{
GameObject* bullet = bullets.first_sprite_colliding_with(sprite);
if (bullet)
if (bullet->is("Bullet"))
{
bullet->bump();
if (sprite->is("AngryPig"))
sprite->bump("shotted");
else
remove_enemy(sprite);
}
}
return Map::collision_with(sprite);
}
void Level::remove_enemy(GameObject* enemy)
{
enemy->bump();
enemies.remove(enemy);
dying.add(enemy);
}
void Level::__load_objects(tmx_layer* layer)
{
while (layer)
{
ObjectCreator::addToParameters(layer, "current layer");
if (layer->type == L_GROUP)
__load_objects(layer->content.group_head);
else if (layer->type == L_OBJGR)
{
for (tmx_object* object = layer->content.objgr->head;
object; object = object->next)
{
ObjectCreator::addToParameters(object, "current object");
/* object->y is the vertical bottom left coordinate */
int x(object->x), y(object->y - object->height);
GameObject* obj(ObjectFactory::create(tmx_get_property(layer->properties, "fruit")?"Fruits":std::string(layer->name), x, y));
std::string type(tmx_get_property(layer->properties, "type")->value.string);
if (type == "Enemies")
add_enemies(obj);
else if (type == "Items")
add_item(obj);
else if (type == "Traps")
add_traps(obj);
}
}
layer = layer->next;
}
}
void Level::center_on(GameObject* sprite, SDL_Rect limit)
{/*
static int s(6);
if (earthquake)
{
viewport->y += s;
s *= -1;
}
else
*/Map::center_on(sprite);
}
RestrictedGroup::RestrictedGroup(SDL_Rect* v) : viewport(v)
{}
void RestrictedGroup::update()
{
}