fade out console
[supertux.git] / src / badguy / yeti.cpp
index 8ab9f9d..f34a550 100644 (file)
@@ -1,16 +1,40 @@
+//  $Id$
+// 
+//  SuperTux
+//  Copyright (C) 2005 Matthias Braun <matze@braunis.de>
+//
+//  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 <config.h>
 
 #include <float.h>
-#include "yeti.h"
-#include "object/camera.h"
-#include "yeti_stalactite.h"
+#include <sstream>
+#include <memory>
+#include "yeti.hpp"
+#include "object/camera.hpp"
+#include "yeti_stalactite.hpp"
+#include "bouncing_snowball.hpp"
+#include "game_session.hpp"
 
 static const float JUMP_VEL1 = 250;
 static const float JUMP_VEL2 = 700;
-static const float RUN_SPEED = 300;
+static const float RUN_SPEED = 350;
 static const float JUMP_TIME = 1.6;
 static const float ANGRY_JUMP_WAIT = .5;
-static const float STUN_TIME = 2;
+/// the time we are safe when tux just hit us
+static const float SAFE_TIME = .5;
 static const int INITIAL_HITPOINTS = 3;
 
 Yeti::Yeti(const lisp::Lisp& reader)
@@ -18,51 +42,60 @@ Yeti::Yeti(const lisp::Lisp& reader)
   reader.get("x", start_position.x);
   reader.get("y", start_position.y);
   bbox.set_size(80, 120);
-  sprite = sprite_manager->create("yeti");
+  sprite = sprite_manager->create("images/creatures/yeti/yeti.sprite");
+  sprite->set_action("right");
   state = INIT;
   side = LEFT;
-  hitpoints = INITIAL_HITPOINTS;
-  sound_gna = SoundManager::get()->load_sound(
-      get_resource_filename("sounds/yeti_gna.wav"));
-  sound_roar = SoundManager::get()->load_sound(
-      get_resource_filename("sounds/yeti_roar.wav"));
-  jump_time_left = 0.0f;
+#if 0
+  sound_manager->preload_sound("yeti_gna");
+  sound_manager->preload_sound("yeti_roar");
+#endif
+  hit_points = INITIAL_HITPOINTS;
+  reader.get("dead-script", dead_script);
+  countMe = false;
 }
 
 Yeti::~Yeti()
 {
-  Mix_FreeChunk(sound_gna);
 }
 
 void
-Yeti::active_action(float elapsed_time)
+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:
       break;
     case GO_RIGHT:
       physic.set_velocity_x(RUN_SPEED);
-      if(jump_timer.check())
+      if(timer.check())
         physic.set_velocity_y(JUMP_VEL2);
       break;
     case GO_LEFT:
       physic.set_velocity_x(-RUN_SPEED);
-      if(jump_timer.check())
+      if(timer.check())
         physic.set_velocity_y(JUMP_VEL2);
       break;
     case ANGRY_JUMPING:
-      if(jump_timer.check()) {
+      if(timer.check()) {
         // jump
-        SoundManager::get()->play_sound(sound_gna);
+        sound_manager->play("sounds/yeti_gna.wav");
         physic.set_velocity_y(JUMP_VEL1);
+        if (side == LEFT)  // on the left, facing Tux who is on the right
+          sprite->set_action("jump-right");
+        else
+          sprite->set_action("jump-left");
       }
       break;
-
-    case STUNNED:
-        if (stun_timer.check()) {
-            go_right();
-        }
-      
     default:
       break;
   }
@@ -74,96 +107,82 @@ void
 Yeti::go_right()
 {
   // jump and move right
+  sprite->set_action("right");
   physic.set_velocity_y(JUMP_VEL1);
   physic.set_velocity_x(RUN_SPEED);
   state = GO_RIGHT;
-  jump_timer.start(JUMP_TIME);
+  timer.start(JUMP_TIME);
 }
 
 void
 Yeti::go_left()
 {
+  sprite->set_action("left");
   physic.set_velocity_y(JUMP_VEL1);
   physic.set_velocity_x(-RUN_SPEED);
   state = GO_LEFT;
-  jump_timer.start(JUMP_TIME);
+  timer.start(JUMP_TIME);
 }
 
 void
 Yeti::angry_jumping()
 {
   jumpcount = 0;
-  jump_timer.start(ANGRY_JUMP_WAIT);
+  timer.start(ANGRY_JUMP_WAIT);
   state = ANGRY_JUMPING;
   physic.set_velocity_x(0);
 }
 
 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(get_pos().x+(side == LEFT ? 64 : -64), get_pos().y, (side == LEFT ? RIGHT : LEFT)));
 }
 
