X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fbadguy%2Ffish.cpp;h=93898d476823c8ea8fc21e97971bd5febc0464aa;hb=7a6f00e27bdc0aac2107506c3b00cbf0bf1cccc5;hp=e5003a4e853b53a7d78433fe85adde94420e5903;hpb=f70596a04e67bb70cf55b91f394883c54234f85a;p=supertux.git diff --git a/src/badguy/fish.cpp b/src/badguy/fish.cpp index e5003a4e8..93898d476 100644 --- a/src/badguy/fish.cpp +++ b/src/badguy/fish.cpp @@ -1,7 +1,7 @@ // $Id$ -// +// // SuperTux -// Copyright (C) 2005 Matthias Braun +// Copyright (C) 2006 Matthias Braun // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License @@ -12,37 +12,30 @@ // 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. +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + #include #include "fish.hpp" +#include "tile.hpp" #include "object/tilemap.hpp" +#include "log.hpp" -static const float FISH_JUMP_POWER = 700; -static const float FISH_FALL_BY_Y = 10; -static const float FISH_WAIT_TIME = 3; +static const float FISH_JUMP_POWER = -600; +static const float FISH_WAIT_TIME = 1; Fish::Fish(const lisp::Lisp& reader) - : waiting(false) + : BadGuy(reader, "images/creatures/fish/fish.sprite", LAYER_TILES-1), stop_y(0) { - reader.get("x", start_position.x); - reader.get("y", start_position.y); - bbox.set_size(31.8, 31.8); - sprite = sprite_manager->create("fish"); physic.enable_gravity(true); } -Fish::Fish(float pos_x, float pos_y) - : waiting(false) +Fish::Fish(const Vector& pos) + : BadGuy(pos, "images/creatures/fish/fish.sprite", LAYER_TILES-1), stop_y(0) { - start_position.x = pos_x; - start_position.y = pos_y; - bbox.set_size(31.8, 31.8); - sprite = sprite_manager->create("fish"); physic.enable_gravity(true); } @@ -57,10 +50,10 @@ Fish::write(lisp::Writer& writer) writer.end_list("fish"); } -HitResponse -Fish::collision_solid(GameObject& , const CollisionHit& chit) +void +Fish::collision_solid(const CollisionHit& chit) { - return hit(chit); + hit(chit); } HitResponse @@ -69,10 +62,19 @@ Fish::collision_badguy(BadGuy& , const CollisionHit& chit) return hit(chit); } +void +Fish::draw(DrawingContext& context) +{ + if(waiting.started()) + return; + + BadGuy::draw(context); +} + HitResponse -Fish::hit(const CollisionHit& chit) +Fish::hit(const CollisionHit& hit) { - if(chit.normal.y < .5) { // hit ceiling + if(hit.top) { physic.set_velocity_y(0); } @@ -80,50 +82,81 @@ Fish::hit(const CollisionHit& chit) } void +Fish::collision_tile(uint32_t tile_attributes) +{ + if ((tile_attributes & Tile::WATER) && (physic.get_velocity_y() >= 0)) { + + // initialize stop position if uninitialized + if (stop_y == 0) stop_y = get_pos().y + get_bbox().get_height(); + + // stop when we have reached the stop position + if (get_pos().y >= stop_y) { + if(!frozen) + start_waiting(); + movement = Vector(0, 0); + } + + } +} + +void Fish::active_update(float elapsed_time) { BadGuy::active_update(elapsed_time); - // check state and modify accordingly - if (!waiting && physic.get_velocity_y() < 0 - && (get_pos().y - start_position.y) >= FISH_FALL_BY_Y) // we fell far enough - { - start_waiting(); - } - else if (waiting) - { - waiting_for += elapsed_time; - if (waiting_for >= FISH_WAIT_TIME) // we've been waiting long enough - { - waiting = false; - physic.set_velocity_y(FISH_JUMP_POWER); - physic.enable_gravity(true); - } + // waited long enough? + if(waiting.check()) { + jump(); } - + // set sprite - sprite->set_action(physic.get_velocity_y() > 0 ? "normal" : "down"); - + if(!frozen) + sprite->set_action(physic.get_velocity_y() < 0 ? "normal" : "down"); + // we can't afford flying out of the tilemap, 'cause the engine would remove us. if ((get_pos().y - 31.8) < 0) // too high, let us fall { - physic.set_velocity_y(0); - physic.enable_gravity(true); - } - else if (Sector::current() && // spares us from possible segfaults - (get_pos().y - 31.8) > (Sector::current()->solids->get_height() * 32)) // too low, wait. - { - start_waiting(); + physic.set_velocity_y(0); + physic.enable_gravity(true); } } void Fish::start_waiting() { - waiting_for = 0; - waiting = true; + waiting.start(FISH_WAIT_TIME); + set_colgroup_active(COLGROUP_DISABLED); physic.enable_gravity(false); physic.set_velocity_y(0); } +void +Fish::jump() +{ + physic.set_velocity_y(FISH_JUMP_POWER); + physic.enable_gravity(true); + set_colgroup_active(COLGROUP_MOVING); +} + +void +Fish::freeze() +{ + BadGuy::freeze(); + sprite->set_action(physic.get_velocity_y() < 0 ? "iced" : "iced-down"); + waiting.stop(); +} + +void +Fish::unfreeze() +{ // does this happen at all? (or do fishes die when they fall frozen?) + BadGuy::unfreeze(); + start_waiting(); +} + +bool +Fish::is_freezable() const +{ + return true; +} + IMPLEMENT_FACTORY(Fish, "fish")