X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fbadguy%2Fmriceblock.cpp;h=f87539b79510d5de8578bffb88789593f20d66a7;hb=146cb5cf3c1ea9bd8f884e3c99a7789c42d598f1;hp=732793742938e70e11c7c113fe60f3eaffe1334d;hpb=c0fed8bdef7dbc9540b476fd28949ebad7d3c2ba;p=supertux.git diff --git a/src/badguy/mriceblock.cpp b/src/badguy/mriceblock.cpp index 732793742..f87539b79 100644 --- a/src/badguy/mriceblock.cpp +++ b/src/badguy/mriceblock.cpp @@ -1,12 +1,10 @@ -// $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 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,63 +12,57 @@ // 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 +#include "badguy/mriceblock.hpp" -#include "mriceblock.hpp" -#include "object/block.hpp" +#include "audio/sound_manager.hpp" +#include "object/player.hpp" +#include "sprite/sprite.hpp" +#include "sprite/sprite_manager.hpp" +#include "supertux/object_factory.hpp" -static const float WALKSPEED = 80; -static const float KICKSPEED = 500; -static const int MAXSQUISHES = 10; +#include -MrIceBlock::MrIceBlock(const lisp::Lisp& reader) - : ice_state(ICESTATE_NORMAL), squishcount(0) -{ - reader.get("x", start_position.x); - reader.get("y", start_position.y); - stay_on_platform = false; - reader.get("stay-on-platform", stay_on_platform); - bbox.set_size(31.8, 31.8); - sprite = sprite_manager->create("images/creatures/mr_iceblock/mr_iceblock.sprite"); - set_direction = false; +namespace { +const float KICKSPEED = 500; +const int MAXSQUISHES = 10; +const float NOKICK_TIME = 0.1f; } -MrIceBlock::MrIceBlock(float pos_x, float pos_y, Direction d, bool stay_on_plat = false ) - : ice_state(ICESTATE_NORMAL), squishcount(0) +MrIceBlock::MrIceBlock(const Reader& reader) : + WalkingBadguy(reader, "images/creatures/mr_iceblock/mr_iceblock.sprite", "left", "right"), + ice_state(ICESTATE_NORMAL), + nokick_timer(), + flat_timer(), + squishcount(0) { - start_position.x = pos_x; - start_position.y = pos_y; - stay_on_platform = stay_on_plat; - bbox.set_size(31.8, 31.8); - sprite = sprite_manager->create("images/creatures/mr_iceblock/mr_iceblock.sprite"); - set_direction = true; - initial_direction = d; + walk_speed = 80; + max_drop_height = 600; + SoundManager::current()->preload("sounds/iceblock_bump.wav"); + SoundManager::current()->preload("sounds/stomp.wav"); + SoundManager::current()->preload("sounds/kick.wav"); } -void -MrIceBlock::write(lisp::Writer& writer) +MrIceBlock::MrIceBlock(const Vector& pos, Direction d) : + WalkingBadguy(pos, d, "images/creatures/mr_iceblock/mr_iceblock.sprite", "left", "right"), + ice_state(ICESTATE_NORMAL), + nokick_timer(), + flat_timer(), + squishcount(0) { - writer.start_list("mriceblock"); - - writer.write_float("x", start_position.x); - writer.write_float("y", start_position.y); - writer.write_bool("stay-on-platform", stay_on_platform); - - writer.end_list("mriceblock"); + walk_speed = 80; + max_drop_height = 600; + SoundManager::current()->preload("sounds/iceblock_bump.wav"); + SoundManager::current()->preload("sounds/stomp.wav"); + SoundManager::current()->preload("sounds/kick.wav"); } void -MrIceBlock::activate() +MrIceBlock::initialize() { - if (set_direction) { - dir = initial_direction; - } - - physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED); - sprite->set_action(dir == LEFT ? "left" : "right"); + WalkingBadguy::initialize(); + set_state(ICESTATE_NORMAL); } void @@ -83,56 +75,51 @@ MrIceBlock::active_update(float elapsed_time) set_state(ICESTATE_NORMAL); } - if (ice_state == ICESTATE_NORMAL && - stay_on_platform && - may_fall_off_platform()) + if (ice_state == ICESTATE_NORMAL) { - dir = (dir == LEFT ? RIGHT : LEFT); - sprite->set_action(dir == LEFT ? "left" : "right"); - physic.set_velocity_x(-physic.get_velocity_x()); + WalkingBadguy::active_update(elapsed_time); + return; } BadGuy::active_update(elapsed_time); } -HitResponse -MrIceBlock::collision_solid(GameObject& object, const CollisionHit& hit) +bool +MrIceBlock::can_break(){ + return ice_state == ICESTATE_KICKED; +} + +void +MrIceBlock::collision_solid(const CollisionHit& hit) { - if(fabsf(hit.normal.y) > .5) { // floor or roof + update_on_ground_flag(hit); + + if(hit.top || hit.bottom) { // floor or roof physic.set_velocity_y(0); - return CONTINUE; } + // hit left or right switch(ice_state) { case ICESTATE_NORMAL: - dir = dir == LEFT ? RIGHT : LEFT; - sprite->set_action(dir == LEFT ? "left" : "right"); - physic.set_velocity_x(-physic.get_velocity_x()); + WalkingBadguy::collision_solid(hit); break; case ICESTATE_KICKED: { - BonusBlock* bonusblock = dynamic_cast (&object); - if(bonusblock) { - bonusblock->try_open(); + if((hit.right && dir == RIGHT) || (hit.left && dir == LEFT)) { + dir = (dir == LEFT) ? RIGHT : LEFT; + SoundManager::current()->play("sounds/iceblock_bump.wav", get_pos()); + physic.set_velocity_x(-physic.get_velocity_x()*.975); } - 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()); - sound_manager->play("sounds/iceblock_bump.wav", get_pos()); + this->set_action(dir == LEFT ? "flat-left" : "flat-right", /* loops = */ -1); + if(fabsf(physic.get_velocity_x()) < walk_speed*1.5) + set_state(ICESTATE_NORMAL); break; } case ICESTATE_FLAT: physic.set_velocity_x(0); break; case ICESTATE_GRABBED: - return FORCE_MOVE; + break; } - - return CONTINUE; } HitResponse @@ -147,25 +134,21 @@ MrIceBlock::collision(GameObject& object, const CollisionHit& 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) { + if(hit.left) { dir = RIGHT; player.kick(); set_state(ICESTATE_KICKED); return FORCE_MOVE; - } else if(hit.normal.x < -0.7) { + } else if(hit.right) { dir = LEFT; player.kick(); set_state(ICESTATE_KICKED); return FORCE_MOVE; } } - + return BadGuy::collision_player(player, hit); } @@ -174,12 +157,7 @@ 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; + return WalkingBadguy::collision_badguy(badguy, hit); case ICESTATE_FLAT: return FORCE_MOVE; case ICESTATE_KICKED: @@ -193,71 +171,87 @@ MrIceBlock::collision_badguy(BadGuy& badguy, const CollisionHit& hit) } bool -MrIceBlock::collision_squished(Player& player) +MrIceBlock::collision_squished(GameObject& object) { + Player* player = dynamic_cast(&object); + if(player && (player->does_buttjump || player->is_invincible())) { + player->bounce(*this); + kill_fall(); + return true; + } + switch(ice_state) { case ICESTATE_KICKED: + { + BadGuy* badguy = dynamic_cast(&object); + if (badguy) { + badguy->kill_fall(); + break; + } + } + + // fall through case ICESTATE_NORMAL: + { squishcount++; - if(squishcount >= MAXSQUISHES) { + if (squishcount >= MAXSQUISHES) { kill_fall(); return true; } + } - set_state(ICESTATE_FLAT); - break; + set_state(ICESTATE_FLAT); + nokick_timer.start(NOKICK_TIME); + break; case ICESTATE_FLAT: - if(player.get_pos().x < get_pos().x) { + { + MovingObject* movingobject = dynamic_cast(&object); + if (movingobject && (movingobject->get_pos().x < get_pos().x)) { dir = RIGHT; } else { dir = LEFT; } - set_state(ICESTATE_KICKED); - break; + } + if (nokick_timer.check()) set_state(ICESTATE_KICKED); + break; case ICESTATE_GRABBED: assert(false); break; } - player.bounce(*this); + if (player) player->bounce(*this); return true; } void -MrIceBlock::set_state(IceState state) +MrIceBlock::set_state(IceState state_, bool up) { - if(ice_state == 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) { + switch(state_) { case ICESTATE_NORMAL: - physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED); - sprite->set_action(dir == LEFT ? "left" : "right"); + this->set_action(dir == LEFT ? "left" : "right", /* loops = */ -1); + WalkingBadguy::initialize(); 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"); + if(up) { + physic.set_velocity_y(-KICKSPEED); + } else { + SoundManager::current()->play("sounds/stomp.wav", get_pos()); + physic.set_velocity_x(0); + physic.set_velocity_y(0); + } + this->set_action(dir == LEFT ? "flat-left" : "flat-right", /* loops = */ -1); flat_timer.start(4); break; case ICESTATE_KICKED: - sound_manager->play("sounds/kick.wav", get_pos()); + SoundManager::current()->play("sounds/kick.wav", get_pos()); physic.set_velocity_x(dir == LEFT ? -KICKSPEED : KICKSPEED); - sprite->set_action(dir == LEFT ? "flat-left" : "flat-right"); + this->set_action(dir == LEFT ? "flat-left" : "flat-right", /* loops = */ -1); // we should slide above 1 block holes now... - bbox.set_size(34, 31.8); + bbox.set_size(34, 31.8f); break; case ICESTATE_GRABBED: flat_timer.stop(); @@ -265,25 +259,42 @@ MrIceBlock::set_state(IceState state) default: assert(false); } - ice_state = state; + ice_state = state_; } void -MrIceBlock::grab(MovingObject&, const Vector& pos, Direction dir) +MrIceBlock::grab(MovingObject&, const Vector& pos, Direction dir_) { movement = pos - get_pos(); - this->dir = dir; - sprite->set_action(dir == LEFT ? "flat-left" : "flat-right"); + this->dir = dir_; + this->set_action(dir_ == LEFT ? "flat-left" : "flat-right", /* loops = */ -1); set_state(ICESTATE_GRABBED); - set_group(COLGROUP_DISABLED); + set_colgroup_active(COLGROUP_DISABLED); } void -MrIceBlock::ungrab(MovingObject& , Direction dir) +MrIceBlock::ungrab(MovingObject& , Direction dir_) +{ + if(dir_ == UP) { + set_state(ICESTATE_FLAT, true); + } else { + this->dir = dir_; + set_state(ICESTATE_KICKED); + } + set_colgroup_active(COLGROUP_MOVING); +} + +bool +MrIceBlock::is_portable() const +{ + return ice_state == ICESTATE_FLAT; +} + +SmartBlock::SmartBlock(const Reader& reader) : + MrIceBlock(reader) { - this->dir = dir; - set_state(ICESTATE_KICKED); - set_group(COLGROUP_MOVING); + max_drop_height = 16; + sprite = SpriteManager::current()->create("images/creatures/mr_iceblock/smart_block/smart_block.sprite"); } -IMPLEMENT_FACTORY(MrIceBlock, "mriceblock") +/* EOF */