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;
}
bouncy_bricks[i].draw();
for (unsigned int i = 0; i < bad_guys.size(); ++i)
- bad_guys[i].draw();
+ bad_guys[i]->draw();
tux.draw();
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<ParticleSystem*>::iterator p;
{
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
}
/* 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);
}
}
}
// 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);
}
}
}
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<BadGuy*>::iterator i = bad_guys.begin();
+ i != bad_guys.end(); ++i)
+ {
+ if(*i == badguy)
+ {
+ World::current()->bad_guys.erase(i);
+ delete badguy;
+ return;
+ }
+ }
}
void
// 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);
}
}
bouncy distros, etc) that are needed to run a game. */
class World
{
- public:
+private:
+ std::vector<BadGuy*> bad_guys;
Level* level;
-
Player tux;
-
+
+ int distro_counter;
+ bool counting_distros;
+ int currentmusic;
+
+ static World* current_;
+public:
std::vector<BouncyDistro> bouncy_distros;
std::vector<BrokenBrick> broken_bricks;
std::vector<BouncyBrick> bouncy_bricks;
std::vector<FloatingScore> floating_scores;
- std::vector<BadGuy> bad_guys;
std::vector<Upgrade> upgrades;
std::vector<Bullet> bullets;
std::vector<ParticleSystem*> 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; }
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);