-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)
-    hitpoints--;
-    if(collision_squished(player))
-      return ABORT_MOVE;
-    else if (hitpoints <= 0) {
-      player.kill(Player::SHRINK);
-      return FORCE_MOVE;
-    }
-  }
-
-  if (state == STUNNED)
-    return ABORT_MOVE;
-
-  player.kill(Player::SHRINK);
-  return FORCE_MOVE;
-}
-
-HitResponse
-Yeti::collision_badguy(BadGuy& badguy, const CollisionHit&)
-{
-  YetiStalactite* yeti_stal = dynamic_cast<YetiStalactite*>(&badguy);
-
-  if (state == STUNNED && yeti_stal && yeti_stal->is_harmful())
-  {
-    kill_fall();
-  }
-
-  return FORCE_MOVE;
-} 
-  
-
 bool
 Yeti::collision_squished(Player& player)
 {
-  // we don't use the player object, even though it was given to us
-  (void)player;
+  if(safe_timer.started())
+    return true;
 
-  // stun yeti
-  stun();
+  player.bounce(*this);
+  sound_manager->play("sounds/yeti_roar.wav");
+  hit_points--;
+  if(hit_points <= 0) {
+    sprite->set_action("dead");
+    kill_squished(player);
 
+    // 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::write(lisp::Writer& )
+Yeti::kill_fall()
 {
+  // shooting bullets or being invincible won't work :)
+}
+
+void
+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
@@ -172,13 +191,16 @@ Yeti::drop_stalactite()
   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<YetiStalactite*> (*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;
@@ -197,15 +219,23 @@ Yeti::collision_solid(GameObject& , const CollisionHit& hit)
     physic.set_velocity_y(0);
     if(state == INIT) {
       go_right();
-    } else if(state == GO_LEFT && !jump_timer.started()) {
+    } else if(state == GO_LEFT && !timer.started()) {
       side = LEFT;
+      summon_snowball();
+      sprite->set_action("stand-right");
       angry_jumping();
-    } else if(state == GO_RIGHT && !jump_timer.started()) {
+    } else if(state == GO_RIGHT && !timer.started()) {
       side = RIGHT;
+      summon_snowball();
+      sprite->set_action("stand-left");
       angry_jumping();
     } else if(state == ANGRY_JUMPING) {
-      if(!jump_timer.started()) {
+      if(!timer.started()) {
         // we just landed
+        if (side == LEFT)  // standing on the left, facing Tux who is on the right
+             sprite->set_action("stand-right");
+           else
+             sprite->set_action("stand-left");
         jumpcount++;
         // make a stalactite falling down and shake camera a bit
         Sector::current()->camera->shake(.1, 0, 10);
@@ -219,7 +249,7 @@ Yeti::collision_solid(GameObject& , const CollisionHit& hit)
             go_left();
         } else {
           // jump again
-          jump_timer.start(ANGRY_JUMP_WAIT);
+          timer.start(ANGRY_JUMP_WAIT);
         }
       }
     }
@@ -228,18 +258,4 @@ Yeti::collision_solid(GameObject& , const CollisionHit& hit)
   return CONTINUE;
 }
 
-void
-Yeti::kill_fall()
-{
-  SoundManager::get()->play_sound(sound_roar);
-  hitpoints--;
-  if (hitpoints <= 0) {
-    SoundManager::get()->play_sound(IDToSound(SND_FALL), this,
-       Sector::current()->player->get_pos());
-    physic.set_velocity_y(0);
-    physic.enable_gravity(true);
-    set_state(STATE_FALLING);
-  }
-}
-
 IMPLEMENT_FACTORY(Yeti, "yeti")