From 6923b0b0fb10c3bcb17f03d7bbe159d034f07db2 Mon Sep 17 00:00:00 2001 From: Ingo Ruhnke Date: Sat, 10 Apr 2004 22:37:13 +0000 Subject: [PATCH] - moved stuff from scene into a World class, just an intermediate step, more cleanup will follow SVN-Revision: 461 --- src/badguy.cpp | 99 ++++++++++++------------- src/collision.cpp | 45 ++++++------ src/gameloop.cpp | 91 +++++------------------ src/gameloop.h | 3 + src/leveleditor.cpp | 40 +++++----- src/player.cpp | 8 +- src/scene.cpp | 88 ---------------------- src/scene.h | 19 +---- src/special.cpp | 13 ++-- src/world.cpp | 205 ++++++++++++++++++++++++++++++++++++++++++++++------ src/world.h | 39 +++++++++- 11 files changed, 347 insertions(+), 303 deletions(-) diff --git a/src/badguy.cpp b/src/badguy.cpp index 8aefbff35..4b2b7db73 100644 --- a/src/badguy.cpp +++ b/src/badguy.cpp @@ -372,13 +372,14 @@ BadGuy::fall() void BadGuy::remove_me() { - std::vector::iterator i; - for(i = bad_guys.begin(); i != bad_guys.end(); ++i) { - if( & (*i) == this) { - bad_guys.erase(i); - return; + for(std::vector::iterator i = world.bad_guys.begin(); + i != world.bad_guys.end(); ++i) + { + if( & (*i) == this) { + world.bad_guys.erase(i); + return; + } } - } } void @@ -783,7 +784,7 @@ BadGuy::squish_me(Player* player) { 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++; @@ -796,58 +797,58 @@ void 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... @@ -885,11 +886,11 @@ BadGuy::kill_me() /* 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); diff --git a/src/collision.cpp b/src/collision.cpp index c87590376..8335367dd 100644 --- a/src/collision.cpp +++ b/src/collision.cpp @@ -204,42 +204,42 @@ void collision_swept_object_map(base_type* old, base_type* current) 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); } } } @@ -247,35 +247,36 @@ void collision_handler() 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); } } diff --git a/src/gameloop.cpp b/src/gameloop.cpp index fc3e73c77..27235f3ce 100644 --- a/src/gameloop.cpp +++ b/src/gameloop.cpp @@ -100,7 +100,7 @@ GameSession::GameSession(const std::string& subset, int levelnb, int mode) level = levelnb; /* Init the game: */ - arrays_free(); + world.arrays_free(); set_defaults(); strcpy(level_subset, subset.c_str()); @@ -174,7 +174,7 @@ void activate_bad_guys(Level* plevel) 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); } } @@ -183,11 +183,11 @@ GameSession::activate_particle_systems() { 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 != "") { @@ -388,7 +388,7 @@ GameSession::action() current_level.cleanup(); level_free_song(); unloadshared(); - arrays_free(); + world.arrays_free(); return(0); } tux.level_begin(); @@ -413,7 +413,7 @@ GameSession::action() current_level.cleanup(); level_free_song(); unloadshared(); - arrays_free(); + world.arrays_free(); return(0); } /* if (lives < 0) */ } @@ -435,7 +435,7 @@ GameSession::action() return 0; } - arrays_free(); + world.arrays_free(); activate_bad_guys(¤t_level); activate_particle_systems(); level_free_gfx(); @@ -451,42 +451,11 @@ GameSession::action() 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::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); } @@ -497,8 +466,6 @@ GameSession::action() return -1; } -/* --- GAME DRAW! --- */ - void GameSession::draw() { @@ -524,13 +491,12 @@ GameSession::draw() /* Draw particle systems (background) */ std::vector::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) @@ -549,30 +515,11 @@ GameSession::draw() 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) @@ -585,7 +532,7 @@ GameSession::draw() } /* 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); } @@ -799,7 +746,7 @@ GameSession::run() current_level.cleanup(); level_free_song(); unloadshared(); - arrays_free(); + world.arrays_free(); game_started = false; @@ -1188,8 +1135,8 @@ void drawshape(float x, float y, unsigned int c, Uint8 alpha) /* 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); } @@ -1345,7 +1292,7 @@ GameSession::loadgame(int slot) 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(); diff --git a/src/gameloop.h b/src/gameloop.h index c0dfe0756..d7edeae2f 100644 --- a/src/gameloop.h +++ b/src/gameloop.h @@ -26,6 +26,9 @@ 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: diff --git a/src/leveleditor.cpp b/src/leveleditor.cpp index 5f0fc8326..6bca703a9 100644 --- a/src/leveleditor.cpp +++ b/src/leveleditor.cpp @@ -242,7 +242,7 @@ int leveleditor(int levelnb) 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) @@ -273,7 +273,7 @@ int leveleditor(int levelnb) 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) @@ -542,7 +542,7 @@ void save_subset_settings_menu() 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) @@ -596,7 +596,7 @@ void le_quit(void) level_free_gfx(); le_current_level->cleanup(); unloadshared(); - arrays_free(); + world.arrays_free(); } } @@ -725,12 +725,12 @@ void le_drawlevel() } /* 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(); } @@ -1119,16 +1119,16 @@ void le_change(float x, float y, int tm, unsigned int c) 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::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::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: @@ -1159,10 +1159,10 @@ void le_change(float x, float y, int tm, unsigned int c) 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::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::iterator>(&world.bad_guys[i])); for(xx = x1; xx <= x2; xx++) for(yy = y1; yy <= y2; yy++) @@ -1170,11 +1170,11 @@ void le_change(float x, float y, int tm, unsigned int c) 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: @@ -1191,7 +1191,7 @@ void le_testlevel() session.run(); Menu::set_current(leveleditor_menu); - arrays_free(); + world.arrays_free(); le_current_level->load_gfx(); loadshared(); activate_bad_guys(le_current_level); diff --git a/src/player.cpp b/src/player.cpp index d6b09bd41..529fe3e51 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -478,7 +478,7 @@ Player::handle_input() 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); } @@ -827,9 +827,9 @@ Player::collision(void* p_c_object, int c_object) { 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); } } } diff --git a/src/scene.cpp b/src/scene.cpp index d9adbd8d9..b2a242111 100644 --- a/src/scene.cpp +++ b/src/scene.cpp @@ -27,14 +27,6 @@ timer_type super_bkgd_timer; float scroll_x; unsigned int global_frame_counter; -std::vector bouncy_distros; -std::vector broken_bricks; -std::vector bouncy_bricks; -std::vector bad_guys; -std::vector floating_scores; -std::vector upgrades; -std::vector bullets; -std::vector particle_systems; Player tux; texture_type img_box_full; texture_type img_box_empty; @@ -45,22 +37,6 @@ texture_type img_red_glow; 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::iterator i; - for(i = particle_systems.begin(); i != particle_systems.end(); ++i) { - delete *i; - } - particle_systems.clear(); -} - void set_defaults(void) { // Set defaults: @@ -78,69 +54,5 @@ void set_defaults(void) 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 // diff --git a/src/scene.h b/src/scene.h index 06a8bdfe4..c3e601886 100644 --- a/src/scene.h +++ b/src/scene.h @@ -38,29 +38,12 @@ extern int distro_counter; extern timer_type super_bkgd_timer; extern float scroll_x; extern unsigned int global_frame_counter; -extern std::vector bouncy_distros; -extern std::vector broken_bricks; -extern std::vector bouncy_bricks; -extern std::vector bad_guys; -extern std::vector floating_scores; -extern std::vector upgrades; -extern std::vector bullets; -extern std::vector 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*/ diff --git a/src/special.cpp b/src/special.cpp index 2e5a05a01..9070f8e5b 100644 --- a/src/special.cpp +++ b/src/special.cpp @@ -72,7 +72,7 @@ void bullet_action(bullet_type* pbullet) issolid(pbullet->base.x + 4, pbullet->base.y + 2) || issolid(pbullet->base.x, pbullet->base.y + 2)) { - bullets.erase(static_cast::iterator>(pbullet)); + world.bullets.erase(static_cast::iterator>(pbullet)); } } @@ -91,9 +91,10 @@ void bullet_collision(bullet_type* pbullet, int c_object) { if(c_object == CO_BADGUY) { std::vector::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; } } @@ -143,9 +144,9 @@ void upgrade_action(upgrade_type *pupgrade) /* Off the screen? Kill it! */ if (pupgrade->base.x < scroll_x - pupgrade->base.width) - upgrades.erase(static_cast::iterator>(pupgrade)); + world.upgrades.erase(static_cast::iterator>(pupgrade)); if (pupgrade->base.y > screen->h) - upgrades.erase(static_cast::iterator>(pupgrade)); + world.upgrades.erase(static_cast::iterator>(pupgrade)); if (issolid(pupgrade->base.x + 1, pupgrade->base.y + 32.) || issolid(pupgrade->base.x + 31., pupgrade->base.y + 32.)) @@ -233,7 +234,7 @@ void upgrade_collision(upgrade_type* pupgrade, void* p_c_object, int c_object) /* p_c_object is CO_PLAYER, so assign it to pplayer */ pplayer = (Player*) p_c_object; - upgrades.erase(static_cast::iterator>(pupgrade)); + world.upgrades.erase(static_cast::iterator>(pupgrade)); /* Affect the player: */ diff --git a/src/world.cpp b/src/world.cpp index f198e81bf..52337be44 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -21,6 +21,165 @@ 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::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; @@ -35,7 +194,7 @@ void bouncy_distro_action(bouncy_distro_type* pbouncy_distro) pbouncy_distro->base.ym += 0.1 * frame_ratio; if (pbouncy_distro->base.ym >= 0) - bouncy_distros.erase(static_cast::iterator>(pbouncy_distro)); + world.bouncy_distros.erase(static_cast::iterator>(pbouncy_distro)); } void bouncy_distro_draw(bouncy_distro_type* pbouncy_distro) @@ -61,7 +220,7 @@ void broken_brick_action(broken_brick_type* pbroken_brick) 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::iterator>(pbroken_brick)); + world.broken_bricks.erase(static_cast::iterator>(pbroken_brick)); } void broken_brick_draw(broken_brick_type* pbroken_brick) @@ -104,7 +263,7 @@ void bouncy_brick_action(bouncy_brick_type* pbouncy_brick) /* Stop bouncing? */ if (pbouncy_brick->offset >= 0) - bouncy_bricks.erase(static_cast::iterator>(pbouncy_brick)); + world.bouncy_bricks.erase(static_cast::iterator>(pbouncy_brick)); } void bouncy_brick_draw(bouncy_brick_type* pbouncy_brick) @@ -156,7 +315,7 @@ void floating_score_action(floating_score_type* pfloating_score) pfloating_score->base.y = pfloating_score->base.y - 2 * frame_ratio; if(!timer_check(&pfloating_score->timer)) - floating_scores.erase(static_cast::iterator>(pfloating_score)); + world.floating_scores.erase(static_cast::iterator>(pfloating_score)); } void floating_score_draw(floating_score_type* pfloating_score) @@ -177,8 +336,8 @@ void trybreakbrick(float x, float y, bool small) 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) { @@ -199,7 +358,7 @@ void trybreakbrick(float x, float y, bool small) 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: */ @@ -227,7 +386,7 @@ void tryemptybox(float x, float y, int col_side) 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++; @@ -235,14 +394,14 @@ void tryemptybox(float x, float y, int col_side) 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; @@ -264,8 +423,8 @@ void trygrabdistro(float x, float y, int bounciness) 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; @@ -277,25 +436,25 @@ void trygrabdistro(float x, float y, int bounciness) 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); } } diff --git a/src/world.h b/src/world.h index ce285d73a..0beb81db7 100644 --- a/src/world.h +++ b/src/world.h @@ -13,8 +13,12 @@ #ifndef SUPERTUX_WORLD_H #define SUPERTUX_WORLD_H +#include #include #include "type.h" +#include "scene.h" +#include "special.h" +#include "particlesystem.h" /* Bounciness of distros: */ @@ -69,7 +73,6 @@ void floating_score_init(floating_score_type* pfloating_score, float x, float y, 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); @@ -83,5 +86,39 @@ void tryemptybox(float x, float y, int col_side); 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_distros; + std::vector broken_bricks; + std::vector bouncy_bricks; + std::vector bad_guys; + std::vector floating_scores; + std::vector upgrades; + std::vector bullets; + std::vector 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*/ -- 2.11.0