From: Ingo Ruhnke Date: Sun, 11 Apr 2004 13:20:43 +0000 (+0000) Subject: - turned special into classes X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=e40fdda30634b3b59a7d782b9d0642bf0209928c;p=supertux.git - turned special into classes SVN-Revision: 473 --- diff --git a/src/special.cpp b/src/special.cpp index 094df3bc3..b62a4b766 100644 --- a/src/special.cpp +++ b/src/special.cpp @@ -29,200 +29,210 @@ void create_special_bitmasks() bm_bullet = bitmask_create_SDL(img_bullet.sdl_surface); } -void bullet_init(bullet_type* pbullet, float x, float y, float xm, int dir) +void +Bullet::init(float x, float y, float xm, int dir) { - pbullet->base.width = 4; - pbullet->base.height = 4; + base.width = 4; + base.height = 4; if (dir == RIGHT) { - pbullet->base.x = x + 32; - pbullet->base.xm = BULLET_XM + xm; + base.x = x + 32; + base.xm = BULLET_XM + xm; } else { - pbullet->base.x = x; - pbullet->base.xm = -BULLET_XM + xm; + base.x = x; + base.xm = -BULLET_XM + xm; } - pbullet->base.y = y; - pbullet->base.ym = BULLET_STARTING_YM; - pbullet->old_base = pbullet->base; + base.y = y; + base.ym = BULLET_STARTING_YM; + old_base = base; } -void bullet_action(bullet_type* pbullet) +void +Bullet::action() { - pbullet->base.x = pbullet->base.x + pbullet->base.xm * frame_ratio; - pbullet->base.y = pbullet->base.y + pbullet->base.ym * frame_ratio; + base.x = base.x + base.xm * frame_ratio; + base.y = base.y + base.ym * frame_ratio; - collision_swept_object_map(&pbullet->old_base,&pbullet->base); + collision_swept_object_map(&old_base,&base); - if (issolid(pbullet->base.x, pbullet->base.y + 4) || issolid(pbullet->base.x, pbullet->base.y)) + if (issolid(base.x, base.y + 4) || issolid(base.x, base.y)) { - pbullet->base.ym = -pbullet->base.ym; - pbullet->base.y = (int)(pbullet->base.y / 32) * 32; + base.ym = -base.ym; + base.y = (int)(base.y / 32) * 32; } - pbullet->base.ym = pbullet->base.ym + GRAVITY; + base.ym = base.ym + GRAVITY; - if (pbullet->base.x < scroll_x || - pbullet->base.x > scroll_x + screen->w || - pbullet->base.y < 0 || - pbullet->base.y > screen->h || - issolid(pbullet->base.x + 4, pbullet->base.y + 2) || - issolid(pbullet->base.x, pbullet->base.y + 2)) + if (base.x < scroll_x || + base.x > scroll_x + screen->w || + base.y < 0 || + base.y > screen->h || + issolid(base.x + 4, base.y + 2) || + issolid(base.x, base.y + 2)) { - World::current()->bullets.erase(static_cast::iterator>(pbullet)); + World::current()->bullets.erase(static_cast::iterator>(this)); } } -void bullet_draw(bullet_type* pbullet) +void +Bullet::draw() { - if (pbullet->base.x >= scroll_x - pbullet->base.width && - pbullet->base.x <= scroll_x + screen->w) + if (base.x >= scroll_x - base.width && + base.x <= scroll_x + screen->w) { - texture_draw(&img_bullet, pbullet->base.x - scroll_x, pbullet->base.y, 255, + texture_draw(&img_bullet, base.x - scroll_x, base.y, 255, NO_UPDATE); } } -void bullet_collision(bullet_type* pbullet, int c_object) +void +Bullet::collision(int c_object) { if(c_object == CO_BADGUY) { - std::vector::iterator i; + std::vector::iterator i; - for(i = World::current()->bullets.begin(); i != World::current()->bullets.end(); ++i) { - if(& (*i) == pbullet) { - World::current()->bullets.erase(i); - return; + for(i = World::current()->bullets.begin(); i != World::current()->bullets.end(); ++i) + { + if(&(*i) == this) + { + World::current()->bullets.erase(i); + return; + } } - } } } -void upgrade_init(upgrade_type *pupgrade, float x, float y, int dir, int kind) +void +Upgrade::init(float x_, float y_, int dir_, int kind_) { - pupgrade->base.width = 32; - pupgrade->base.height = 0; - pupgrade->kind = kind; - pupgrade->base.x = x; - pupgrade->base.y = y; - if(dir == LEFT) - pupgrade->base.xm = -2; + base.width = 32; + base.height = 0; + kind = kind_; + base.x = x_; + base.y = y_; + + if(dir_ == LEFT) + base.xm = -2; else - pupgrade->base.xm = 2; - pupgrade->base.ym = -2; - pupgrade->base.height = 0; - pupgrade->old_base = pupgrade->base; + base.xm = 2; + + base.ym = -2; + base.height = 0; + old_base = base; } -void upgrade_action(upgrade_type *pupgrade) +void +Upgrade::action() { - - - if (pupgrade->base.height < 32) + if (base.height < 32) { /* Rise up! */ - pupgrade->base.height = pupgrade->base.height + 0.7 * frame_ratio; - if(pupgrade->base.height > 32) - pupgrade->base.height = 32; + base.height = base.height + 0.7 * frame_ratio; + if(base.height > 32) + base.height = 32; } else { /* Move around? */ - if (pupgrade->kind == UPGRADE_MINTS || - pupgrade->kind == UPGRADE_HERRING) + if (kind == UPGRADE_MINTS || + kind == UPGRADE_HERRING) { - pupgrade->base.x = pupgrade->base.x + pupgrade->base.xm * frame_ratio; - pupgrade->base.y = pupgrade->base.y + pupgrade->base.ym * frame_ratio; + base.x = base.x + base.xm * frame_ratio; + base.y = base.y + base.ym * frame_ratio; - collision_swept_object_map(&pupgrade->old_base,&pupgrade->base); + collision_swept_object_map(&old_base,&base); /* Off the screen? Kill it! */ - if (pupgrade->base.x < scroll_x - pupgrade->base.width) - World::current()->upgrades.erase(static_cast::iterator>(pupgrade)); - if (pupgrade->base.y > screen->h) - World::current()->upgrades.erase(static_cast::iterator>(pupgrade)); + if (base.x < scroll_x - base.width) + World::current()->upgrades.erase(static_cast::iterator>(this)); + if (base.y > screen->h) + World::current()->upgrades.erase(static_cast::iterator>(this)); - if (issolid(pupgrade->base.x + 1, pupgrade->base.y + 32.) || - issolid(pupgrade->base.x + 31., pupgrade->base.y + 32.)) + if (issolid(base.x + 1, base.y + 32.) || + issolid(base.x + 31., base.y + 32.)) { - if (pupgrade->base.ym > 0) + if (base.ym > 0) { - if (pupgrade->kind == UPGRADE_MINTS) + if (kind == UPGRADE_MINTS) { - pupgrade->base.ym = 0; + base.ym = 0; } - else if (pupgrade->kind == UPGRADE_HERRING) + else if (kind == UPGRADE_HERRING) { - pupgrade->base.ym = -8; + base.ym = -8; } - pupgrade->base.y = (int)(pupgrade->base.y / 32) * 32; + base.y = (int)(base.y / 32) * 32; } } else - pupgrade->base.ym = pupgrade->base.ym + GRAVITY * frame_ratio; + base.ym = base.ym + GRAVITY * frame_ratio; - if (issolid(pupgrade->base.x - 1, (int) pupgrade->base.y)) + if (issolid(base.x - 1, (int) base.y)) { - if(pupgrade->base.xm < 0) - pupgrade->base.xm = -pupgrade->base.xm; + if(base.xm < 0) + base.xm = -base.xm; } - else if (issolid(pupgrade->base.x + pupgrade->base.width, (int) pupgrade->base.y)) + else if (issolid(base.x + base.width, (int) base.y)) { - if(pupgrade->base.xm > 0) - pupgrade->base.xm = -pupgrade->base.xm; + if(base.xm > 0) + base.xm = -base.xm; } } } } -void upgrade_draw(upgrade_type* pupgrade) +void +Upgrade::draw() { SDL_Rect dest; - if (pupgrade->base.height < 32) + if (base.height < 32) { /* Rising up... */ - dest.x = (int)(pupgrade->base.x - scroll_x); - dest.y = (int)(pupgrade->base.y + 32 - pupgrade->base.height); + dest.x = (int)(base.x - scroll_x); + dest.y = (int)(base.y + 32 - base.height); dest.w = 32; - dest.h = (int)pupgrade->base.height; + dest.h = (int)base.height; - if (pupgrade->kind == UPGRADE_MINTS) + if (kind == UPGRADE_MINTS) texture_draw_part(&img_mints,0,0,dest.x,dest.y,dest.w,dest.h); - else if (pupgrade->kind == UPGRADE_COFFEE) + else if (kind == UPGRADE_COFFEE) texture_draw_part(&img_coffee,0,0,dest.x,dest.y,dest.w,dest.h); - else if (pupgrade->kind == UPGRADE_HERRING) + else if (kind == UPGRADE_HERRING) texture_draw_part(&img_golden_herring,0,0,dest.x,dest.y,dest.w,dest.h); } else { - if (pupgrade->kind == UPGRADE_MINTS) + if (kind == UPGRADE_MINTS) { texture_draw(&img_mints, - pupgrade->base.x - scroll_x, pupgrade->base.y); + base.x - scroll_x, base.y); } - else if (pupgrade->kind == UPGRADE_COFFEE) + else if (kind == UPGRADE_COFFEE) { texture_draw(&img_coffee, - pupgrade->base.x - scroll_x, pupgrade->base.y); + base.x - scroll_x, base.y); } - else if (pupgrade->kind == UPGRADE_HERRING) + else if (kind == UPGRADE_HERRING) { texture_draw(&img_golden_herring, - pupgrade->base.x - scroll_x, pupgrade->base.y); + base.x - scroll_x, base.y); } } } -void upgrade_collision(upgrade_type* pupgrade, void* p_c_object, int c_object) +void +Upgrade::collision(void* p_c_object, int c_object) { Player* pplayer = NULL; @@ -234,11 +244,11 @@ 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; - World::current()->upgrades.erase(static_cast::iterator>(pupgrade)); + World::current()->upgrades.erase(static_cast::iterator>(this)); /* Affect the player: */ - if (pupgrade->kind == UPGRADE_MINTS) + if (kind == UPGRADE_MINTS) { play_sound(sounds[SND_EXCELLENT], SOUND_CENTER_SPEAKER); pplayer->size = BIG; @@ -252,13 +262,13 @@ void upgrade_collision(upgrade_type* pupgrade, void* p_c_object, int c_object) } timer_start(&super_bkgd_timer, 350); } - else if (pupgrade->kind == UPGRADE_COFFEE) + else if (kind == UPGRADE_COFFEE) { play_sound(sounds[SND_COFFEE], SOUND_CENTER_SPEAKER); pplayer->got_coffee = true; timer_start(&super_bkgd_timer, 250); } - else if (pupgrade->kind == UPGRADE_HERRING) + else if (kind == UPGRADE_HERRING) { play_sound(sounds[SND_HERRING], SOUND_CENTER_SPEAKER); timer_start(&pplayer->invincible_timer,TUX_INVINCIBLE_TIME); diff --git a/src/special.h b/src/special.h index 2bdb6dd5c..c246bd8ef 100644 --- a/src/special.h +++ b/src/special.h @@ -31,17 +31,29 @@ enum { UPGRADE_HERRING }; -struct upgrade_type +class Upgrade { + public: int kind; base_type base; base_type old_base; + + void init(float x, float y, int dir, int kind); + void action(); + void draw(); + void collision(void* p_c_object, int c_object); }; -struct bullet_type +class Bullet { + public: base_type base; base_type old_base; + + void init(float x, float y, float xm, int dir); + void action(); + void draw(); + void collision(int c_object); }; extern texture_type img_bullet; @@ -51,14 +63,4 @@ void create_special_bitmasks(); extern texture_type img_golden_herring; -void upgrade_init(upgrade_type *pupgrade, float x, float y, int dir, int kind); -void upgrade_action(upgrade_type *pupgrade); -void upgrade_draw(upgrade_type *pupgrade); -void upgrade_collision(upgrade_type *pupgrade, void* p_c_object, int c_object); - -void bullet_init(bullet_type* pbullet, float x, float y, float xm, int dir); -void bullet_action(bullet_type *pbullet); -void bullet_draw(bullet_type *pbullet); -void bullet_collision(bullet_type *pbullet, int c_object); - #endif /*SUPERTUX_SPECIAL_H*/ diff --git a/src/world.cpp b/src/world.cpp index 234b062c7..0e9220087 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -183,13 +183,13 @@ World::draw() tux.draw(); for (unsigned int i = 0; i < bullets.size(); ++i) - bullet_draw(&bullets[i]); + bullets[i].draw(); for (unsigned int i = 0; i < floating_scores.size(); ++i) floating_scores[i].draw(); for (unsigned int i = 0; i < upgrades.size(); ++i) - upgrade_draw(&upgrades[i]); + upgrades[i].draw(); for (unsigned int i = 0; i < bouncy_distros.size(); ++i) bouncy_distros[i].draw(); @@ -242,10 +242,10 @@ World::action() floating_scores[i].action(); for (unsigned int i = 0; i < bullets.size(); ++i) - bullet_action(&bullets[i]); + bullets[i].action(); for (unsigned int i = 0; i < upgrades.size(); i++) - upgrade_action(&upgrades[i]); + upgrades[i].action(); for (unsigned int i = 0; i < bad_guys.size(); i++) bad_guys[i].action(); @@ -279,7 +279,7 @@ World::collision_handler() // 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); + bullets[i].collision(CO_BADGUY); break; // bullet is invalid now, so break } } @@ -338,7 +338,7 @@ World::collision_handler() { // We have detected a collision and now call the collision // functions of the collided objects. - upgrade_collision(&upgrades[i], &tux, CO_PLAYER); + upgrades[i].collision(&tux, CO_PLAYER); } } } @@ -399,16 +399,16 @@ World::add_bad_guy(float x, float y, BadGuyKind 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); + Upgrade new_upgrade; + new_upgrade.init(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); + Bullet new_bullet; + new_bullet.init(x,y,xm,dir); bullets.push_back(new_bullet); play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER); diff --git a/src/world.h b/src/world.h index e655e4620..a29ccbb05 100644 --- a/src/world.h +++ b/src/world.h @@ -41,8 +41,8 @@ class World std::vector floating_scores; std::vector bad_guys; - std::vector upgrades; - std::vector bullets; + std::vector upgrades; + std::vector bullets; std::vector particle_systems; static World* current_;