void
BadGuy::remove_me()
{
- std::vector<BadGuy>::iterator i;
- for(i = bad_guys.begin(); i != bad_guys.end(); ++i) {
- if( & (*i) == this) {
- bad_guys.erase(i);
- return;
+ for(std::vector<BadGuy>::iterator i = world.bad_guys.begin();
+ i != world.bad_guys.end(); ++i)
+ {
+ if( & (*i) == this) {
+ world.bad_guys.erase(i);
+ return;
+ }
}
- }
}
void
{
make_player_jump(player);
- add_score(base.x - scroll_x, base.y, 50 * score_multiplier);
+ world.add_score(base.x - scroll_x, base.y, 50 * score_multiplier);
play_sound(sounds[SND_SQUISH], SOUND_CENTER_SPEAKER);
score_multiplier++;
BadGuy::squish(Player* player)
{
if(kind == BAD_MRBOMB) {
- // mrbomb transforms into a bomb now
- add_bad_guy(base.x, base.y, BAD_BOMB);
-
- make_player_jump(player);
- add_score(base.x - scroll_x, base.y, 50 * score_multiplier);
- play_sound(sounds[SND_SQUISH], SOUND_CENTER_SPEAKER);
- score_multiplier++;
+ // mrbomb transforms into a bomb now
+ world.add_bad_guy(base.x, base.y, BAD_BOMB);
+
+ make_player_jump(player);
+ world.add_score(base.x - scroll_x, base.y, 50 * score_multiplier);
+ play_sound(sounds[SND_SQUISH], SOUND_CENTER_SPEAKER);
+ score_multiplier++;
- remove_me();
- return;
+ remove_me();
+ return;
} else if(kind == BAD_BSOD) {
- squish_me(player);
- set_texture(img_bsod_squished_left, img_bsod_squished_right, 1);
- physic.set_velocity(0, physic.get_velocity_y());
- return;
+ squish_me(player);
+ set_texture(img_bsod_squished_left, img_bsod_squished_right, 1);
+ physic.set_velocity(0, physic.get_velocity_y());
+ return;
} else if (kind == BAD_LAPTOP) {
- if (mode == NORMAL || mode == KICK)
+ if (mode == NORMAL || mode == KICK)
{
- /* Flatten! */
- play_sound(sounds[SND_STOMP], SOUND_CENTER_SPEAKER);
- mode = FLAT;
- set_texture(img_laptop_flat_left, img_laptop_flat_right, 1);
- physic.set_velocity(0, physic.get_velocity_y());
+ /* Flatten! */
+ play_sound(sounds[SND_STOMP], SOUND_CENTER_SPEAKER);
+ mode = FLAT;
+ set_texture(img_laptop_flat_left, img_laptop_flat_right, 1);
+ physic.set_velocity(0, physic.get_velocity_y());
- timer_start(&timer, 4000);
+ timer_start(&timer, 4000);
} else if (mode == FLAT) {
- /* Kick! */
- play_sound(sounds[SND_KICK], SOUND_CENTER_SPEAKER);
-
- if (player->base.x < base.x + (base.width/2)) {
- physic.set_velocity(5, physic.get_velocity_y());
- dir = RIGHT;
- } else {
- physic.set_velocity(-5, physic.get_velocity_y());
- dir = LEFT;
- }
-
- mode = KICK;
- set_texture(img_laptop_flat_left, img_laptop_flat_right, 1);
+ /* Kick! */
+ play_sound(sounds[SND_KICK], SOUND_CENTER_SPEAKER);
+
+ if (player->base.x < base.x + (base.width/2)) {
+ physic.set_velocity(5, physic.get_velocity_y());
+ dir = RIGHT;
+ } else {
+ physic.set_velocity(-5, physic.get_velocity_y());
+ dir = LEFT;
+ }
+
+ mode = KICK;
+ set_texture(img_laptop_flat_left, img_laptop_flat_right, 1);
}
- make_player_jump(player);
+ make_player_jump(player);
- add_score(base.x - scroll_x, base.y, 25 * score_multiplier);
- score_multiplier++;
- return;
+ world.add_score(base.x - scroll_x, base.y, 25 * score_multiplier);
+ score_multiplier++;
+ return;
} else if(kind == BAD_FISH) {
make_player_jump(player);
- add_score(base.x - scroll_x, base.y, 25 * score_multiplier);
+ world.add_score(base.x - scroll_x, base.y, 25 * score_multiplier);
score_multiplier++;
// simply remove the fish...
/* Gain some points: */
if (kind == BAD_BSOD)
- add_score(base.x - scroll_x, base.y,
- 50 * score_multiplier);
+ world.add_score(base.x - scroll_x, base.y,
+ 50 * score_multiplier);
else
- add_score(base.x - scroll_x, base.y,
- 25 * score_multiplier);
+ world.add_score(base.x - scroll_x, base.y,
+ 25 * score_multiplier);
/* Play death sound: */
play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
void collision_handler()
{
// CO_BULLET & CO_BADGUY check
- for(unsigned int i = 0; i < bullets.size(); ++i)
+ for(unsigned int i = 0; i < world.bullets.size(); ++i)
{
- for(unsigned int j = 0; j < bad_guys.size(); ++j)
+ for(unsigned int j = 0; j < world.bad_guys.size(); ++j)
{
- if(bad_guys[j].dying != DYING_NOT)
+ if(world.bad_guys[j].dying != DYING_NOT)
continue;
- if(rectcollision(&bullets[i].base, &bad_guys[j].base))
+ if(rectcollision(&world.bullets[i].base, &world.bad_guys[j].base))
{
// We have detected a collision and now call the
// collision functions of the collided objects.
// collide with bad_guy first, since bullet_collision will
// delete the bullet
- bad_guys[j].collision(0, CO_BULLET);
- bullet_collision(&bullets[i], CO_BADGUY);
+ world.bad_guys[j].collision(0, CO_BULLET);
+ bullet_collision(&world.bullets[i], CO_BADGUY);
break; // bullet is invalid now, so break
}
}
}
/* CO_BADGUY & CO_BADGUY check */
- for(unsigned int i = 0; i < bad_guys.size(); ++i)
+ for(unsigned int i = 0; i < world.bad_guys.size(); ++i)
{
- if(bad_guys[i].dying != DYING_NOT)
+ if(world.bad_guys[i].dying != DYING_NOT)
continue;
- for(unsigned int j = i+1; j < bad_guys.size(); ++j)
+ for(unsigned int j = i+1; j < world.bad_guys.size(); ++j)
{
- if(j == i || bad_guys[j].dying != DYING_NOT)
+ if(j == i || world.bad_guys[j].dying != DYING_NOT)
continue;
- if(rectcollision(&bad_guys[i].base, &bad_guys[j].base))
+ if(rectcollision(&world.bad_guys[i].base, &world.bad_guys[j].base))
{
// We have detected a collision and now call the
// collision functions of the collided objects.
- bad_guys[j].collision(&bad_guys[i], CO_BADGUY);
- bad_guys[i].collision(&bad_guys[j], CO_BADGUY);
+ world.bad_guys[j].collision(&world.bad_guys[i], CO_BADGUY);
+ world.bad_guys[i].collision(&world.bad_guys[j], CO_BADGUY);
}
}
}
if(tux.dying != DYING_NOT) return;
// CO_BADGUY & CO_PLAYER check
- for(unsigned int i = 0; i < bad_guys.size(); ++i)
+ for(unsigned int i = 0; i < world.bad_guys.size(); ++i)
{
- if(bad_guys[i].dying != DYING_NOT)
+ if(world.bad_guys[i].dying != DYING_NOT)
continue;
- if(rectcollision_offset(&bad_guys[i].base,&tux.base,0,0))
+ if(rectcollision_offset(&world.bad_guys[i].base,&tux.base,0,0))
{
// We have detected a collision and now call the collision
// functions of the collided objects.
if (tux.previous_base.y < tux.base.y &&
- tux.previous_base.y + tux.previous_base.height < bad_guys[i].base.y + bad_guys[i].base.height/2)
+ tux.previous_base.y + tux.previous_base.height
+ < world.bad_guys[i].base.y + world.bad_guys[i].base.height/2)
{
- bad_guys[i].collision(&tux, CO_PLAYER, COLLISION_SQUISH);
+ world.bad_guys[i].collision(&tux, CO_PLAYER, COLLISION_SQUISH);
}
else
{
- tux.collision(&bad_guys[i], CO_BADGUY);
+ tux.collision(&world.bad_guys[i], CO_BADGUY);
}
}
}
// CO_UPGRADE & CO_PLAYER check
- for(unsigned int i = 0; i < upgrades.size(); ++i)
+ for(unsigned int i = 0; i < world.upgrades.size(); ++i)
{
- if(rectcollision(&upgrades[i].base,&tux.base))
+ if(rectcollision(&world.upgrades[i].base, &tux.base))
{
// We have detected a collision and now call the collision
// functions of the collided objects.
- upgrade_collision(&upgrades[i], &tux, CO_PLAYER);
+ upgrade_collision(&world.upgrades[i], &tux, CO_PLAYER);
}
}
level = levelnb;
/* Init the game: */
- arrays_free();
+ world.arrays_free();
set_defaults();
strcpy(level_subset, subset.c_str());
i != plevel->badguy_data.end();
++i)
{
- add_bad_guy(i->x, i->y, i->kind);
+ world.add_bad_guy(i->x, i->y, i->kind);
}
}
{
if(current_level.particle_system == "clouds")
{
- particle_systems.push_back(new CloudParticleSystem);
+ world.particle_systems.push_back(new CloudParticleSystem);
}
else if(current_level.particle_system == "snow")
{
- particle_systems.push_back(new SnowParticleSystem);
+ world.particle_systems.push_back(new SnowParticleSystem);
}
else if(current_level.particle_system != "")
{
current_level.cleanup();
level_free_song();
unloadshared();
- arrays_free();
+ world.arrays_free();
return(0);
}
tux.level_begin();
current_level.cleanup();
level_free_song();
unloadshared();
- arrays_free();
+ world.arrays_free();
return(0);
} /* if (lives < 0) */
}
return 0;
}
- arrays_free();
+ world.arrays_free();
activate_bad_guys(¤t_level);
activate_particle_systems();
level_free_gfx();
tux.action();
- /* Handle bouncy distros: */
- for (unsigned int i = 0; i < bouncy_distros.size(); i++)
- bouncy_distro_action(&bouncy_distros[i]);
-
- /* Handle broken bricks: */
- for (unsigned int i = 0; i < broken_bricks.size(); i++)
- broken_brick_action(&broken_bricks[i]);
-
- /* Handle distro counting: */
- if (counting_distros)
- {
- distro_counter--;
-
- if (distro_counter <= 0)
- counting_distros = -1;
- }
-
- // Handle all kinds of game objects
- for (unsigned int i = 0; i < bouncy_bricks.size(); i++)
- bouncy_brick_action(&bouncy_bricks[i]);
-
- for (unsigned int i = 0; i < floating_scores.size(); i++)
- floating_score_action(&floating_scores[i]);
-
- for (unsigned int i = 0; i < bullets.size(); ++i)
- bullet_action(&bullets[i]);
-
- for (unsigned int i = 0; i < upgrades.size(); i++)
- upgrade_action(&upgrades[i]);
-
- for (unsigned int i = 0; i < bad_guys.size(); i++)
- bad_guys[i].action();
+ world.action();
/* update particle systems */
std::vector<ParticleSystem*>::iterator p;
- for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
+ for(p = world.particle_systems.begin(); p != world.particle_systems.end(); ++p)
{
(*p)->simulate(frame_ratio);
}
return -1;
}
-/* --- GAME DRAW! --- */
-
void
GameSession::draw()
{
/* Draw particle systems (background) */
std::vector<ParticleSystem*>::iterator p;
- for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
+ for(p = world.particle_systems.begin(); p != world.particle_systems.end(); ++p)
{
(*p)->draw(scroll_x, 0, 0);
}
/* Draw background: */
-
for (y = 0; y < 15; ++y)
{
for (x = 0; x < 21; ++x)
current_level.ia_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
}
}
+
+ world.draw();
- /* (Bouncy bricks): */
- for (unsigned int i = 0; i < bouncy_bricks.size(); ++i)
- bouncy_brick_draw(&bouncy_bricks[i]);
-
- for (unsigned int i = 0; i < bad_guys.size(); ++i)
- bad_guys[i].draw();
-
- tux.draw();
-
- for (unsigned int i = 0; i < bullets.size(); ++i)
- bullet_draw(&bullets[i]);
-
- for (unsigned int i = 0; i < floating_scores.size(); ++i)
- floating_score_draw(&floating_scores[i]);
-
- for (unsigned int i = 0; i < upgrades.size(); ++i)
- upgrade_draw(&upgrades[i]);
-
- for (unsigned int i = 0; i < bouncy_distros.size(); ++i)
- bouncy_distro_draw(&bouncy_distros[i]);
-
- for (unsigned int i = 0; i < broken_bricks.size(); ++i)
- broken_brick_draw(&broken_bricks[i]);
+ for (unsigned int i = 0; i < world.broken_bricks.size(); ++i)
+ broken_brick_draw(&world.broken_bricks[i]);
/* Draw foreground: */
for (y = 0; y < 15; ++y)
}
/* Draw particle systems (foreground) */
- for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
+ for(p = world.particle_systems.begin(); p != world.particle_systems.end(); ++p)
{
(*p)->draw(scroll_x, 0, 1);
}
current_level.cleanup();
level_free_song();
unloadshared();
- arrays_free();
+ world.arrays_free();
game_started = false;
/* Bounce a brick: */
void bumpbrick(float x, float y)
{
- add_bouncy_brick(((int)(x + 1) / 32) * 32,
- (int)(y / 32) * 32);
+ world.add_bouncy_brick(((int)(x + 1) / 32) * 32,
+ (int)(y / 32) * 32);
play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER);
}
current_level.cleanup();
if(current_level.load(level_subset,level) != 0)
exit(1);
- arrays_free();
+ world.arrays_free();
activate_bad_guys(¤t_level);
activate_particle_systems();
level_free_gfx();
extern int game_started;
+/** The GameSession class controlls the controll flow of a World, ie.
+ present the menu on specifc keypresses, render and update it while
+ keeping the speed and framerate sane, etc. */
class GameSession
{
private:
le_level_subset.load(level_subsets.item[i-2]);
leveleditor_menu->item[3].kind = MN_GOTO;
le_level = 1;
- arrays_free();
+ world.arrays_free();
loadshared();
le_current_level = new Level;
if(le_current_level->load(le_level_subset.name.c_str(), le_level) != 0)
le_level_subset.load(subset_new_menu->item[2].input);
leveleditor_menu->item[3].kind = MN_GOTO;
le_level = 1;
- arrays_free();
+ world.arrays_free();
loadshared();
le_current_level = new Level;
if(le_current_level->load(le_level_subset.name.c_str(), le_level) != 0)
void le_goto_level(int levelnb)
{
- arrays_free();
+ world.arrays_free();
le_current_level->cleanup();
if(le_current_level->load(le_level_subset.name.c_str(), levelnb) != 0)
level_free_gfx();
le_current_level->cleanup();
unloadshared();
- arrays_free();
+ world.arrays_free();
}
}
}
/* Draw the Bad guys: */
- for (i = 0; i < bad_guys.size(); ++i)
+ for (i = 0; i < world.bad_guys.size(); ++i)
{
/* to support frames: img_bsod_left[(frame / 5) % 4] */
scroll_x = pos_x;
- bad_guys[i].draw();
+ world.bad_guys[i].draw();
}
xx = ((int)x / 32);
/* if there is a bad guy over there, remove it */
- for(i = 0; i < bad_guys.size(); ++i)
- if(xx == bad_guys[i].base.x/32 && yy == bad_guys[i].base.y/32)
- bad_guys.erase(static_cast<std::vector<BadGuy>::iterator>(&bad_guys[i]));
+ for(i = 0; i < world.bad_guys.size(); ++i)
+ if(xx == world.bad_guys[i].base.x/32 && yy == world.bad_guys[i].base.y/32)
+ world.bad_guys.erase(static_cast<std::vector<BadGuy>::iterator>(&world.bad_guys[i]));
if(c == '0') /* if it's a bad guy */
- add_bad_guy(xx*32, yy*32, BAD_BSOD);
+ world.add_bad_guy(xx*32, yy*32, BAD_BSOD);
else if(c == '1')
- add_bad_guy(xx*32, yy*32, BAD_LAPTOP);
+ world.add_bad_guy(xx*32, yy*32, BAD_LAPTOP);
else if(c == '2')
- add_bad_guy(xx*32, yy*32, BAD_MONEY);
+ world.add_bad_guy(xx*32, yy*32, BAD_MONEY);
break;
case SQUARE:
y2 /= 32;
/* if there is a bad guy over there, remove it */
- for(i = 0; i < bad_guys.size(); ++i)
- if(bad_guys[i].base.x/32 >= x1 && bad_guys[i].base.x/32 <= x2
- && bad_guys[i].base.y/32 >= y1 && bad_guys[i].base.y/32 <= y2)
- bad_guys.erase(static_cast<std::vector<BadGuy>::iterator>(&bad_guys[i]));
+ for(i = 0; i < world.bad_guys.size(); ++i)
+ if(world.bad_guys[i].base.x/32 >= x1 && world.bad_guys[i].base.x/32 <= x2
+ && world.bad_guys[i].base.y/32 >= y1 && world.bad_guys[i].base.y/32 <= y2)
+ world.bad_guys.erase(static_cast<std::vector<BadGuy>::iterator>(&world.bad_guys[i]));
for(xx = x1; xx <= x2; xx++)
for(yy = y1; yy <= y2; yy++)
le_current_level->change(xx*32, yy*32, tm, c);
if(c == '0') // if it's a bad guy
- add_bad_guy(xx*32, yy*32, BAD_BSOD);
+ world.add_bad_guy(xx*32, yy*32, BAD_BSOD);
else if(c == '1')
- add_bad_guy(xx*32, yy*32, BAD_LAPTOP);
+ world.add_bad_guy(xx*32, yy*32, BAD_LAPTOP);
else if(c == '2')
- add_bad_guy(xx*32, yy*32, BAD_MONEY);
+ world.add_bad_guy(xx*32, yy*32, BAD_MONEY);
}
break;
default:
session.run();
Menu::set_current(leveleditor_menu);
- arrays_free();
+ world.arrays_free();
le_current_level->load_gfx();
loadshared();
activate_bad_guys(le_current_level);
if (input.fire == DOWN && input.old_fire == UP && got_coffee)
{
- add_bullet(base.x, base.y, physic.get_velocity_x(), dir);
+ world.add_bullet(base.x, base.y, physic.get_velocity_x(), dir);
}
{
pbad_c->dying = DYING_FALLING;
play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
- add_score(pbad_c->base.x - scroll_x,
- pbad_c->base.y,
- 25 * score_multiplier);
+ world.add_score(pbad_c->base.x - scroll_x,
+ pbad_c->base.y,
+ 25 * score_multiplier);
}
}
}
float scroll_x;
unsigned int global_frame_counter;
-std::vector<bouncy_distro_type> bouncy_distros;
-std::vector<broken_brick_type> broken_bricks;
-std::vector<bouncy_brick_type> bouncy_bricks;
-std::vector<BadGuy> bad_guys;
-std::vector<floating_score_type> floating_scores;
-std::vector<upgrade_type> upgrades;
-std::vector<bullet_type> bullets;
-std::vector<ParticleSystem*> particle_systems;
Player tux;
texture_type img_box_full;
texture_type img_box_empty;
timer_type time_left;
double frame_ratio;
-void arrays_free(void)
-{
- bad_guys.clear();
- bouncy_distros.clear();
- broken_bricks.clear();
- bouncy_bricks.clear();
- floating_scores.clear();
- upgrades.clear();
- bullets.clear();
- std::vector<ParticleSystem*>::iterator i;
- for(i = particle_systems.begin(); i != particle_systems.end(); ++i) {
- delete *i;
- }
- particle_systems.clear();
-}
-
void set_defaults(void)
{
// Set defaults:
set_current_music(LEVEL_MUSIC);
}
-void add_score(float x, float y, int s)
-{
- score += s;
-
- floating_score_type new_floating_score;
- floating_score_init(&new_floating_score,x,y,s);
- floating_scores.push_back(new_floating_score);
-}
-
-void add_bouncy_distro(float x, float y)
-{
-
- bouncy_distro_type new_bouncy_distro;
- bouncy_distro_init(&new_bouncy_distro,x,y);
- bouncy_distros.push_back(new_bouncy_distro);
-}
-
-void add_broken_brick(float x, float y)
-{
- add_broken_brick_piece(x, y, -1, -4);
- add_broken_brick_piece(x, y + 16, -1.5, -3);
-
- add_broken_brick_piece(x + 16, y, 1, -4);
- add_broken_brick_piece(x + 16, y + 16, 1.5, -3);
-}
-
-void add_broken_brick_piece(float x, float y, float xm, float ym)
-{
- broken_brick_type new_broken_brick;
- broken_brick_init(&new_broken_brick,x,y,xm,ym);
- broken_bricks.push_back(new_broken_brick);
-}
-
-void add_bouncy_brick(float x, float y)
-{
- bouncy_brick_type new_bouncy_brick;
- bouncy_brick_init(&new_bouncy_brick,x,y);
- bouncy_bricks.push_back(new_bouncy_brick);
-}
-
-void add_bad_guy(float x, float y, BadGuyKind kind)
-{
- bad_guys.push_back(BadGuy());
- BadGuy& new_bad_guy = bad_guys.back();
-
- new_bad_guy.init(x,y,kind);
-}
-
-void add_upgrade(float x, float y, int dir, int kind)
-{
- upgrade_type new_upgrade;
- upgrade_init(&new_upgrade,x,y,dir,kind);
- upgrades.push_back(new_upgrade);
-}
-
-void add_bullet(float x, float y, float xm, int dir)
-{
- bullet_type new_bullet;
- bullet_init(&new_bullet,x,y,xm,dir);
- bullets.push_back(new_bullet);
-
- play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER);
-}
-
// EOF //
extern timer_type super_bkgd_timer;
extern float scroll_x;
extern unsigned int global_frame_counter;
-extern std::vector<bouncy_distro_type> bouncy_distros;
-extern std::vector<broken_brick_type> broken_bricks;
-extern std::vector<bouncy_brick_type> bouncy_bricks;
-extern std::vector<BadGuy> bad_guys;
-extern std::vector<floating_score_type> floating_scores;
-extern std::vector<upgrade_type> upgrades;
-extern std::vector<bullet_type> bullets;
-extern std::vector<ParticleSystem*> particle_systems;
+
extern Player tux;
extern texture_type img_box_full, img_box_empty, img_mints, img_coffee, img_super_bkgd, img_red_glow;
extern timer_type time_left;
extern double frame_ratio;
-void add_score(float x, float y, int s);
void set_defaults(void);
-void arrays_free(void);
-
-void add_bouncy_distro(float x, float y);
-void add_broken_brick(float x, float y);
-void add_broken_brick_piece(float x, float y, float xm, float ym);
-void add_bouncy_brick(float x, float y);
-void add_bad_guy(float x, float y, BadGuyKind kind);
-void add_upgrade(float x, float y, int dir, int kind);
-void add_bullet(float x, float y, float xm, int dir);
#endif /*SUPERTUX_SCENE_H*/
issolid(pbullet->base.x + 4, pbullet->base.y + 2) ||
issolid(pbullet->base.x, pbullet->base.y + 2))
{
- bullets.erase(static_cast<std::vector<bullet_type>::iterator>(pbullet));
+ world.bullets.erase(static_cast<std::vector<bullet_type>::iterator>(pbullet));
}
}
{
if(c_object == CO_BADGUY) {
std::vector<bullet_type>::iterator i;
- for(i = bullets.begin(); i != bullets.end(); ++i) {
+
+ for(i = world.bullets.begin(); i != world.bullets.end(); ++i) {
if(& (*i) == pbullet) {
- bullets.erase(i);
+ world.bullets.erase(i);
return;
}
}
/* Off the screen? Kill it! */
if (pupgrade->base.x < scroll_x - pupgrade->base.width)
- upgrades.erase(static_cast<std::vector<upgrade_type>::iterator>(pupgrade));
+ world.upgrades.erase(static_cast<std::vector<upgrade_type>::iterator>(pupgrade));
if (pupgrade->base.y > screen->h)
- upgrades.erase(static_cast<std::vector<upgrade_type>::iterator>(pupgrade));
+ world.upgrades.erase(static_cast<std::vector<upgrade_type>::iterator>(pupgrade));
if (issolid(pupgrade->base.x + 1, pupgrade->base.y + 32.) ||
issolid(pupgrade->base.x + 31., pupgrade->base.y + 32.))
/* p_c_object is CO_PLAYER, so assign it to pplayer */
pplayer = (Player*) p_c_object;
- upgrades.erase(static_cast<std::vector<upgrade_type>::iterator>(pupgrade));
+ world.upgrades.erase(static_cast<std::vector<upgrade_type>::iterator>(pupgrade));
/* Affect the player: */
texture_type img_distro[4];
+World world;
+
+World::World()
+{
+
+}
+
+void
+World::arrays_free(void)
+{
+ bad_guys.clear();
+ bouncy_distros.clear();
+ broken_bricks.clear();
+ bouncy_bricks.clear();
+ floating_scores.clear();
+ upgrades.clear();
+ bullets.clear();
+ std::vector<ParticleSystem*>::iterator i;
+ for(i = particle_systems.begin(); i != particle_systems.end(); ++i) {
+ delete *i;
+ }
+ particle_systems.clear();
+}
+
+void
+World::draw()
+{
+ /* (Bouncy bricks): */
+ for (unsigned int i = 0; i < bouncy_bricks.size(); ++i)
+ bouncy_brick_draw(&bouncy_bricks[i]);
+
+ for (unsigned int i = 0; i < bad_guys.size(); ++i)
+ bad_guys[i].draw();
+
+ tux.draw();
+
+ for (unsigned int i = 0; i < bullets.size(); ++i)
+ bullet_draw(&bullets[i]);
+
+ for (unsigned int i = 0; i < floating_scores.size(); ++i)
+ floating_score_draw(&floating_scores[i]);
+
+ for (unsigned int i = 0; i < upgrades.size(); ++i)
+ upgrade_draw(&upgrades[i]);
+
+ for (unsigned int i = 0; i < bouncy_distros.size(); ++i)
+ bouncy_distro_draw(&bouncy_distros[i]);
+}
+
+void
+World::action()
+{
+ /* Handle bouncy distros: */
+ for (unsigned int i = 0; i < bouncy_distros.size(); i++)
+ bouncy_distro_action(&bouncy_distros[i]);
+
+ /* Handle broken bricks: */
+ for (unsigned int i = 0; i < broken_bricks.size(); i++)
+ broken_brick_action(&broken_bricks[i]);
+
+ /* Handle distro counting: */
+ if (counting_distros)
+ {
+ distro_counter--;
+
+ if (distro_counter <= 0)
+ counting_distros = -1;
+ }
+
+ // Handle all kinds of game objects
+ for (unsigned int i = 0; i < bouncy_bricks.size(); i++)
+ bouncy_brick_action(&bouncy_bricks[i]);
+
+ for (unsigned int i = 0; i < floating_scores.size(); i++)
+ floating_score_action(&floating_scores[i]);
+
+ for (unsigned int i = 0; i < bullets.size(); ++i)
+ bullet_action(&bullets[i]);
+
+ for (unsigned int i = 0; i < upgrades.size(); i++)
+ upgrade_action(&upgrades[i]);
+
+ for (unsigned int i = 0; i < bad_guys.size(); i++)
+ bad_guys[i].action();
+}
+
+void
+World::add_score(float x, float y, int s)
+{
+ score += s;
+
+ floating_score_type new_floating_score;
+ floating_score_init(&new_floating_score,x,y,s);
+ floating_scores.push_back(new_floating_score);
+}
+
+void
+World::add_bouncy_distro(float x, float y)
+{
+ bouncy_distro_type new_bouncy_distro;
+ bouncy_distro_init(&new_bouncy_distro,x,y);
+ bouncy_distros.push_back(new_bouncy_distro);
+}
+
+void
+World::add_broken_brick(float x, float y)
+{
+ add_broken_brick_piece(x, y, -1, -4);
+ add_broken_brick_piece(x, y + 16, -1.5, -3);
+
+ add_broken_brick_piece(x + 16, y, 1, -4);
+ add_broken_brick_piece(x + 16, y + 16, 1.5, -3);
+}
+
+void
+World::add_broken_brick_piece(float x, float y, float xm, float ym)
+{
+ broken_brick_type new_broken_brick;
+ broken_brick_init(&new_broken_brick,x,y,xm,ym);
+ broken_bricks.push_back(new_broken_brick);
+}
+
+void
+World::add_bouncy_brick(float x, float y)
+{
+ bouncy_brick_type new_bouncy_brick;
+ bouncy_brick_init(&new_bouncy_brick,x,y);
+ bouncy_bricks.push_back(new_bouncy_brick);
+}
+
+void
+World::add_bad_guy(float x, float y, BadGuyKind kind)
+{
+ bad_guys.push_back(BadGuy());
+ BadGuy& new_bad_guy = bad_guys.back();
+
+ new_bad_guy.init(x,y,kind);
+}
+
+void
+World::add_upgrade(float x, float y, int dir, int kind)
+{
+ upgrade_type new_upgrade;
+ upgrade_init(&new_upgrade,x,y,dir,kind);
+ upgrades.push_back(new_upgrade);
+}
+
+void
+World::add_bullet(float x, float y, float xm, int dir)
+{
+ bullet_type new_bullet;
+ bullet_init(&new_bullet,x,y,xm,dir);
+ bullets.push_back(new_bullet);
+
+ play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER);
+}
+
+
+
void bouncy_distro_init(bouncy_distro_type* pbouncy_distro, float x, float y)
{
pbouncy_distro->base.x = x;
pbouncy_distro->base.ym += 0.1 * frame_ratio;
if (pbouncy_distro->base.ym >= 0)
- bouncy_distros.erase(static_cast<std::vector<bouncy_distro_type>::iterator>(pbouncy_distro));
+ world.bouncy_distros.erase(static_cast<std::vector<bouncy_distro_type>::iterator>(pbouncy_distro));
}
void bouncy_distro_draw(bouncy_distro_type* pbouncy_distro)
pbroken_brick->base.y = pbroken_brick->base.y + pbroken_brick->base.ym * frame_ratio;
if (!timer_check(&pbroken_brick->timer))
- broken_bricks.erase(static_cast<std::vector<broken_brick_type>::iterator>(pbroken_brick));
+ world.broken_bricks.erase(static_cast<std::vector<broken_brick_type>::iterator>(pbroken_brick));
}
void broken_brick_draw(broken_brick_type* pbroken_brick)
/* Stop bouncing? */
if (pbouncy_brick->offset >= 0)
- bouncy_bricks.erase(static_cast<std::vector<bouncy_brick_type>::iterator>(pbouncy_brick));
+ world.bouncy_bricks.erase(static_cast<std::vector<bouncy_brick_type>::iterator>(pbouncy_brick));
}
void bouncy_brick_draw(bouncy_brick_type* pbouncy_brick)
pfloating_score->base.y = pfloating_score->base.y - 2 * frame_ratio;
if(!timer_check(&pfloating_score->timer))
- floating_scores.erase(static_cast<std::vector<floating_score_type>::iterator>(pfloating_score));
+ world.floating_scores.erase(static_cast<std::vector<floating_score_type>::iterator>(pfloating_score));
}
void floating_score_draw(floating_score_type* pfloating_score)
if (tile->data > 0)
{
/* Get a distro from it: */
- add_bouncy_distro(((int)(x + 1) / 32) * 32,
- (int)(y / 32) * 32);
+ world.add_bouncy_distro(((int)(x + 1) / 32) * 32,
+ (int)(y / 32) * 32);
if (!counting_distros)
{
plevel->change(x, y, TM_IA, tile->next_tile);
/* Replace it with broken bits: */
- add_broken_brick(((int)(x + 1) / 32) * 32,
+ world.add_broken_brick(((int)(x + 1) / 32) * 32,
(int)(y / 32) * 32);
/* Get some score: */
switch(tile->data)
{
case 1: //'A': /* Box with a distro! */
- add_bouncy_distro(((int)(x + 1) / 32) * 32, (int)(y / 32) * 32 - 32);
+ world.add_bouncy_distro(((int)(x + 1) / 32) * 32, (int)(y / 32) * 32 - 32);
play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
score = score + SCORE_DISTRO;
distros++;
case 2: // 'B': /* Add an upgrade! */
if (tux.size == SMALL) /* Tux is small, add mints! */
- add_upgrade((int)((x + 1) / 32) * 32, (int)(y / 32) * 32 - 32, col_side, UPGRADE_MINTS);
+ world.add_upgrade((int)((x + 1) / 32) * 32, (int)(y / 32) * 32 - 32, col_side, UPGRADE_MINTS);
else /* Tux is big, add coffee: */
- add_upgrade((int)((x + 1) / 32) * 32, (int)(y / 32) * 32 - 32, col_side, UPGRADE_COFFEE);
+ world.add_upgrade((int)((x + 1) / 32) * 32, (int)(y / 32) * 32 - 32, col_side, UPGRADE_COFFEE);
play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
break;
case 3:// '!': /* Add a golden herring */
- add_upgrade((int)((x + 1) / 32) * 32, (int)(y / 32) * 32 - 32, col_side, UPGRADE_HERRING);
+ world.add_upgrade((int)((x + 1) / 32) * 32, (int)(y / 32) * 32 - 32, col_side, UPGRADE_HERRING);
break;
default:
break;
if (bounciness == BOUNCE)
{
- add_bouncy_distro(((int)(x + 1) / 32) * 32,
- (int)(y / 32) * 32);
+ world.add_bouncy_distro(((int)(x + 1) / 32) * 32,
+ (int)(y / 32) * 32);
}
score = score + SCORE_DISTRO;
void trybumpbadguy(float x, float y)
{
/* Bad guys: */
- for (unsigned int i = 0; i < bad_guys.size(); i++)
+ for (unsigned int i = 0; i < world.bad_guys.size(); i++)
{
- if (bad_guys[i].base.x >= x - 32 && bad_guys[i].base.x <= x + 32 &&
- bad_guys[i].base.y >= y - 16 && bad_guys[i].base.y <= y + 16)
+ if (world.bad_guys[i].base.x >= x - 32 && world.bad_guys[i].base.x <= x + 32 &&
+ world.bad_guys[i].base.y >= y - 16 && world.bad_guys[i].base.y <= y + 16)
{
- bad_guys[i].collision(&tux, CO_PLAYER, COLLISION_BUMP);
+ world.bad_guys[i].collision(&tux, CO_PLAYER, COLLISION_BUMP);
}
}
/* Upgrades: */
- for (unsigned int i = 0; i < upgrades.size(); i++)
+ for (unsigned int i = 0; i < world.upgrades.size(); i++)
{
- if (upgrades[i].base.height == 32 &&
- upgrades[i].base.x >= x - 32 && upgrades[i].base.x <= x + 32 &&
- upgrades[i].base.y >= y - 16 && upgrades[i].base.y <= y + 16)
+ if (world.upgrades[i].base.height == 32 &&
+ world.upgrades[i].base.x >= x - 32 && world.upgrades[i].base.x <= x + 32 &&
+ world.upgrades[i].base.y >= y - 16 && world.upgrades[i].base.y <= y + 16)
{
- upgrades[i].base.xm = -upgrades[i].base.xm;
- upgrades[i].base.ym = -8;
+ world.upgrades[i].base.xm = -world.upgrades[i].base.xm;
+ world.upgrades[i].base.ym = -8;
play_sound(sounds[SND_BUMP_UPGRADE], SOUND_CENTER_SPEAKER);
}
}
#ifndef SUPERTUX_WORLD_H
#define SUPERTUX_WORLD_H
+#include <vector>
#include <SDL.h>
#include "type.h"
+#include "scene.h"
+#include "special.h"
+#include "particlesystem.h"
/* Bounciness of distros: */
void floating_score_action(floating_score_type* pfloating_score);
void floating_score_draw(floating_score_type* pfloating_score);
-
/** Try to grab the coin at the given coordinates */
void trygrabdistro(float x, float y, int bounciness);
the tile which the badguy is walking on an killing him this way */
void trybumpbadguy(float x, float y);
+
+/** The World class holds a level and all the game objects (badguys,
+ bouncy distros, etc) that are needed to run a game. */
+class World
+{
+ public:
+ Level* level;
+ std::vector<bouncy_distro_type> bouncy_distros;
+ std::vector<broken_brick_type> broken_bricks;
+ std::vector<bouncy_brick_type> bouncy_bricks;
+ std::vector<BadGuy> bad_guys;
+ std::vector<floating_score_type> floating_scores;
+ std::vector<upgrade_type> upgrades;
+ std::vector<bullet_type> bullets;
+ std::vector<ParticleSystem*> particle_systems;
+
+ public:
+ World();
+ void draw();
+ void action();
+ void arrays_free();
+
+ void add_score(float x, float y, int s);
+ void add_bouncy_distro(float x, float y);
+ void add_broken_brick(float x, float y);
+ void add_broken_brick_piece(float x, float y, float xm, float ym);
+ void add_bouncy_brick(float x, float y);
+ void add_bad_guy(float x, float y, BadGuyKind kind);
+ void add_upgrade(float x, float y, int dir, int kind);
+ void add_bullet(float x, float y, float xm, int dir);
+};
+
+extern World world;
+
#endif /*SUPERTUX_WORLD_H*/