X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fbadguy%2Fsnail.cpp;h=faf86602ff86dfb31bffb3c3aa7b9bae55103416;hb=35f17b8b7e5e0bafca3a34550fa300db704aedaa;hp=1f21922aff04033faba35def5328cebf59442488;hpb=22ebbf03379aad8d3fc704e47e6cfa7acca8651d;p=supertux.git diff --git a/src/badguy/snail.cpp b/src/badguy/snail.cpp index 1f21922af..faf86602f 100644 --- a/src/badguy/snail.cpp +++ b/src/badguy/snail.cpp @@ -1,12 +1,10 @@ -// $Id$ -// // SuperTux - Badguy "Snail" // 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,22 +12,28 @@ // 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. +// along with this program. If not, see . + +#include "badguy/snail.hpp" -#include +#include "audio/sound_manager.hpp" +#include "object/player.hpp" +#include "sprite/sprite.hpp" +#include "supertux/object_factory.hpp" -#include "snail.hpp" -#include "object/block.hpp" +#include namespace { - const float KICKSPEED = 500; - const int MAXSQUISHES = 10; - const float KICKSPEED_Y = -500; /**< y-velocity gained when kicked */ +const float SNAIL_KICK_SPEED = 500; +const int MAX_SNAIL_SQUISHES = 10; +const float SNAIL_KICK_SPEED_Y = -500; /**< y-velocity gained when kicked */ } -Snail::Snail(const lisp::Lisp& reader) - : WalkingBadguy(reader, "images/creatures/snail/snail.sprite", "left", "right"), state(STATE_NORMAL), squishcount(0) +Snail::Snail(const Reader& reader) : + WalkingBadguy(reader, "images/creatures/snail/snail.sprite", "left", "right"), + state(STATE_NORMAL), + kicked_delay_timer(), + squishcount(0) { walk_speed = 80; max_drop_height = 600; @@ -38,8 +42,11 @@ Snail::Snail(const lisp::Lisp& reader) sound_manager->preload("sounds/kick.wav"); } -Snail::Snail(const Vector& pos, Direction d) - : WalkingBadguy(pos, d, "images/creatures/snail/snail.sprite", "left", "right"), state(STATE_NORMAL), squishcount(0) +Snail::Snail(const Vector& pos, Direction d) : + WalkingBadguy(pos, d, "images/creatures/snail/snail.sprite", "left", "right"), + state(STATE_NORMAL), + kicked_delay_timer(), + squishcount(0) { walk_speed = 80; max_drop_height = 600; @@ -49,17 +56,9 @@ Snail::Snail(const Vector& pos, Direction d) } void -Snail::write(lisp::Writer& writer) -{ - writer.start_list("snail"); - WalkingBadguy::write(writer); - writer.end_list("snail"); -} - -void -Snail::activate() +Snail::initialize() { - WalkingBadguy::activate(); + WalkingBadguy::initialize(); be_normal(); } @@ -69,39 +68,35 @@ Snail::be_normal() if (state == STATE_NORMAL) return; state = STATE_NORMAL; - WalkingBadguy::activate(); + WalkingBadguy::initialize(); } void Snail::be_flat() { state = STATE_FLAT; - sprite->set_action(dir == LEFT ? "flat-left" : "flat-right"); - sprite->set_fps(64); + sprite->set_action(dir == LEFT ? "flat-left" : "flat-right", 1); physic.set_velocity_x(0); physic.set_velocity_y(0); - - flat_timer.start(4); } void Snail::be_kicked() { state = STATE_KICKED_DELAY; - sprite->set_action(dir == LEFT ? "flat-left" : "flat-right"); - sprite->set_fps(64); + sprite->set_action(dir == LEFT ? "flat-left" : "flat-right", 1); - physic.set_velocity_x(0); + physic.set_velocity_x(dir == LEFT ? -SNAIL_KICK_SPEED : SNAIL_KICK_SPEED); physic.set_velocity_y(0); // start a timer to delay addition of upward movement until we are (hopefully) out from under the player - kicked_delay_timer.start(0.05); + kicked_delay_timer.start(0.05f); } bool Snail::can_break(){ - return state == STATE_KICKED; + return state == STATE_KICKED; } void @@ -111,79 +106,61 @@ Snail::active_update(float elapsed_time) case STATE_NORMAL: WalkingBadguy::active_update(elapsed_time); - break; + return; case STATE_FLAT: - if (flat_timer.started()) { - sprite->set_fps(64 - 15 * flat_timer.get_timegone()); + if (sprite->animation_done()) { + be_normal(); } - if (flat_timer.check()) { - be_normal(); - } - BadGuy::active_update(elapsed_time); break; case STATE_KICKED_DELAY: if (kicked_delay_timer.check()) { - physic.set_velocity_x(dir == LEFT ? -KICKSPEED : KICKSPEED); - physic.set_velocity_y(KICKSPEED_Y); - state = STATE_KICKED; + physic.set_velocity_x(dir == LEFT ? -SNAIL_KICK_SPEED : SNAIL_KICK_SPEED); + physic.set_velocity_y(SNAIL_KICK_SPEED_Y); + state = STATE_KICKED; } - BadGuy::active_update(elapsed_time); break; case STATE_KICKED: physic.set_velocity_x(physic.get_velocity_x() * pow(0.99, elapsed_time/0.02)); - if (fabsf(physic.get_velocity_x()) < walk_speed) be_normal(); - BadGuy::active_update(elapsed_time); + if (sprite->animation_done() || (fabsf(physic.get_velocity_x()) < walk_speed)) be_normal(); break; } + + BadGuy::active_update(elapsed_time); } void Snail::collision_solid(const CollisionHit& hit) { - update_on_ground_flag(hit); - switch (state) { case STATE_NORMAL: WalkingBadguy::collision_solid(hit); - break; - case STATE_FLAT: - if(hit.top || hit.bottom) { - physic.set_velocity_y(0); - } - if(hit.left || hit.right) { - } - break; - case STATE_KICKED_DELAY: - if(hit.top || hit.bottom) { - physic.set_velocity_y(0); - } - if(hit.left || hit.right) { - physic.set_velocity_x(0); - } - break; + return; case STATE_KICKED: - if(hit.top || hit.bottom) { - physic.set_velocity_y(0); - } if(hit.left || hit.right) { - sound_manager->play("sounds/iceblock_bump.wav", get_pos()); - - if( ( dir == LEFT && hit.left ) || ( dir == RIGHT && hit.right) ){ - dir = (dir == LEFT) ? RIGHT : LEFT; - sprite->set_action(dir == LEFT ? "flat-left" : "flat-right"); + sound_manager->play("sounds/iceblock_bump.wav", get_pos()); - physic.set_velocity_x(-physic.get_velocity_x()*0.75); - if (fabsf(physic.get_velocity_x()) < walk_speed) be_normal(); - } + if( ( dir == LEFT && hit.left ) || ( dir == RIGHT && hit.right) ){ + dir = (dir == LEFT) ? RIGHT : LEFT; + sprite->set_action(dir == LEFT ? "flat-left" : "flat-right"); + physic.set_velocity_x(-physic.get_velocity_x()); + } + } + /* fall-through */ + case STATE_FLAT: + case STATE_KICKED_DELAY: + if(hit.top || hit.bottom) { + physic.set_velocity_y(0); } break; } + update_on_ground_flag(hit); + } HitResponse @@ -205,19 +182,48 @@ Snail::collision_badguy(BadGuy& badguy, const CollisionHit& hit) return ABORT_MOVE; } +HitResponse +Snail::collision_player(Player& player, const CollisionHit& hit) +{ + // handle kicks from left or right side + if(state == STATE_FLAT && (hit.left || hit.right)) { + if(hit.left) { + dir = RIGHT; + } else if(hit.right) { + dir = LEFT; + } + player.kick(); + be_kicked(); + return FORCE_MOVE; + } + + return BadGuy::collision_player(player, hit); +} + bool Snail::collision_squished(GameObject& object) { + Player* player = dynamic_cast(&object); + if(player && (player->does_buttjump || player->is_invincible())) { + kill_fall(); + player->bounce(*this); + return true; + } + switch(state) { case STATE_KICKED: case STATE_NORMAL: + + // Can't stomp in midair + if(!on_ground()) + break; + squishcount++; - if(squishcount >= MAXSQUISHES) { + if (squishcount >= MAX_SNAIL_SQUISHES) { kill_fall(); return true; } - sound_manager->play("sounds/stomp.wav", get_pos()); be_flat(); break; @@ -225,12 +231,12 @@ Snail::collision_squished(GameObject& object) case STATE_FLAT: sound_manager->play("sounds/kick.wav", get_pos()); { - MovingObject* movingobject = dynamic_cast(&object); - if (movingobject && (movingobject->get_pos().x < get_pos().x)) { - dir = RIGHT; - } else { - dir = LEFT; - } + MovingObject* movingobject = dynamic_cast(&object); + if (movingobject && (movingobject->get_pos().x < get_pos().x)) { + dir = RIGHT; + } else { + dir = LEFT; + } } be_kicked(); break; @@ -240,9 +246,8 @@ Snail::collision_squished(GameObject& object) } - Player* player = dynamic_cast(&object); if (player) player->bounce(*this); return true; } -IMPLEMENT_FACTORY(Snail, "snail") +/* EOF */