X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fbadguy%2Fmriceblock.cpp;h=3361f41ab2af87eb25cca11100d5c7044a2ae51d;hb=4c9b57d29e67f9db5605f378549b07f670baa837;hp=5e71413676ca523e1482aa325e8f7f28f21db370;hpb=d46c78c842ab4090a3f46e560c891234167f124b;p=supertux.git diff --git a/src/badguy/mriceblock.cpp b/src/badguy/mriceblock.cpp index 5e7141367..3361f41ab 100644 --- a/src/badguy/mriceblock.cpp +++ b/src/badguy/mriceblock.cpp @@ -1,27 +1,59 @@ +// $Id$ +// +// SuperTux +// 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 +// 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 "mriceblock.h" +#include "mriceblock.hpp" +#include "object/block.hpp" static const float WALKSPEED = 80; static const float KICKSPEED = 500; static const int MAXSQUISHES = 10; -MrIceBlock::MrIceBlock(LispReader& reader) +MrIceBlock::MrIceBlock(const lisp::Lisp& reader) + : ice_state(ICESTATE_NORMAL), squishcount(0) +{ + reader.get("x", start_position.x); + reader.get("y", start_position.y); + bbox.set_size(31.8, 31.8); + sprite = sprite_manager->create("images/creatures/mr_iceblock/mr_iceblock.sprite"); + set_direction = false; +} + +MrIceBlock::MrIceBlock(float pos_x, float pos_y, Direction d) : ice_state(ICESTATE_NORMAL), squishcount(0) { - reader.read_float("x", start_position.x); - reader.read_float("y", start_position.y); - bbox.set_size(32, 32); - sprite = sprite_manager->create("mriceblock"); + start_position.x = pos_x; + start_position.y = pos_y; + bbox.set_size(31.8, 31.8); + sprite = sprite_manager->create("images/creatures/mr_iceblock/mr_iceblock.sprite"); + set_direction = true; + initial_direction = d; } void -MrIceBlock::write(LispWriter& writer) +MrIceBlock::write(lisp::Writer& writer) { writer.start_list("mriceblock"); - writer.write_float("x", get_pos().x); - writer.write_float("y", get_pos().y); + writer.write_float("x", start_position.x); + writer.write_float("y", start_position.y); writer.end_list("mriceblock"); } @@ -29,24 +61,36 @@ MrIceBlock::write(LispWriter& writer) void MrIceBlock::activate() { + if (set_direction) { + dir = initial_direction; + } + physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED); sprite->set_action(dir == LEFT ? "left" : "right"); } void -MrIceBlock::active_action(float elapsed_time) +MrIceBlock::active_update(float elapsed_time) { + if(ice_state == ICESTATE_GRABBED) + return; + if(ice_state == ICESTATE_FLAT && flat_timer.check()) { - printf("unflat.\n"); - ice_state = ICESTATE_NORMAL; - physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED); + set_state(ICESTATE_NORMAL); + } + + if (ice_state == ICESTATE_NORMAL && might_fall(601)) + { + dir = (dir == LEFT ? RIGHT : LEFT); sprite->set_action(dir == LEFT ? "left" : "right"); + physic.set_velocity_x(-physic.get_velocity_x()); } - BadGuy::active_action(elapsed_time); + + BadGuy::active_update(elapsed_time); } HitResponse -MrIceBlock::collision_solid(GameObject& other, const CollisionHit& hit) +MrIceBlock::collision_solid(GameObject& object, const CollisionHit& hit) { if(fabsf(hit.normal.y) > .5) { // floor or roof physic.set_velocity_y(0); @@ -59,21 +103,89 @@ MrIceBlock::collision_solid(GameObject& other, const CollisionHit& hit) sprite->set_action(dir == LEFT ? "left" : "right"); physic.set_velocity_x(-physic.get_velocity_x()); break; - case ICESTATE_KICKED: + case ICESTATE_KICKED: { + BonusBlock* bonusblock = dynamic_cast (&object); + if(bonusblock) { + bonusblock->try_open(); + } + Brick* brick = dynamic_cast (&object); + if(brick) { + brick->try_break(); + } + dir = dir == LEFT ? RIGHT : LEFT; sprite->set_action(dir == LEFT ? "flat-left" : "flat-right"); physic.set_velocity_x(-physic.get_velocity_x()); - SoundManager::get()->play_sound(IDToSound(SND_RICOCHET), get_pos(), - Sector::current()->player->get_pos()); + sound_manager->play("sounds/iceblock_bump.wav", get_pos()); break; + } case ICESTATE_FLAT: physic.set_velocity_x(0); break; + case ICESTATE_GRABBED: + return FORCE_MOVE; } return CONTINUE; } +HitResponse +MrIceBlock::collision(GameObject& object, const CollisionHit& hit) +{ + if(ice_state == ICESTATE_GRABBED) + return FORCE_MOVE; + + return BadGuy::collision(object, hit); +} + +HitResponse +MrIceBlock::collision_player(Player& player, const CollisionHit& hit) +{ + if(ice_state == ICESTATE_GRABBED) + return FORCE_MOVE; + + // handle kicks from left or right side + if(ice_state == ICESTATE_FLAT && get_state() == STATE_ACTIVE) { + // hit from left side + if(hit.normal.x > 0.7) { + dir = RIGHT; + player.kick(); + set_state(ICESTATE_KICKED); + return FORCE_MOVE; + } else if(hit.normal.x < -0.7) { + dir = LEFT; + player.kick(); + set_state(ICESTATE_KICKED); + return FORCE_MOVE; + } + } + + return BadGuy::collision_player(player, hit); +} + +HitResponse +MrIceBlock::collision_badguy(BadGuy& badguy, const CollisionHit& hit) +{ + switch(ice_state) { + case ICESTATE_NORMAL: + if(fabsf(hit.normal.x) > .8) { + dir = dir == LEFT ? RIGHT : LEFT; + sprite->set_action(dir == LEFT ? "left" : "right"); + physic.set_velocity_x(-physic.get_velocity_x()); + } + return CONTINUE; + case ICESTATE_FLAT: + return FORCE_MOVE; + case ICESTATE_KICKED: + badguy.kill_fall(); + return FORCE_MOVE; + default: + assert(false); + } + + return ABORT_MOVE; +} + bool MrIceBlock::collision_squished(Player& player) { @@ -86,31 +198,18 @@ MrIceBlock::collision_squished(Player& player) return true; } - // flatten - SoundManager::get()->play_sound(IDToSound(SND_STOMP), get_pos(), - player.get_pos()); - physic.set_velocity_x(0); - physic.set_velocity_y(0); - - sprite->set_action(dir == LEFT ? "flat-left" : "flat-right"); - flat_timer.start(4); - ice_state = ICESTATE_FLAT; - printf("flat.\n"); + set_state(ICESTATE_FLAT); break; case ICESTATE_FLAT: - // kick - SoundManager::get()->play_sound(IDToSound(SND_KICK), this, - player.get_pos()); - if(player.get_pos().x < get_pos().x) { dir = RIGHT; } else { dir = LEFT; } - physic.set_velocity_x(dir == LEFT ? -KICKSPEED : KICKSPEED); - sprite->set_action(dir == LEFT ? "flat-left" : "flat-right"); - ice_state = ICESTATE_KICKED; - printf("kicked.\n"); + set_state(ICESTATE_KICKED); + break; + case ICESTATE_GRABBED: + assert(false); break; } @@ -118,3 +217,67 @@ MrIceBlock::collision_squished(Player& player) return true; } +void +MrIceBlock::set_state(IceState state) +{ + if(ice_state == state) + return; + + if(state == ICESTATE_FLAT) + flags |= FLAG_PORTABLE; + else + flags &= ~FLAG_PORTABLE; + + if(ice_state == ICESTATE_KICKED) { + bbox.set_size(31.8, 31.8); + } + + switch(state) { + case ICESTATE_NORMAL: + physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED); + sprite->set_action(dir == LEFT ? "left" : "right"); + break; + case ICESTATE_FLAT: + sound_manager->play("sounds/stomp.wav", get_pos()); + physic.set_velocity_x(0); + physic.set_velocity_y(0); + + sprite->set_action(dir == LEFT ? "flat-left" : "flat-right"); + flat_timer.start(4); + break; + case ICESTATE_KICKED: + sound_manager->play("sounds/kick.wav", get_pos()); + + physic.set_velocity_x(dir == LEFT ? -KICKSPEED : KICKSPEED); + sprite->set_action(dir == LEFT ? "flat-left" : "flat-right"); + // we should slide above 1 block holes now... + bbox.set_size(34, 31.8); + break; + case ICESTATE_GRABBED: + flat_timer.stop(); + break; + default: + assert(false); + } + ice_state = state; +} + +void +MrIceBlock::grab(MovingObject&, const Vector& pos, Direction dir) +{ + movement = pos - get_pos(); + this->dir = dir; + sprite->set_action(dir == LEFT ? "flat-left" : "flat-right"); + set_state(ICESTATE_GRABBED); + set_group(COLGROUP_DISABLED); +} + +void +MrIceBlock::ungrab(MovingObject& , Direction dir) +{ + this->dir = dir; + set_state(ICESTATE_KICKED); + set_group(COLGROUP_MOVING); +} + +IMPLEMENT_FACTORY(MrIceBlock, "mriceblock")