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

src/badguy/walking_badguy.cpp [new file with mode: 0644]
src/badguy/walking_badguy.hpp [new file with mode: 0644]

diff --git a/src/badguy/walking_badguy.cpp b/src/badguy/walking_badguy.cpp
new file mode 100644 (file)
index 0000000..6b51f00
--- /dev/null
@@ -0,0 +1,106 @@
+//  $Id$
+//
+//  SuperTux - WalkingBadguy
+//  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
+//
+//  This program is free software; you can redistribute it and/or
+//  modify it under the terms of the GNU General Public License
+//  as published by the Free Software Foundation; either version 2
+//  of the License, or (at your option) any later version.
+//
+//  This program is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+//  GNU General Public License for more details.
+//
+//  You should have received a copy of the GNU General Public License
+//  along with this program; if not, write to the Free Software
+//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+
+#include <config.h>
+
+#include "walking_badguy.hpp"
+#include "log.hpp"
+
+
+WalkingBadguy::WalkingBadguy(const Vector& pos, const std::string& sprite_name, const std::string& walk_left_action, const std::string& walk_right_action, int layer)
+       : BadGuy(pos, sprite_name, layer), walk_left_action(walk_left_action), walk_right_action(walk_right_action), walk_speed(80), max_drop_height(-1)
+{
+}
+
+WalkingBadguy::WalkingBadguy(const Vector& pos, Direction direction, const std::string& sprite_name, const std::string& walk_left_action, const std::string& walk_right_action, int layer)
+       : BadGuy(pos, direction, sprite_name, layer), walk_left_action(walk_left_action), walk_right_action(walk_right_action), walk_speed(80), max_drop_height(-1)
+{
+}
+
+WalkingBadguy::WalkingBadguy(const lisp::Lisp& reader, const std::string& sprite_name, const std::string& walk_left_action, const std::string& walk_right_action, int layer)
+       : BadGuy(reader, sprite_name, layer), walk_left_action(walk_left_action), walk_right_action(walk_right_action), walk_speed(80), max_drop_height(-1)
+{
+}
+
+void
+WalkingBadguy::write(lisp::Writer& writer)
+{
+  writer.write_float("x", start_position.x);
+  writer.write_float("y", start_position.y);
+}
+
+void
+WalkingBadguy::activate()
+{
+  sprite->set_action(dir == LEFT ? walk_left_action : walk_right_action);
+  bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
+  physic.set_velocity_x(dir == LEFT ? -walk_speed : walk_speed);
+}
+
+void
+WalkingBadguy::active_update(float elapsed_time)
+{
+  BadGuy::active_update(elapsed_time);
+
+  if (max_drop_height > -1) {
+    if (on_ground() && might_fall(max_drop_height+1))
+    {
+      turn_around();
+    }
+  }
+
+}
+
+void
+WalkingBadguy::collision_solid(const CollisionHit& hit)
+{
+
+  update_on_ground_flag(hit);
+
+  if (hit.top || hit.bottom) { 
+    physic.set_velocity_y(0);
+  }
+
+//  if ((hit.left && dir == LEFT) || (hit.right && dir == RIGHT)) { 
+  if (hit.left || hit.right) {
+    turn_around();
+  }
+
+}
+
+HitResponse
+WalkingBadguy::collision_badguy(BadGuy& , const CollisionHit& hit)
+{
+
+  //if ((hit.left && (dir == LEFT)) || (hit.right && (dir == RIGHT))) { 
+  if (hit.left || hit.right) {
+    turn_around();
+  }
+
+  return CONTINUE;
+}
+
+void
+WalkingBadguy::turn_around()
+{
+  dir = dir == LEFT ? RIGHT : LEFT;
+  sprite->set_action(dir == LEFT ? walk_left_action : walk_right_action);
+  physic.set_velocity_x(dir == LEFT ? -walk_speed : walk_speed);
+}
+
diff --git a/src/badguy/walking_badguy.hpp b/src/badguy/walking_badguy.hpp
new file mode 100644 (file)
index 0000000..feeae86
--- /dev/null
@@ -0,0 +1,50 @@
+//  $Id$
+//
+//  SuperTux - WalkingBadguy
+//  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
+//
+//  This program is free software; you can redistribute it and/or
+//  modify it under the terms of the GNU General Public License
+//  as published by the Free Software Foundation; either version 2
+//  of the License, or (at your option) any later version.
+//
+//  This program is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+//  GNU General Public License for more details.
+//
+//  You should have received a copy of the GNU General Public License
+//  along with this program; if not, write to the Free Software
+//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+
+#ifndef __WALKING_BADGUY_H__
+#define __WALKING_BADGUY_H__
+
+#include "badguy.hpp"
+
+/**
+ * Baseclass for a Badguy that just walks around.
+ */
+class WalkingBadguy : public BadGuy
+{
+public:
+  WalkingBadguy(const Vector& pos, const std::string& sprite_name, const std::string& walk_left_action, const std::string& walk_right_action, int layer = LAYER_OBJECTS);
+  WalkingBadguy(const Vector& pos, Direction direction, const std::string& sprite_name, const std::string& walk_left_action, const std::string& walk_right_action, int layer = LAYER_OBJECTS);
+  WalkingBadguy(const lisp::Lisp& reader, const std::string& sprite_name, const std::string& walk_left_action, const std::string& walk_right_action, int layer = LAYER_OBJECTS);
+
+  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);
+
+protected:
+  void turn_around(); 
+
+  const std::string walk_left_action;
+  const std::string walk_right_action;
+  float walk_speed;
+  int max_drop_height; /**< Maximum height of drop before we will turn around, or -1 to just drop from any ledge */
+};
+
+#endif