X-Git-Url: https://git.octo.it/?a=blobdiff_plain;ds=sidebyside;f=src%2Fbadguy%2Fstalactite.cpp;h=5e6e8375370e268f440867c5679f6247d4e1b1a5;hb=d31cd058683a79db981a2f4fc06956ad71414f26;hp=b562ff2b9f237722406d47fb87f76aa2c89391ce;hpb=5e12121c11b1f2b6fe32fa285a6a40eece80fa91;p=supertux.git diff --git a/src/badguy/stalactite.cpp b/src/badguy/stalactite.cpp index b562ff2b9..5e6e83753 100644 --- a/src/badguy/stalactite.cpp +++ b/src/badguy/stalactite.cpp @@ -1,74 +1,136 @@ -#include +// 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 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 +// 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, see . -#include "stalactite.h" +#include "badguy/stalactite.hpp" -static const int SHAKE_RANGE = 40; -static const float SHAKE_TIME = .8; -static const float SQUISH_TIME = 2; +#include "audio/sound_manager.hpp" +#include "math/random_generator.hpp" +#include "object/bullet.hpp" +#include "object/player.hpp" +#include "sprite/sprite.hpp" +#include "supertux/object_factory.hpp" -Stalactite::Stalactite(const lisp::Lisp& lisp) -{ - lisp.get("x", start_position.x); - lisp.get("y", start_position.y); - bbox.set_size(31.8, 31.8); - sprite = sprite_manager->create("stalactite"); - state = STALACTITE_HANGING; -} +static const int SHAKE_RANGE_X = 40; +static const float SHAKE_TIME = .8f; +static const float SHAKE_RANGE_Y = 400; -void -Stalactite::write(lisp::Writer& writer) +Stalactite::Stalactite(const Reader& lisp) : + BadGuy(lisp, "images/creatures/stalactite/stalactite.sprite", LAYER_TILES - 1), + timer(), + state(STALACTITE_HANGING), + shake_delta() { - writer.start_list("stalactite"); - writer.write_float("x", start_position.x); - writer.write_float("y", start_position.y); - writer.end_list("stalactite"); + countMe = false; + set_colgroup_active(COLGROUP_TOUCHABLE); + sound_manager->preload("sounds/cracking.wav"); + sound_manager->preload("sounds/sizzle.ogg"); + sound_manager->preload("sounds/icecrash.ogg"); } void -Stalactite::active_action(float elapsed_time) +Stalactite::active_update(float elapsed_time) { if(state == STALACTITE_HANGING) { - Player* player = Sector::current()->player; - if(player->get_bbox().p2.x > bbox.p1.x - SHAKE_RANGE - && player->get_bbox().p1.x < bbox.p2.x + SHAKE_RANGE - && player->get_bbox().p2.y > bbox.p1.y) { - timer.start(SHAKE_TIME); - state = STALACTITE_SHAKING; + Player* player = this->get_nearest_player(); + if (player) { + if(player->get_bbox().p2.x > bbox.p1.x - SHAKE_RANGE_X + && player->get_bbox().p1.x < bbox.p2.x + SHAKE_RANGE_X + && player->get_bbox().p2.y > bbox.p1.y + && player->get_bbox().p1.y < bbox.p2.y + SHAKE_RANGE_Y) { + timer.start(SHAKE_TIME); + state = STALACTITE_SHAKING; + sound_manager->play("sounds/cracking.wav", get_pos()); + } } } else if(state == STALACTITE_SHAKING) { + shake_delta = Vector(graphicsRandom.rand(-3,3), 0); if(timer.check()) { state = STALACTITE_FALLING; physic.enable_gravity(true); + set_colgroup_active(COLGROUP_MOVING); } - } else if(state == STALACTITE_FALLING || state == STALACTITE_SQUISHED) { + } else if(state == STALACTITE_FALLING) { movement = physic.get_movement(elapsed_time); - if(state == STALACTITE_SQUISHED && timer.check()) - remove_me(); } } -HitResponse -Stalactite::collision_solid(GameObject& , const CollisionHit& hit) +void +Stalactite::squish() { - if(state != STALACTITE_FALLING && state != STALACTITE_SQUISHED) - return FORCE_MOVE; - - if(fabsf(hit.normal.y) > .5) { // hit floor or roof? - state = STALACTITE_SQUISHED; + state = STALACTITE_SQUISHED; + physic.enable_gravity(true); + physic.set_velocity_x(0); + physic.set_velocity_y(0); + set_state(STATE_SQUISHED); + sprite->set_action("squished"); + sound_manager->play("sounds/icecrash.ogg", get_pos()); + set_group(COLGROUP_MOVING_ONLY_STATIC); + run_dead_script(); +} + +void +Stalactite::collision_solid(const CollisionHit& hit) +{ + if(state == STALACTITE_FALLING) { + if (hit.bottom) squish(); + } + if(state == STALACTITE_SQUISHED) { physic.set_velocity_y(0); - sprite->set_action("squished"); - if(!timer.started()) - timer.start(SQUISH_TIME); } - - return CONTINUE; } HitResponse Stalactite::collision_player(Player& player, const CollisionHit& ) { if(state != STALACTITE_SQUISHED) { - player.kill(Player::SHRINK); + player.kill(false); + } + + return FORCE_MOVE; +} + +HitResponse +Stalactite::collision_badguy(BadGuy& other, const CollisionHit& hit) +{ + if (state == STALACTITE_SQUISHED) return FORCE_MOVE; + + // ignore other Stalactites + if (dynamic_cast(&other)) return FORCE_MOVE; + + if (state != STALACTITE_FALLING) return BadGuy::collision_badguy(other, hit); + + if (other.is_freezable()) { + other.freeze(); + } else { + other.kill_fall(); + } + + return FORCE_MOVE; +} + +HitResponse +Stalactite::collision_bullet(Bullet& bullet, const CollisionHit& ) +{ + if(state == STALACTITE_HANGING) { + timer.start(SHAKE_TIME); + state = STALACTITE_SHAKING; + bullet.remove_me(); + if(bullet.get_type() == FIRE_BONUS) + sound_manager->play("sounds/sizzle.ogg", get_pos()); + sound_manager->play("sounds/cracking.wav", get_pos()); } return FORCE_MOVE; @@ -82,13 +144,15 @@ Stalactite::kill_fall() void Stalactite::draw(DrawingContext& context) { - if(get_state() != STATE_ACTIVE) + if(get_state() == STATE_INIT || get_state() == STATE_INACTIVE) return; - - if(state == STALACTITE_SHAKING) { - sprite->draw(context, get_pos() + Vector((rand() % 6)-3, 0), LAYER_OBJECTS); - } else { + + if(state == STALACTITE_SQUISHED) { sprite->draw(context, get_pos(), LAYER_OBJECTS); + } else if(state == STALACTITE_SHAKING) { + sprite->draw(context, get_pos() + shake_delta, layer); + } else { + sprite->draw(context, get_pos(), layer); } } @@ -99,13 +163,4 @@ Stalactite::deactivate() remove_me(); } -bool -Stalactite::is_harmful() -{ - if (state == STALACTITE_SQUISHED) - return false; - else - return true; -} - -IMPLEMENT_FACTORY(Stalactite, "stalactite") +/* EOF */