From 29f938bbdc1b2749211221bef3faa1cce701bc32 Mon Sep 17 00:00:00 2001 From: Christoph Sommer Date: Mon, 10 Jul 2006 01:57:10 +0000 Subject: [PATCH] Testing new base class "WalkingBadguy" SVN-Revision: 3977 --- src/badguy/walking_badguy.cpp | 106 ++++++++++++++++++++++++++++++++++++++++++ src/badguy/walking_badguy.hpp | 50 ++++++++++++++++++++ 2 files changed, 156 insertions(+) create mode 100644 src/badguy/walking_badguy.cpp create mode 100644 src/badguy/walking_badguy.hpp diff --git a/src/badguy/walking_badguy.cpp b/src/badguy/walking_badguy.cpp new file mode 100644 index 000000000..6b51f0048 --- /dev/null +++ b/src/badguy/walking_badguy.cpp @@ -0,0 +1,106 @@ +// $Id$ +// +// SuperTux - WalkingBadguy +// Copyright (C) 2006 Christoph Sommer +// +// 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 + +#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 index 000000000..feeae8652 --- /dev/null +++ b/src/badguy/walking_badguy.hpp @@ -0,0 +1,50 @@ +// $Id$ +// +// SuperTux - WalkingBadguy +// Copyright (C) 2006 Christoph Sommer +// +// 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 -- 2.11.0