X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fbadguy%2Fyeti.cpp;h=8528e828da02bd4f5d19e438a1bb3137af7594f7;hb=8a598e8d635ee4d629371493fef50826a38cd20d;hp=5dc3cc0eae9b5f09df990ee41713575c9598fd72;hpb=5e12121c11b1f2b6fe32fa285a6a40eece80fa91;p=supertux.git diff --git a/src/badguy/yeti.cpp b/src/badguy/yeti.cpp index 5dc3cc0ea..8528e828d 100644 --- a/src/badguy/yeti.cpp +++ b/src/badguy/yeti.cpp @@ -1,65 +1,114 @@ +// $Id$ +// +// SuperTux - Boss "Yeti" +// Copyright (C) 2005 Matthias Braun +// 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 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 -#include "yeti.h" -#include "object/camera.h" -#include "yeti_stalactite.h" +#include +#include +#include "yeti.hpp" +#include "object/camera.hpp" +#include "yeti_stalactite.hpp" +#include "bouncing_snowball.hpp" +#include "game_session.hpp" +#include "level.hpp" -static const float JUMP_VEL1 = 250; -static const float JUMP_VEL2 = 700; -static const float RUN_SPEED = 300; -static const float JUMP_TIME = 1.6; -static const float ANGRY_JUMP_WAIT = .5; -static const float STUN_TIME = 2; +namespace { + const float JUMP_DOWN_VX = 250; /**< horizontal speed while jumping off the dais */ + const float JUMP_DOWN_VY = -250; /**< vertical speed while jumping off the dais */ + + const float RUN_VX = 350; /**< horizontal speed while running */ + + const float JUMP_UP_VX = 350; /**< horizontal speed while jumping on the dais */ + const float JUMP_UP_VY = -800; /**< vertical speed while jumping on the dais */ + + const float STOMP_VY = -250; /** vertical speed while stomping on the dais */ + + const float LEFT_STAND_X = 16; /**< x-coordinate of left dais' end position */ + const float RIGHT_STAND_X = 800-60-16; /**< x-coordinate of right dais' end position */ + const float LEFT_JUMP_X = LEFT_STAND_X+224; /**< x-coordinate of from where to jump on the left dais */ + const float RIGHT_JUMP_X = RIGHT_STAND_X-224; /**< x-coordinate of from where to jump on the right dais */ + const float STOMP_WAIT = .5; /**< time we stay on the dais before jumping again */ + const float SAFE_TIME = .5; /**< the time we are safe when tux just hit us */ + const int INITIAL_HITPOINTS = 3; /**< number of hits we can take */ + + const float SQUISH_TIME = 5; +} Yeti::Yeti(const lisp::Lisp& reader) + : BadGuy(reader, "images/creatures/yeti/yeti.sprite") { - reader.get("x", start_position.x); - reader.get("y", start_position.y); - bbox.set_size(80, 120); - sprite = sprite_manager->create("yeti"); - state = INIT; - side = LEFT; - sound_gna = SoundManager::get()->load_sound( - get_resource_filename("sounds/yeti_gna.wav")); - jump_time_left = 0.0f; + hit_points = INITIAL_HITPOINTS; + reader.get("dead-script", dead_script); + countMe = false; + sound_manager->preload("sounds/yeti_gna.wav"); + sound_manager->preload("sounds/yeti_roar.wav"); } Yeti::~Yeti() { - Mix_FreeChunk(sound_gna); } void -Yeti::active_action(float elapsed_time) +Yeti::activate() +{ + dir = RIGHT; + jump_down(); +} + +void +Yeti::draw(DrawingContext& context) +{ + // we blink when we are safe + if(safe_timer.started() && size_t(game_time*40)%2) + return; + + BadGuy::draw(context); +} + +void +Yeti::active_update(float elapsed_time) { switch(state) { - case INIT: + case JUMP_DOWN: + physic.set_velocity_x((dir==RIGHT)?+JUMP_DOWN_VX:-JUMP_DOWN_VX); break; - case GO_RIGHT: - physic.set_velocity_x(RUN_SPEED); - if(jump_timer.check()) - physic.set_velocity_y(JUMP_VEL2); + case RUN: + physic.set_velocity_x((dir==RIGHT)?+RUN_VX:-RUN_VX); + if (((dir == RIGHT) && (get_pos().x >= RIGHT_JUMP_X)) || ((dir == LEFT) && (get_pos().x <= LEFT_JUMP_X))) jump_up(); break; - case GO_LEFT: - physic.set_velocity_x(-RUN_SPEED); - if(jump_timer.check()) - physic.set_velocity_y(JUMP_VEL2); + case JUMP_UP: + physic.set_velocity_x((dir==RIGHT)?+JUMP_UP_VX:-JUMP_UP_VX); + if (((dir == RIGHT) && (get_pos().x >= RIGHT_STAND_X)) || ((dir == LEFT) && (get_pos().x <= LEFT_STAND_X))) be_angry(); break; - case ANGRY_JUMPING: - if(jump_timer.check()) { - // jump - SoundManager::get()->play_sound(sound_gna); - physic.set_velocity_y(JUMP_VEL1); + case BE_ANGRY: + if(state_timer.check()) { + sound_manager->play("sounds/yeti_gna.wav"); + physic.set_velocity_y(STOMP_VY); + sprite->set_action((dir==RIGHT)?"stomp-right":"stomp-left"); } break; - - case STUNNED: - if (stun_timer.check()) { - go_right(); - } - - default: + case SQUISHED: + if (state_timer.check()) { + remove_me(); + } break; } @@ -67,115 +116,141 @@ Yeti::active_action(float elapsed_time) } void -Yeti::go_right() +Yeti::jump_down() +{ + sprite->set_action((dir==RIGHT)?"jump-right":"jump-left"); + physic.set_velocity_x((dir==RIGHT)?(+JUMP_DOWN_VX):(-JUMP_DOWN_VX)); + physic.set_velocity_y(JUMP_DOWN_VY); + state = JUMP_DOWN; +} + +void +Yeti::run() { - // jump and move right - physic.set_velocity_y(JUMP_VEL1); - physic.set_velocity_x(RUN_SPEED); - state = GO_RIGHT; - jump_timer.start(JUMP_TIME); + sprite->set_action((dir==RIGHT)?"run-right":"run-left"); + physic.set_velocity_x((dir==RIGHT)?(+RUN_VX):(-RUN_VX)); + physic.set_velocity_y(0); + state = RUN; } void -Yeti::go_left() +Yeti::jump_up() { - physic.set_velocity_y(JUMP_VEL1); - physic.set_velocity_x(-RUN_SPEED); - state = GO_LEFT; - jump_timer.start(JUMP_TIME); + sprite->set_action((dir==RIGHT)?"jump-right":"jump-left"); + physic.set_velocity_x((dir==RIGHT)?(+JUMP_UP_VX):(-JUMP_UP_VX)); + physic.set_velocity_y(JUMP_UP_VY); + state = JUMP_UP; } void -Yeti::angry_jumping() +Yeti::be_angry() { - jumpcount = 0; - jump_timer.start(ANGRY_JUMP_WAIT); - state = ANGRY_JUMPING; + //turn around + dir = (dir==RIGHT)?LEFT:RIGHT; + + sprite->set_action((dir==RIGHT)?"stand-right":"stand-left"); physic.set_velocity_x(0); + physic.set_velocity_y(0); + if (hit_points < INITIAL_HITPOINTS) summon_snowball(); + stomp_count = 0; + state = BE_ANGRY; + state_timer.start(STOMP_WAIT); } void -Yeti::stun() +Yeti::summon_snowball() { - physic.set_acceleration(0.0f, 0.0f); - physic.set_velocity(0.0f, 0.0f); - jump_time_left = jump_timer.get_timeleft(); - jump_timer.stop(); - stun_timer.start(STUN_TIME); - state = STUNNED; + Sector::current()->add_object(new BouncingSnowball(Vector(get_pos().x+(dir == RIGHT ? 64 : -64), get_pos().y), dir)); } -HitResponse -Yeti::collision_player(Player& player, const CollisionHit& hit) -{ - if(player.is_invincible()) { - kill_fall(); - return ABORT_MOVE; - } - if(hit.normal.y > .9) { - //TODO: fix inaccuracy (tux sometimes dies even if badguy was hit) - // give badguys some invincible time (prevent them from being hit multiple times) - // use hitpoints also when hit by fireball or invincible tux - hitpoints--; - if(collision_squished(player)) - return ABORT_MOVE; - else if (hitpoints <= 0) { - player.kill(Player::SHRINK); - return FORCE_MOVE; - } - } +bool +Yeti::collision_squished(Player& player) +{ + kill_squished(player); - if (state == STUNNED) - return ABORT_MOVE; + return true; +} - player.kill(Player::SHRINK); - return FORCE_MOVE; +void +Yeti::kill_squished(Player& player) +{ + player.bounce(*this); + take_hit(player); } -HitResponse -Yeti::collision_badguy(BadGuy& badguy, const CollisionHit&) +void Yeti::take_hit(Player& ) { - YetiStalactite* yeti_stal = dynamic_cast(&badguy); + if(safe_timer.started()) + return; - if (state == STUNNED && yeti_stal && yeti_stal->is_harmful()) - { - kill_fall(); - } + sound_manager->play("sounds/yeti_roar.wav"); + hit_points--; - return FORCE_MOVE; -} - + if(hit_points <= 0) { + // We're dead + physic.enable_gravity(true); + physic.set_velocity_x(0); + physic.set_velocity_y(0); + + state = SQUISHED; + state_timer.start(SQUISH_TIME); + set_group(COLGROUP_MOVING_ONLY_STATIC); + sprite->set_action("dead"); -bool -Yeti::collision_squished(Player& player) -{ - // we don't use the player object, even though it was given to us - (void)player; + if (countMe) Sector::current()->get_level()->stats.badguys++; - // stun yeti - stun(); + // start script + if(dead_script != "") { + std::istringstream stream(dead_script); + Sector::current()->run_script(stream, "Yeti - dead-script"); + } + } + else { + safe_timer.start(SAFE_TIME); + } +} - return true; +void +Yeti::kill_fall() +{ + // shooting bullets or being invincible won't work :) + take_hit(*get_nearest_player()); // FIXME: debug only(?) } void -Yeti::write(lisp::Writer& ) +Yeti::write(lisp::Writer& writer) { + writer.start_list("yeti"); + + writer.write_float("x", start_position.x); + writer.write_float("y", start_position.y); + + if(dead_script != "") { + writer.write_string("dead-script", dead_script); + } + + writer.end_list("yeti"); } void Yeti::drop_stalactite() { + // make a stalactite falling down and shake camera a bit + Sector::current()->camera->shake(.1, 0, 10); + YetiStalactite* nearest = 0; float dist = FLT_MAX; + Player* player = this->get_nearest_player(); + if (!player) return; + Sector* sector = Sector::current(); for(Sector::GameObjects::iterator i = sector->gameobjects.begin(); i != sector->gameobjects.end(); ++i) { YetiStalactite* stalactite = dynamic_cast (*i); if(stalactite && stalactite->is_hanging()) { float sdist - = fabsf(stalactite->get_pos().x - sector->player->get_pos().x); + = fabsf(stalactite->get_pos().x - player->get_pos().x); if(sdist < dist) { nearest = stalactite; dist = sdist; @@ -190,38 +265,42 @@ Yeti::drop_stalactite() HitResponse Yeti::collision_solid(GameObject& , const CollisionHit& hit) { - if(fabsf(hit.normal.y) > .5) { // hit floor or roof? + if(fabsf(hit.normal.y) > .5) { + // hit floor or roof physic.set_velocity_y(0); - if(state == INIT) { - go_right(); - } else if(state == GO_LEFT && !jump_timer.started()) { - side = LEFT; - angry_jumping(); - } else if(state == GO_RIGHT && !jump_timer.started()) { - side = RIGHT; - angry_jumping(); - } else if(state == ANGRY_JUMPING) { - if(!jump_timer.started()) { - // we just landed - jumpcount++; - // make a stalactite falling down and shake camera a bit - Sector::current()->camera->shake(.1, 0, 10); - drop_stalactite(); - - // go to other side after 3 jumps - if(jumpcount == 3) { - if(side == LEFT) - go_right(); - else - go_left(); - } else { - // jump again - jump_timer.start(ANGRY_JUMP_WAIT); - } - } + switch (state) { + case JUMP_DOWN: + run(); + break; + case RUN: + break; + case JUMP_UP: + break; + case BE_ANGRY: + // we just landed + if(!state_timer.started()) { + sprite->set_action((dir==RIGHT)?"stand-right":"stand-left"); + stomp_count++; + drop_stalactite(); + + // go to other side after 3 jumps + if(stomp_count == 3) { + jump_down(); + } else { + // jump again + state_timer.start(STOMP_WAIT); + } + } + break; + case SQUISHED: + break; } + } else + if(fabsf(hit.normal.x) > .5) { + // hit wall + jump_up(); } - + return CONTINUE; }