From e9e8840806ca81e2719def6baab03d342645bd04 Mon Sep 17 00:00:00 2001 From: Ingo Ruhnke Date: Thu, 22 Apr 2004 17:53:08 +0000 Subject: [PATCH] - made some arrays private - store badguys in pointers instead of per value SVN-Revision: 646 --- src/badguy.cpp | 9 +------- src/world.cpp | 65 ++++++++++++++++++++++++++++++++++++++-------------------- src/world.h | 26 ++++++++++++----------- 3 files changed, 58 insertions(+), 42 deletions(-) diff --git a/src/badguy.cpp b/src/badguy.cpp index 870ee4291..c09678b85 100644 --- a/src/badguy.cpp +++ b/src/badguy.cpp @@ -408,14 +408,7 @@ BadGuy::fall() void BadGuy::remove_me() { - for(std::vector::iterator i = World::current()->bad_guys.begin(); - i != World::current()->bad_guys.end(); ++i) - { - if( & (*i) == this) { - World::current()->bad_guys.erase(i); - return; - } - } + World::current()->remove_bad_guy(this); } void diff --git a/src/world.cpp b/src/world.cpp index 8cdee7667..27a06c249 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -72,6 +72,11 @@ World::World(const std::string& subset, int level_nr) World::~World() { + for (unsigned int i = 0; i < bad_guys.size(); ++i) + { + delete bad_guys[i]; + } + halt_music(); // just to be sure (because levelmusic is freed now) delete level; } @@ -168,7 +173,7 @@ World::draw() bouncy_bricks[i].draw(); for (unsigned int i = 0; i < bad_guys.size(); ++i) - bad_guys[i].draw(); + bad_guys[i]->draw(); tux.draw(); @@ -240,7 +245,7 @@ World::action(double frame_ratio) upgrades[i].action(frame_ratio); for (unsigned int i = 0; i < bad_guys.size(); i++) - bad_guys[i].action(frame_ratio); + bad_guys[i]->action(frame_ratio); /* update particle systems */ std::vector::iterator p; @@ -262,15 +267,15 @@ World::collision_handler() { for(unsigned int j = 0; j < bad_guys.size(); ++j) { - if(bad_guys[j].dying != DYING_NOT) + if(bad_guys[j]->dying != DYING_NOT) continue; - if(rectcollision(&bullets[i].base, &bad_guys[j].base)) + if(rectcollision(&bullets[i].base, &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); + bad_guys[j]->collision(0, CO_BULLET); bullets[i].collision(CO_BADGUY); break; // bullet is invalid now, so break } @@ -280,20 +285,20 @@ World::collision_handler() /* CO_BADGUY & CO_BADGUY check */ for(unsigned int i = 0; i < bad_guys.size(); ++i) { - if(bad_guys[i].dying != DYING_NOT) + if(bad_guys[i]->dying != DYING_NOT) continue; for(unsigned int j = i+1; j < bad_guys.size(); ++j) { - if(j == i || bad_guys[j].dying != DYING_NOT) + if(j == i || bad_guys[j]->dying != DYING_NOT) continue; - if(rectcollision(&bad_guys[i].base, &bad_guys[j].base)) + if(rectcollision(&bad_guys[i]->base, &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); + bad_guys[j]->collision(&bad_guys[i], CO_BADGUY); + bad_guys[i]->collision(&bad_guys[j], CO_BADGUY); } } } @@ -303,23 +308,23 @@ World::collision_handler() // CO_BADGUY & CO_PLAYER check for(unsigned int i = 0; i < bad_guys.size(); ++i) { - if(bad_guys[i].dying != DYING_NOT) + if(bad_guys[i]->dying != DYING_NOT) continue; - if(rectcollision_offset(&bad_guys[i].base,&tux.base,0,0)) + if(rectcollision_offset(&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) + < bad_guys[i]->base.y + bad_guys[i]->base.height/2) { - bad_guys[i].collision(&tux, CO_PLAYER, COLLISION_SQUISH); + bad_guys[i]->collision(&tux, CO_PLAYER, COLLISION_SQUISH); } else { tux.collision(&bad_guys[i], CO_BADGUY); - bad_guys[i].collision(&tux, CO_PLAYER, COLLISION_NORMAL); + bad_guys[i]->collision(&tux, CO_PLAYER, COLLISION_NORMAL); } } } @@ -380,13 +385,29 @@ World::add_bouncy_brick(float x, float y) bouncy_bricks.push_back(new_bouncy_brick); } -void +BadGuy* World::add_bad_guy(float x, float y, BadGuyKind kind, bool stay_on_platform) { - bad_guys.push_back(BadGuy()); - BadGuy& new_bad_guy = bad_guys.back(); + bad_guys.push_back(new BadGuy()); + BadGuy* new_bad_guy = bad_guys.back(); - new_bad_guy.init(x,y,kind, stay_on_platform); + new_bad_guy->init(x,y,kind, stay_on_platform); + return new_bad_guy; +} + +void +World::remove_bad_guy(BadGuy* badguy) +{ + for(std::vector::iterator i = bad_guys.begin(); + i != bad_guys.end(); ++i) + { + if(*i == badguy) + { + World::current()->bad_guys.erase(i); + delete badguy; + return; + } + } } void @@ -557,10 +578,10 @@ World::trybumpbadguy(float x, float y) // Bad guys: for (unsigned int i = 0; i < 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 (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) { - bad_guys[i].collision(&tux, CO_PLAYER, COLLISION_BUMP); + bad_guys[i]->collision(&tux, CO_PLAYER, COLLISION_BUMP); } } diff --git a/src/world.h b/src/world.h index ddbedd48f..d7b8e9712 100644 --- a/src/world.h +++ b/src/world.h @@ -37,28 +37,27 @@ class Level; bouncy distros, etc) that are needed to run a game. */ class World { - public: +private: + std::vector bad_guys; Level* level; - Player tux; - + + int distro_counter; + bool counting_distros; + int currentmusic; + + static World* current_; +public: std::vector bouncy_distros; std::vector broken_bricks; std::vector bouncy_bricks; std::vector floating_scores; - std::vector bad_guys; std::vector upgrades; std::vector bullets; std::vector particle_systems; - int distro_counter; - bool counting_distros; - - int currentmusic; - - static World* current_; - public: +public: static World* current() { return current_; } static void set_current(World* w) { current_ = w; } @@ -92,7 +91,10 @@ class World void add_broken_brick(Tile* tile, float x, float y); void add_broken_brick_piece(Tile* tile, 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, bool stay_on_platform = false); + + BadGuy* add_bad_guy(float x, float y, BadGuyKind kind, bool stay_on_platform = false); + void remove_bad_guy(BadGuy* badguy); + void add_upgrade(float x, float y, Direction dir, UpgradeKind kind); void add_bullet(float x, float y, float xm, Direction dir); -- 2.11.0