From: Christoph Sommer Date: Thu, 11 May 2006 19:35:16 +0000 (+0000) Subject: First attempts at making BadGuys cloneable X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=58eb3364f724b2100859fd39da9bba5a9a09cafc;p=supertux.git First attempts at making BadGuys cloneable SVN-Revision: 3505 --- diff --git a/src/badguy/angrystone.hpp b/src/badguy/angrystone.hpp index b658ca806..0b7dbf788 100644 --- a/src/badguy/angrystone.hpp +++ b/src/badguy/angrystone.hpp @@ -35,6 +35,8 @@ public: void active_update(float elapsed_time); void kill_fall(); + virtual AngryStone* clone() const { return new AngryStone(*this); } + protected: Vector attackDirection; /**< 1-normalized vector of current attack direction */ Vector oldWallDirection; /**< if wall was hit during last attack: 1-normalized vector of last attack direction, (0,0) otherwise */ diff --git a/src/badguy/badguy.cpp b/src/badguy/badguy.cpp index 0870ead41..fa400f48f 100644 --- a/src/badguy/badguy.cpp +++ b/src/badguy/badguy.cpp @@ -38,6 +38,12 @@ BadGuy::BadGuy() set_group(COLGROUP_DISABLED); } +BadGuy::BadGuy(const BadGuy& other) + : MovingObject(other), Serializable(other), countMe(other.countMe), physic(other.physic), activated(other.activated), start_position(other.start_position), dir(other.dir), layer(other.layer), state(other.state), state_timer(other.state_timer) +{ + sprite = new Sprite(*other.sprite); +} + BadGuy::~BadGuy() { delete sprite; diff --git a/src/badguy/badguy.hpp b/src/badguy/badguy.hpp index 182ab6e0a..e9f68333b 100644 --- a/src/badguy/badguy.hpp +++ b/src/badguy/badguy.hpp @@ -44,6 +44,7 @@ class BadGuy : public MovingObject, public Serializable { public: BadGuy(); + BadGuy(const BadGuy& badguy); ~BadGuy(); /** Called when the badguy is drawn. The default implementation simply draws @@ -87,6 +88,8 @@ public: * during runtime. */ bool countMe; + virtual BadGuy* clone() const = 0; + protected: enum State { STATE_INIT, diff --git a/src/badguy/bomb.hpp b/src/badguy/bomb.hpp index b2da78bd4..abd8b9d06 100644 --- a/src/badguy/bomb.hpp +++ b/src/badguy/bomb.hpp @@ -35,6 +35,8 @@ public: void kill_fall(); void explode(); + virtual Bomb* clone() const { return new Bomb(*this); } + private: enum State { STATE_TICKING, diff --git a/src/badguy/bouncing_snowball.hpp b/src/badguy/bouncing_snowball.hpp index ba6c7b275..a16ed2295 100644 --- a/src/badguy/bouncing_snowball.hpp +++ b/src/badguy/bouncing_snowball.hpp @@ -33,6 +33,8 @@ public: HitResponse collision_solid(GameObject& other, const CollisionHit& hit); HitResponse collision_badguy(BadGuy& badguy, const CollisionHit& hit); + virtual BouncingSnowball* clone() const { return new BouncingSnowball(*this); } + protected: bool collision_squished(Player& player); bool set_direction; diff --git a/src/badguy/dart.cpp b/src/badguy/dart.cpp index 1c6ab0d4f..8e508d9db 100644 --- a/src/badguy/dart.cpp +++ b/src/badguy/dart.cpp @@ -46,11 +46,27 @@ Dart::Dart(float pos_x, float pos_y, Direction d, const BadGuy* parent = 0) : se countMe = false; } +Dart::Dart(const Dart& other) + : BadGuy(other), set_direction(other.set_direction), initial_direction(other.initial_direction), parent(other.parent) +{ + soundSource = sound_manager->create_sound_source("sounds/flame.wav"); +} + Dart::~Dart() { delete soundSource; } +bool +Dart::updatePointers(const GameObject* from_object, GameObject* to_object) +{ + if (from_object == parent) { + parent = dynamic_cast(to_object); + return true; + } + return false; +} + void Dart::write(lisp::Writer& writer) { diff --git a/src/badguy/dart.hpp b/src/badguy/dart.hpp index c816e9271..aaebdc26c 100644 --- a/src/badguy/dart.hpp +++ b/src/badguy/dart.hpp @@ -31,6 +31,7 @@ class Dart : public BadGuy public: Dart(const lisp::Lisp& reader); Dart(float pos_x, float pos_y, Direction d, const BadGuy* parent); + Dart(const Dart& dart); ~Dart(); void activate(); @@ -43,6 +44,10 @@ public: HitResponse collision_badguy(BadGuy& badguy, const CollisionHit& hit); HitResponse collision_player(Player& player, const CollisionHit& hit); + virtual Dart* clone() const { return new Dart(*this); } + + virtual bool updatePointers(const GameObject* from_object, GameObject* to_object); + protected: bool set_direction; Direction initial_direction; diff --git a/src/badguy/darttrap.hpp b/src/badguy/darttrap.hpp index d3598b8e5..461e8525e 100644 --- a/src/badguy/darttrap.hpp +++ b/src/badguy/darttrap.hpp @@ -36,6 +36,8 @@ public: void active_update(float elapsed_time); HitResponse collision_player(Player& player, const CollisionHit& hit); + virtual DartTrap* clone() const { return new DartTrap(*this); } + protected: enum State { IDLE, LOADING diff --git a/src/badguy/dispenser.hpp b/src/badguy/dispenser.hpp index fc1a4245f..f79e8e6d0 100644 --- a/src/badguy/dispenser.hpp +++ b/src/badguy/dispenser.hpp @@ -32,6 +32,8 @@ public: void write(lisp::Writer& writer); void active_update(float elapsed_time); + virtual Dispenser* clone() const { return new Dispenser(*this); } + protected: bool collision_squished(Player& player); void launch_badguy(); diff --git a/src/badguy/fish.hpp b/src/badguy/fish.hpp index 173eb4c07..812678396 100644 --- a/src/badguy/fish.hpp +++ b/src/badguy/fish.hpp @@ -37,6 +37,8 @@ public: void write(lisp::Writer& ); void active_update(float); + virtual Fish* clone() const { return new Fish(*this); } + private: HitResponse hit(const CollisionHit& ); void start_waiting(); diff --git a/src/badguy/flame.cpp b/src/badguy/flame.cpp index c88e11867..4bdafb6ff 100644 --- a/src/badguy/flame.cpp +++ b/src/badguy/flame.cpp @@ -37,8 +37,12 @@ Flame::Flame(const lisp::Lisp& reader) layer = LAYER_FLOATINGOBJECTS; } -Flame::~Flame() +Flame::Flame(const Flame& other) + : BadGuy(other), angle(other.angle), radius(other.radius), speed(other.speed) { + if (sound_manager->is_sound_enabled()) { + source.reset(sound_manager->create_sound_source("sounds/flame.wav")); + } } void diff --git a/src/badguy/flame.hpp b/src/badguy/flame.hpp index b437efd6f..303c69848 100644 --- a/src/badguy/flame.hpp +++ b/src/badguy/flame.hpp @@ -25,7 +25,7 @@ class Flame : public BadGuy { public: Flame(const lisp::Lisp& reader); - ~Flame(); + Flame(const Flame& flame); void activate(); void deactivate(); @@ -34,6 +34,8 @@ public: void active_update(float elapsed_time); void kill_fall(); + virtual Flame* clone() const { return new Flame(*this); } + private: float angle; float radius; diff --git a/src/badguy/flyingsnowball.hpp b/src/badguy/flyingsnowball.hpp index c9fa3fca3..3bac6399d 100644 --- a/src/badguy/flyingsnowball.hpp +++ b/src/badguy/flyingsnowball.hpp @@ -32,6 +32,9 @@ public: void write(lisp::Writer& writer); void active_update(float elapsed_time); HitResponse collision_solid(GameObject& other, const CollisionHit& hit); + + virtual FlyingSnowBall* clone() const { return new FlyingSnowBall(*this); } + protected: enum FlyingSnowballMode { FLY_UP, diff --git a/src/badguy/igel.hpp b/src/badguy/igel.hpp index 14b8ee50a..be0a7b39f 100644 --- a/src/badguy/igel.hpp +++ b/src/badguy/igel.hpp @@ -40,6 +40,8 @@ public: void active_update(float elapsed_time); + virtual Igel* clone() const { return new Igel(*this); } + protected: bool collision_squished(Player& player); void be_normal(); /**< switch to state STATE_NORMAL */ diff --git a/src/badguy/jumpy.hpp b/src/badguy/jumpy.hpp index 1ca2aa416..9304a42b7 100644 --- a/src/badguy/jumpy.hpp +++ b/src/badguy/jumpy.hpp @@ -33,6 +33,8 @@ public: void write(lisp::Writer& writer); void active_update(float); + virtual Jumpy* clone() const { return new Jumpy(*this); } + private: HitResponse hit(const CollisionHit& hit); Vector pos_groundhit; diff --git a/src/badguy/kugelblitz.hpp b/src/badguy/kugelblitz.hpp index e5ffd6320..a9c64bafa 100644 --- a/src/badguy/kugelblitz.hpp +++ b/src/badguy/kugelblitz.hpp @@ -39,6 +39,8 @@ public: void kill_fall(); void explode(); + virtual Kugelblitz* clone() const { return new Kugelblitz(*this); } + private: void try_activate(); HitResponse hit(const CollisionHit& hit); diff --git a/src/badguy/mrbomb.hpp b/src/badguy/mrbomb.hpp index 92af127c1..86c4fa610 100644 --- a/src/badguy/mrbomb.hpp +++ b/src/badguy/mrbomb.hpp @@ -35,6 +35,8 @@ public: HitResponse collision_badguy(BadGuy& badguy, const CollisionHit& hit); void kill_fall(); + virtual MrBomb* clone() const { return new MrBomb(*this); } + protected: bool collision_squished(Player& player); bool set_direction; diff --git a/src/badguy/mriceblock.hpp b/src/badguy/mriceblock.hpp index 7ded21a37..d3ad9dbb2 100644 --- a/src/badguy/mriceblock.hpp +++ b/src/badguy/mriceblock.hpp @@ -41,6 +41,8 @@ public: void grab(MovingObject& object, const Vector& pos, Direction dir); void ungrab(MovingObject& object, Direction dir); + virtual MrIceBlock* clone() const { return new MrIceBlock(*this); } + protected: bool collision_squished(Player& player); diff --git a/src/badguy/mrrocket.hpp b/src/badguy/mrrocket.hpp index 8550a42c7..25500ec2b 100644 --- a/src/badguy/mrrocket.hpp +++ b/src/badguy/mrrocket.hpp @@ -35,6 +35,8 @@ public: void write(lisp::Writer& writer); HitResponse collision_solid(GameObject& other, const CollisionHit& hit); + virtual MrRocket* clone() const { return new MrRocket(*this); } + protected: bool collision_squished(Player& player); bool set_direction; diff --git a/src/badguy/mrtree.hpp b/src/badguy/mrtree.hpp index db3d019ba..652a00e6b 100644 --- a/src/badguy/mrtree.hpp +++ b/src/badguy/mrtree.hpp @@ -33,6 +33,8 @@ public: HitResponse collision_solid(GameObject& other, const CollisionHit& hit); HitResponse collision_badguy(BadGuy& badguy, const CollisionHit& hit); + virtual MrTree* clone() const { return new MrTree(*this); } + protected: enum MyState { STATE_BIG, STATE_INVINCIBLE, STATE_NORMAL diff --git a/src/badguy/nolok_01.hpp b/src/badguy/nolok_01.hpp index 45a93bdd0..2a0cdf09e 100644 --- a/src/badguy/nolok_01.hpp +++ b/src/badguy/nolok_01.hpp @@ -34,7 +34,9 @@ public: void active_update(float elapsed_time); void kill_fall(); HitResponse collision_solid(GameObject& other, const CollisionHit& hit); - + + virtual Nolok_01* clone() const { return new Nolok_01(*this); } + protected: bool collision_squished(Player& player); Timer action_timer; diff --git a/src/badguy/plant.hpp b/src/badguy/plant.hpp index 49755fd1d..ab0dfd5bb 100644 --- a/src/badguy/plant.hpp +++ b/src/badguy/plant.hpp @@ -33,6 +33,8 @@ public: HitResponse collision_badguy(BadGuy& badguy, const CollisionHit& hit); void active_update(float elapsed_time); + virtual Plant* clone() const { return new Plant(*this); } + protected: Timer timer; diff --git a/src/badguy/poisonivy.hpp b/src/badguy/poisonivy.hpp index 33d7e2c8d..0005eaa34 100644 --- a/src/badguy/poisonivy.hpp +++ b/src/badguy/poisonivy.hpp @@ -32,7 +32,9 @@ public: void write(lisp::Writer& writer); HitResponse collision_solid(GameObject& other, 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); bool set_direction; diff --git a/src/badguy/rocketexplosion.hpp b/src/badguy/rocketexplosion.hpp index 63ca12a0f..c76a6b8f0 100644 --- a/src/badguy/rocketexplosion.hpp +++ b/src/badguy/rocketexplosion.hpp @@ -35,6 +35,8 @@ public: void kill_fall(); void explode(); + virtual RocketExplosion* clone() const { return new RocketExplosion(*this); } + private: Timer timer; }; diff --git a/src/badguy/skullyhop.hpp b/src/badguy/skullyhop.hpp index 6e86119b5..13c7d0699 100644 --- a/src/badguy/skullyhop.hpp +++ b/src/badguy/skullyhop.hpp @@ -39,6 +39,8 @@ public: bool collision_squished(Player& player); void active_update(float elapsed_time); + virtual SkullyHop* clone() const { return new SkullyHop(*this); } + protected: enum SkullyHopState { STANDING, diff --git a/src/badguy/snail.hpp b/src/badguy/snail.hpp index 76252caf4..7aff914f0 100644 --- a/src/badguy/snail.hpp +++ b/src/badguy/snail.hpp @@ -38,6 +38,8 @@ public: void active_update(float elapsed_time); + virtual Snail* clone() const { return new Snail(*this); } + protected: bool collision_squished(Player& player); void be_normal(); /**< switch to state STATE_NORMAL */ diff --git a/src/badguy/snowball.hpp b/src/badguy/snowball.hpp index ce642fb9c..dfd0e8ec5 100644 --- a/src/badguy/snowball.hpp +++ b/src/badguy/snowball.hpp @@ -32,7 +32,9 @@ public: void write(lisp::Writer& writer); HitResponse collision_solid(GameObject& other, 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); bool set_direction; diff --git a/src/badguy/spidermite.hpp b/src/badguy/spidermite.hpp index 113a70ce4..048831ce4 100644 --- a/src/badguy/spidermite.hpp +++ b/src/badguy/spidermite.hpp @@ -32,6 +32,9 @@ public: void write(lisp::Writer& writer); void active_update(float elapsed_time); HitResponse collision_solid(GameObject& other, const CollisionHit& hit); + + virtual SpiderMite* clone() const { return new SpiderMite(*this); } + protected: enum SpiderMiteMode { FLY_UP, diff --git a/src/badguy/spiky.hpp b/src/badguy/spiky.hpp index 8eed44297..f4cbe69dd 100644 --- a/src/badguy/spiky.hpp +++ b/src/badguy/spiky.hpp @@ -32,6 +32,9 @@ public: void active_update(float elapsed_time); HitResponse collision_solid(GameObject& other, const CollisionHit& hit); HitResponse collision_badguy(BadGuy& badguy, const CollisionHit& hit); + + virtual Spiky* clone() const { return new Spiky(*this); } + }; #endif diff --git a/src/badguy/sspiky.hpp b/src/badguy/sspiky.hpp index 532e06e01..bb7741930 100644 --- a/src/badguy/sspiky.hpp +++ b/src/badguy/sspiky.hpp @@ -33,6 +33,8 @@ public: HitResponse collision_badguy(BadGuy& badguy, const CollisionHit& hit); void active_update(float elapsed_time); + virtual SSpiky* clone() const { return new SSpiky(*this); } + protected: Timer timer; diff --git a/src/badguy/stalactite.hpp b/src/badguy/stalactite.hpp index bd58d1b7d..9619f6127 100644 --- a/src/badguy/stalactite.hpp +++ b/src/badguy/stalactite.hpp @@ -36,6 +36,8 @@ public: void draw(DrawingContext& context); void deactivate(); + virtual Stalactite* clone() const { return new Stalactite(*this); } + protected: Timer timer; diff --git a/src/badguy/totem.cpp b/src/badguy/totem.cpp index 88620f913..7d2b932a1 100644 --- a/src/badguy/totem.cpp +++ b/src/badguy/totem.cpp @@ -38,12 +38,31 @@ Totem::Totem(const lisp::Lisp& reader) bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height()); } +Totem::Totem(const Totem& other) + : BadGuy(other), carrying(other.carrying), carried_by(other.carried_by) +{ +} + Totem::~Totem() { if (carrying) carrying->jump_off(); if (carried_by) jump_off(); } +bool +Totem::updatePointers(const GameObject* from_object, GameObject* to_object) +{ + if (from_object == carrying) { + carrying = dynamic_cast(to_object); + return true; + } + if (from_object == carried_by) { + carried_by = dynamic_cast(to_object); + return true; + } + return false; +} + void Totem::write(lisp::Writer& writer) { diff --git a/src/badguy/totem.hpp b/src/badguy/totem.hpp index f64e0985b..874202514 100644 --- a/src/badguy/totem.hpp +++ b/src/badguy/totem.hpp @@ -30,6 +30,7 @@ class Totem : public BadGuy { public: Totem(const lisp::Lisp& reader); + Totem(const Totem& totem); ~Totem(); void activate(); @@ -38,6 +39,9 @@ public: HitResponse collision_solid(GameObject& other, const CollisionHit& hit); HitResponse collision_badguy(BadGuy& badguy, const CollisionHit& hit); + virtual Totem* clone() const { return new Totem(*this); } + virtual bool updatePointers(const GameObject* from_object, GameObject* to_object); + protected: Totem* carrying; /**< Totem we are currently carrying (or 0) */ Totem* carried_by; /**< Totem by which we are currently carried (or 0) */ diff --git a/src/badguy/willowisp.cpp b/src/badguy/willowisp.cpp index d9c6a63cb..1a4969b31 100644 --- a/src/badguy/willowisp.cpp +++ b/src/badguy/willowisp.cpp @@ -41,6 +41,12 @@ WillOWisp::WillOWisp(const lisp::Lisp& reader) layer = LAYER_FLOATINGOBJECTS; } +WillOWisp::WillOWisp(const WillOWisp& other) + : BadGuy(other), mystate(other.mystate), target_sector(other.target_sector), target_spawnpoint(other.target_spawnpoint) +{ + soundSource = sound_manager->create_sound_source("sounds/willowisp.wav"); +} + WillOWisp::~WillOWisp() { delete soundSource; diff --git a/src/badguy/willowisp.hpp b/src/badguy/willowisp.hpp index e21671649..cfbcb19e9 100644 --- a/src/badguy/willowisp.hpp +++ b/src/badguy/willowisp.hpp @@ -27,6 +27,7 @@ class WillOWisp : public BadGuy { public: WillOWisp(const lisp::Lisp& reader); + WillOWisp(const WillOWisp& willowisp); ~WillOWisp(); void activate(); @@ -38,6 +39,8 @@ public: virtual void draw(DrawingContext& context); + virtual WillOWisp* clone() const { return new WillOWisp(*this); } + protected: HitResponse collision_player(Player& player, const CollisionHit& hit); diff --git a/src/badguy/yeti.hpp b/src/badguy/yeti.hpp index b9d4f4547..30a6744c3 100644 --- a/src/badguy/yeti.hpp +++ b/src/badguy/yeti.hpp @@ -38,6 +38,8 @@ public: void kill_squished(Player& player); void kill_fall(); + virtual Yeti* clone() const { return new Yeti(*this); } + private: void run(); void jump_up(); diff --git a/src/badguy/yeti_stalactite.hpp b/src/badguy/yeti_stalactite.hpp index f0e5d2383..40ead0d03 100644 --- a/src/badguy/yeti_stalactite.hpp +++ b/src/badguy/yeti_stalactite.hpp @@ -33,6 +33,8 @@ public: void active_update(float elapsed_time); void start_shaking(); bool is_hanging(); + + virtual YetiStalactite* clone() const { return new YetiStalactite(*this); } }; #endif diff --git a/src/badguy/zeekling.hpp b/src/badguy/zeekling.hpp index 1fee377a9..ab88a9afa 100644 --- a/src/badguy/zeekling.hpp +++ b/src/badguy/zeekling.hpp @@ -35,6 +35,8 @@ public: HitResponse collision_solid(GameObject& other, const CollisionHit& hit); void active_update(float elapsed_time); + virtual Zeekling* clone() const { return new Zeekling(*this); } + protected: bool collision_squished(Player& player); bool set_direction;