Testing new base class "WalkingBadguy"
authorChristoph Sommer <mail@christoph-sommer.de>
Mon, 10 Jul 2006 01:56:01 +0000 (01:56 +0000)
committerChristoph Sommer <mail@christoph-sommer.de>
Mon, 10 Jul 2006 01:56:01 +0000 (01:56 +0000)
SVN-Revision: 3976

src/badguy/mrbomb.cpp
src/badguy/mrbomb.hpp
src/badguy/mriceblock.cpp
src/badguy/mriceblock.hpp
src/badguy/poisonivy.cpp
src/badguy/poisonivy.hpp
src/badguy/snowball.cpp
src/badguy/snowball.hpp
src/badguy/spiky.cpp
src/badguy/spiky.hpp

index 78cb5b7..61bc9d9 100644 (file)
 #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();
index 450d0d1..63c4364 100644 (file)
 #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); }
index 09f2bb0..8b3a995 100644 (file)
 #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());
index 01e693a..4f95bd9 100644 (file)
 #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);
index 34d4ed8..e881933 100644 (file)
 #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")
index fd77d05..908a788 100644 (file)
 #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
index 57db13a..2876357 100644 (file)
 
 #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")
index e7387ef..bdb9b57 100644 (file)
 #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
index 44ef749..f2b42ac 100644 (file)
 
 #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")
index c0e448e..98f7e28 100644 (file)
 #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: