From: Christoph Sommer Date: Mon, 10 Jul 2006 01:56:01 +0000 (+0000) Subject: Testing new base class "WalkingBadguy" X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=ff59adfe9b44e1447ae02a2f9705d695a280059e;p=supertux.git Testing new base class "WalkingBadguy" SVN-Revision: 3976 --- diff --git a/src/badguy/mrbomb.cpp b/src/badguy/mrbomb.cpp index 78cb5b726..61bc9d95e 100644 --- a/src/badguy/mrbomb.cpp +++ b/src/badguy/mrbomb.cpp @@ -23,11 +23,12 @@ #include "bomb.hpp" #include "sprite/sprite_manager.hpp" -static const float WALKSPEED = 80; - MrBomb::MrBomb(const lisp::Lisp& reader) - : BadGuy(reader, "images/creatures/mr_cherry/mr_cherry.sprite") + : WalkingBadguy(reader, "images/creatures/mr_cherry/mr_cherry.sprite", "left", "right") { + walk_speed = 80; + max_drop_height = 0; + //Check if we need another sprite if( !reader.get( "sprite", sprite_name ) ){ return; @@ -42,42 +43,20 @@ MrBomb::MrBomb(const lisp::Lisp& reader) /* MrBomb created by a despencer always gets default sprite atm.*/ MrBomb::MrBomb(const Vector& pos, Direction d) - : BadGuy(pos, d, "images/creatures/mr_cherry/mr_cherry.sprite") + : WalkingBadguy(pos, d, "images/creatures/mr_cherry/mr_cherry.sprite", "left", "right") { + walk_speed = 80; + max_drop_height = 0; } void MrBomb::write(lisp::Writer& writer) { writer.start_list("mrbomb"); - - writer.write_float("x", start_position.x); - writer.write_float("y", start_position.y); - + WalkingBadguy::write(writer); writer.end_list("mrbomb"); } -void -MrBomb::activate() -{ - physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED); - sprite->set_action(dir == LEFT ? "left" : "right"); - bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height()); -} - -void -MrBomb::active_update(float elapsed_time) -{ - if (on_ground() && might_fall()) - { - dir = (dir == LEFT ? RIGHT : LEFT); - sprite->set_action(dir == LEFT ? "left" : "right"); - physic.set_velocity_x(-physic.get_velocity_x()); - } - - BadGuy::active_update(elapsed_time); -} - bool MrBomb::collision_squished(Player& player) { @@ -88,32 +67,6 @@ MrBomb::collision_squished(Player& player) } void -MrBomb::collision_solid(const CollisionHit& hit) -{ - update_on_ground_flag(hit); - - if(hit.bottom || hit.top) { - physic.set_velocity_y(0); - } - if(hit.left || hit.right) { - dir = dir == LEFT ? RIGHT : LEFT; - sprite->set_action(dir == LEFT ? "left" : "right"); - physic.set_velocity_x(-physic.get_velocity_x()); - } -} - -HitResponse -MrBomb::collision_badguy(BadGuy&, const CollisionHit& hit ) -{ - if(hit.left || hit.right) { - dir = (dir == LEFT) ? RIGHT : LEFT; - sprite->set_action(dir == LEFT ? "left" : "right"); - physic.set_velocity_x(-physic.get_velocity_x()); - } - return CONTINUE; -} - -void MrBomb::kill_fall() { remove_me(); diff --git a/src/badguy/mrbomb.hpp b/src/badguy/mrbomb.hpp index 450d0d11f..63c436493 100644 --- a/src/badguy/mrbomb.hpp +++ b/src/badguy/mrbomb.hpp @@ -20,19 +20,15 @@ #ifndef __MRBOMB_H__ #define __MRBOMB_H__ -#include "badguy.hpp" +#include "walking_badguy.hpp" -class MrBomb : public BadGuy +class MrBomb : public WalkingBadguy { public: MrBomb(const lisp::Lisp& reader); MrBomb(const Vector& pos, Direction d); - void activate(); - void active_update(float elapsed_time); void write(lisp::Writer& writer); - void collision_solid(const CollisionHit& hit); - HitResponse collision_badguy(BadGuy& badguy, const CollisionHit& hit); void kill_fall(); virtual MrBomb* clone() const { return new MrBomb(*this); } diff --git a/src/badguy/mriceblock.cpp b/src/badguy/mriceblock.cpp index 09f2bb096..8b3a995f4 100644 --- a/src/badguy/mriceblock.cpp +++ b/src/badguy/mriceblock.cpp @@ -23,22 +23,25 @@ #include "object/block.hpp" namespace { - const float WALKSPEED = 80; const float KICKSPEED = 500; const int MAXSQUISHES = 10; } MrIceBlock::MrIceBlock(const lisp::Lisp& reader) - : BadGuy(reader, "images/creatures/mr_iceblock/mr_iceblock.sprite"), ice_state(ICESTATE_NORMAL), squishcount(0) + : WalkingBadguy(reader, "images/creatures/mr_iceblock/mr_iceblock.sprite", "left", "right"), ice_state(ICESTATE_NORMAL), squishcount(0) { + walk_speed = 80; + max_drop_height = 600; sound_manager->preload("sounds/iceblock_bump.wav"); sound_manager->preload("sounds/stomp.wav"); sound_manager->preload("sounds/kick.wav"); } MrIceBlock::MrIceBlock(const Vector& pos, Direction d) - : BadGuy(pos, d, "images/creatures/mr_iceblock/mr_iceblock.sprite"), ice_state(ICESTATE_NORMAL), squishcount(0) + : WalkingBadguy(pos, d, "images/creatures/mr_iceblock/mr_iceblock.sprite", "left", "right"), ice_state(ICESTATE_NORMAL), squishcount(0) { + walk_speed = 80; + max_drop_height = 600; sound_manager->preload("sounds/iceblock_bump.wav"); sound_manager->preload("sounds/stomp.wav"); sound_manager->preload("sounds/kick.wav"); @@ -48,18 +51,14 @@ void MrIceBlock::write(lisp::Writer& writer) { writer.start_list("mriceblock"); - - writer.write_float("x", start_position.x); - writer.write_float("y", start_position.y); - + WalkingBadguy::write(writer); writer.end_list("mriceblock"); } void MrIceBlock::activate() { - physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED); - sprite->set_action(dir == LEFT ? "left" : "right"); + WalkingBadguy::activate(); set_state(ICESTATE_NORMAL); } @@ -73,11 +72,10 @@ MrIceBlock::active_update(float elapsed_time) set_state(ICESTATE_NORMAL); } - if (ice_state == ICESTATE_NORMAL && on_ground() && might_fall(601)) + if (ice_state == ICESTATE_NORMAL) { - dir = (dir == LEFT ? RIGHT : LEFT); - sprite->set_action(dir == LEFT ? "left" : "right"); - physic.set_velocity_x(-physic.get_velocity_x()); + WalkingBadguy::active_update(elapsed_time); + return; } BadGuy::active_update(elapsed_time); @@ -96,15 +94,7 @@ MrIceBlock::collision_solid(const CollisionHit& hit) // hit left or right switch(ice_state) { case ICESTATE_NORMAL: - if(hit.right && dir == RIGHT) { - dir = LEFT; - sprite->set_action(dir == LEFT ? "left" : "right"); - physic.set_velocity_x(-physic.get_velocity_x()); - } else if(hit.left && dir == LEFT) { - dir = RIGHT; - sprite->set_action(dir == LEFT ? "left" : "right"); - physic.set_velocity_x(-physic.get_velocity_x()); - } + WalkingBadguy::collision_solid(hit); break; case ICESTATE_KICKED: { #if 0 @@ -176,12 +166,7 @@ MrIceBlock::collision_badguy(BadGuy& badguy, const CollisionHit& hit) { switch(ice_state) { case ICESTATE_NORMAL: - if(hit.left || hit.right) { - dir = dir == LEFT ? RIGHT : LEFT; - sprite->set_action(dir == LEFT ? "left" : "right"); - physic.set_velocity_x(-physic.get_velocity_x()); - } - return CONTINUE; + return WalkingBadguy::collision_badguy(badguy, hit); case ICESTATE_FLAT: return FORCE_MOVE; case ICESTATE_KICKED: @@ -238,8 +223,7 @@ MrIceBlock::set_state(IceState state) switch(state) { case ICESTATE_NORMAL: - physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED); - sprite->set_action(dir == LEFT ? "left" : "right"); + WalkingBadguy::activate(); break; case ICESTATE_FLAT: sound_manager->play("sounds/stomp.wav", get_pos()); diff --git a/src/badguy/mriceblock.hpp b/src/badguy/mriceblock.hpp index 01e693a65..4f95bd95d 100644 --- a/src/badguy/mriceblock.hpp +++ b/src/badguy/mriceblock.hpp @@ -20,10 +20,10 @@ #ifndef __MRICEBLOCK_H__ #define __MRICEBLOCK_H__ -#include "badguy.hpp" +#include "walking_badguy.hpp" #include "object/portable.hpp" -class MrIceBlock : public BadGuy, public Portable +class MrIceBlock : public WalkingBadguy, public Portable { public: MrIceBlock(const lisp::Lisp& reader); diff --git a/src/badguy/poisonivy.cpp b/src/badguy/poisonivy.cpp index 34d4ed897..e881933dc 100644 --- a/src/badguy/poisonivy.cpp +++ b/src/badguy/poisonivy.cpp @@ -23,78 +23,44 @@ #include "random_generator.hpp" #include "object/sprite_particle.hpp" -static const float WALKSPEED = 80; - PoisonIvy::PoisonIvy(const lisp::Lisp& reader) - : BadGuy(reader, "images/creatures/poison_ivy/poison_ivy.sprite") + : WalkingBadguy(reader, "images/creatures/poison_ivy/poison_ivy.sprite", "left", "right") { + walk_speed = 80; } PoisonIvy::PoisonIvy(const Vector& pos, Direction d) - : BadGuy(pos, d, "images/creatures/poison_ivy/poison_ivy.sprite") + : WalkingBadguy(pos, d, "images/creatures/poison_ivy/poison_ivy.sprite", "left", "right") { + walk_speed = 80; } void PoisonIvy::write(lisp::Writer& writer) { writer.start_list("poisonivy"); - - writer.write_float("x", start_position.x); - writer.write_float("y", start_position.y); - + WalkingBadguy::write(writer); writer.end_list("poisonivy"); } -void -PoisonIvy::activate() -{ - physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED); - sprite->set_action(dir == LEFT ? "left" : "right"); -} - bool PoisonIvy::collision_squished(Player& player) { sprite->set_action(dir == LEFT ? "squished-left" : "squished-right"); - // spawn some particles - // TODO: provide convenience function in MovingSprite or MovingObject? - for (int i = 0; i < 3; i++) { - Vector ppos = bbox.get_middle(); - float angle = systemRandom.randf(-M_PI_2, M_PI_2); - float velocity = systemRandom.randf(350, 400); - float vx = sin(angle)*velocity; - float vy = -cos(angle)*velocity; - Vector pspeed = Vector(vx, vy); - Vector paccel = Vector(0, 100); - Sector::current()->add_object(new SpriteParticle("images/objects/particles/poisonivy.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1)); - } + // spawn some particles + // TODO: provide convenience function in MovingSprite or MovingObject? + for (int i = 0; i < 3; i++) { + Vector ppos = bbox.get_middle(); + float angle = systemRandom.randf(-M_PI_2, M_PI_2); + float velocity = systemRandom.randf(350, 400); + float vx = sin(angle)*velocity; + float vy = -cos(angle)*velocity; + Vector pspeed = Vector(vx, vy); + Vector paccel = Vector(0, 100); + Sector::current()->add_object(new SpriteParticle("images/objects/particles/poisonivy.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1)); + } kill_squished(player); return true; } -void -PoisonIvy::collision_solid(const CollisionHit& hit) -{ - if(hit.top || hit.bottom) { - physic.set_velocity_y(0); - } else if(hit.left || hit.right) { - dir = dir == LEFT ? RIGHT : LEFT; - sprite->set_action(dir == LEFT ? "left" : "right"); - physic.set_velocity_x(-physic.get_velocity_x()); - } -} - -HitResponse -PoisonIvy::collision_badguy(BadGuy& , const CollisionHit& hit) -{ - if(hit.left || hit.right) { - dir = dir == LEFT ? RIGHT : LEFT; - sprite->set_action(dir == LEFT ? "left" : "right"); - physic.set_velocity_x(-physic.get_velocity_x()); - } - - return CONTINUE; -} - IMPLEMENT_FACTORY(PoisonIvy, "poisonivy") diff --git a/src/badguy/poisonivy.hpp b/src/badguy/poisonivy.hpp index fd77d0574..908a788e4 100644 --- a/src/badguy/poisonivy.hpp +++ b/src/badguy/poisonivy.hpp @@ -20,23 +20,20 @@ #ifndef __POISONIVY_H__ #define __POISONIVY_H__ -#include "badguy.hpp" +#include "walking_badguy.hpp" -class PoisonIvy : public BadGuy +class PoisonIvy : public WalkingBadguy { public: PoisonIvy(const lisp::Lisp& reader); PoisonIvy(const Vector& pos, Direction d); - void activate(); void write(lisp::Writer& writer); - void collision_solid(const CollisionHit& hit); - HitResponse collision_badguy(BadGuy& other, const CollisionHit& hit); - virtual PoisonIvy* clone() const { return new PoisonIvy(*this); } protected: bool collision_squished(Player& player); + }; #endif diff --git a/src/badguy/snowball.cpp b/src/badguy/snowball.cpp index 57db13a21..2876357a4 100644 --- a/src/badguy/snowball.cpp +++ b/src/badguy/snowball.cpp @@ -21,34 +21,26 @@ #include "snowball.hpp" -static const float WALKSPEED = 80; - SnowBall::SnowBall(const lisp::Lisp& reader) - : BadGuy(reader, "images/creatures/snowball/snowball.sprite") + : WalkingBadguy(reader, "images/creatures/snowball/snowball.sprite", "left", "right") { + walk_speed = 80; } SnowBall::SnowBall(const Vector& pos, Direction d) - : BadGuy(pos, d, "images/creatures/snowball/snowball.sprite") + : WalkingBadguy(pos, d, "images/creatures/snowball/snowball.sprite", "left", "right") { + walk_speed = 80; } void SnowBall::write(lisp::Writer& writer) { writer.start_list("snowball"); - writer.write_float("x", start_position.x); - writer.write_float("y", start_position.y); + WalkingBadguy::write(writer); writer.end_list("snowball"); } -void -SnowBall::activate() -{ - physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED); - sprite->set_action(dir == LEFT ? "left" : "right"); -} - bool SnowBall::collision_squished(Player& player) { @@ -57,32 +49,4 @@ SnowBall::collision_squished(Player& player) return true; } -void -SnowBall::collision_solid(const CollisionHit& hit) -{ - if(hit.top || hit.bottom) { - physic.set_velocity_y(0); - } - if(hit.left || hit.right) { - dir = dir == LEFT ? RIGHT : LEFT; - sprite->set_action(dir == LEFT ? "left" : "right"); - physic.set_velocity_x(-physic.get_velocity_x()); - } -} - -HitResponse -SnowBall::collision_badguy(BadGuy& , const CollisionHit& hit) -{ - if(hit.top || hit.bottom) { - physic.set_velocity_y(0); - } - if(hit.left || hit.right) { - dir = dir == LEFT ? RIGHT : LEFT; - sprite->set_action(dir == LEFT ? "left" : "right"); - physic.set_velocity_x(-physic.get_velocity_x()); - } - - return CONTINUE; -} - IMPLEMENT_FACTORY(SnowBall, "snowball") diff --git a/src/badguy/snowball.hpp b/src/badguy/snowball.hpp index e7387ef51..bdb9b57ad 100644 --- a/src/badguy/snowball.hpp +++ b/src/badguy/snowball.hpp @@ -20,23 +20,20 @@ #ifndef __SNOWBALL_H__ #define __SNOWBALL_H__ -#include "badguy.hpp" +#include "walking_badguy.hpp" -class SnowBall : public BadGuy +class SnowBall : public WalkingBadguy { public: SnowBall(const lisp::Lisp& reader); SnowBall(const Vector& pos, Direction d); - void activate(); void write(lisp::Writer& writer); - void collision_solid(const CollisionHit& hit); - HitResponse collision_badguy(BadGuy& other, const CollisionHit& hit); - virtual SnowBall* clone() const { return new SnowBall(*this); } protected: bool collision_squished(Player& player); + }; #endif diff --git a/src/badguy/spiky.cpp b/src/badguy/spiky.cpp index 44ef749c9..f2b42ac8c 100644 --- a/src/badguy/spiky.cpp +++ b/src/badguy/spiky.cpp @@ -21,68 +21,19 @@ #include "spiky.hpp" -static const float WALKSPEED = 80; - Spiky::Spiky(const lisp::Lisp& reader) - : BadGuy(reader, "images/creatures/spiky/spiky.sprite") + : WalkingBadguy(reader, "images/creatures/spiky/spiky.sprite", "left", "right") { + walk_speed = 80; + max_drop_height = 600; } void Spiky::write(lisp::Writer& writer) { writer.start_list("spiky"); - - writer.write_float("x", start_position.x); - writer.write_float("y", start_position.y); - + WalkingBadguy::write(writer); writer.end_list("spiky"); } -void -Spiky::activate() -{ - physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED); - sprite->set_action(dir == LEFT ? "left" : "right"); -} - -void -Spiky::active_update(float elapsed_time) -{ - BadGuy::active_update(elapsed_time); - - if (on_ground() && might_fall(601)) - { - dir = (dir == LEFT ? RIGHT : LEFT); - sprite->set_action(dir == LEFT ? "left" : "right"); - physic.set_velocity_x(-physic.get_velocity_x()); - } -} - -void -Spiky::collision_solid(const CollisionHit& hit) -{ - update_on_ground_flag(hit); - - if(hit.top || hit.bottom) { // hit floor or roof? - physic.set_velocity_y(0); - } else { // hit right or left - dir = dir == LEFT ? RIGHT : LEFT; - sprite->set_action(dir == LEFT ? "left" : "right"); - physic.set_velocity_x(-physic.get_velocity_x()); - } -} - -HitResponse -Spiky::collision_badguy(BadGuy& , const CollisionHit& hit) -{ - if(hit.left || hit.right) { - dir = dir == LEFT ? RIGHT : LEFT; - sprite->set_action(dir == LEFT ? "left" : "right"); - physic.set_velocity_x(-physic.get_velocity_x()); - } - - return CONTINUE; -} - IMPLEMENT_FACTORY(Spiky, "spiky") diff --git a/src/badguy/spiky.hpp b/src/badguy/spiky.hpp index c0e448e18..98f7e28e0 100644 --- a/src/badguy/spiky.hpp +++ b/src/badguy/spiky.hpp @@ -20,19 +20,14 @@ #ifndef __SPIKY_H__ #define __SPIKY_H__ -#include "badguy.hpp" +#include "walking_badguy.hpp" -class Spiky : public BadGuy +class Spiky : public WalkingBadguy { public: Spiky(const lisp::Lisp& reader); - void activate(); void write(lisp::Writer& writer); - void active_update(float elapsed_time); - void collision_solid(const CollisionHit& hit); - HitResponse collision_badguy(BadGuy& badguy, const CollisionHit& hit); - virtual Spiky* clone() const { return new Spiky(*this); } private: