X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fbadguy%2Fwalking_badguy.cpp;h=90ba94d3e7e01ac9ebf272aa2b6f934023283f4b;hb=12a28b64dcce9c7ff706451b4f3aecd201cc8a5f;hp=4943a0f1361659256aeee51f75b32113f62df1aa;hpb=714a30abd887def6331a193216387e66cbfbd1bb;p=supertux.git diff --git a/src/badguy/walking_badguy.cpp b/src/badguy/walking_badguy.cpp index 4943a0f13..90ba94d3e 100644 --- a/src/badguy/walking_badguy.cpp +++ b/src/badguy/walking_badguy.cpp @@ -1,12 +1,10 @@ -// $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 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 3 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 @@ -14,45 +12,66 @@ // 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) +// along with this program. If not, see . + +#include "badguy/walking_badguy.hpp" + +#include "sprite/sprite.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), + turn_around_timer(), + turn_around_counter() { } -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 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), + turn_around_timer(), + turn_around_counter() { } -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) +WalkingBadguy::WalkingBadguy(const Reader& 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), + turn_around_timer(), + turn_around_counter() { } void -WalkingBadguy::write(lisp::Writer& writer) -{ - writer.write_float("x", start_position.x); - writer.write_float("y", start_position.y); -} - -void -WalkingBadguy::activate() +WalkingBadguy::initialize() { if(frozen) return; 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.vx = (dir == LEFT ? -walk_speed : walk_speed); + physic.set_velocity_x(dir == LEFT ? -walk_speed : walk_speed); } void @@ -76,10 +95,10 @@ WalkingBadguy::collision_solid(const CollisionHit& hit) update_on_ground_flag(hit); if (hit.top) { - if (physic.vy < 0) physic.vy = 0; + if (physic.get_velocity_y() < 0) physic.set_velocity_y(0); } if (hit.bottom) { - if (physic.vy > 0) physic.vy = 0; + if (physic.get_velocity_y() > 0) physic.set_velocity_y(0); } if ((hit.left && (hit.slope_normal.y == 0) && (dir == LEFT)) || (hit.right && (hit.slope_normal.y == 0) && (dir == RIGHT))) { @@ -106,34 +125,42 @@ WalkingBadguy::turn_around() return; dir = dir == LEFT ? RIGHT : LEFT; sprite->set_action(dir == LEFT ? walk_left_action : walk_right_action); - physic.vx = -physic.vx; + physic.set_velocity_x(-physic.get_velocity_x()); + + // if we get dizzy, we fall off the screen + if (turn_around_timer.started()) { + if (turn_around_counter++ > 10) kill_fall(); + } else { + turn_around_timer.start(1); + turn_around_counter = 0; + } + } void WalkingBadguy::freeze() { BadGuy::freeze(); - physic.vx = 0; + physic.set_velocity_x(0); } void WalkingBadguy::unfreeze() { BadGuy::unfreeze(); - WalkingBadguy::activate(); + WalkingBadguy::initialize(); } - -float +float WalkingBadguy::get_velocity_y() const { - return physic.vy; + return physic.get_velocity_y(); } -void +void WalkingBadguy::set_velocity_y(float vy) { - physic.vy = vy; + physic.set_velocity_y(vy); } - +/* EOF */