added new class for particle systems that need absolute coordinates (currently only...
[supertux.git] / src / badguy / yeti.cpp
index 20b849c..c38c3ac 100644 (file)
@@ -1,17 +1,41 @@
+//  $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 <sstream>
 #include "yeti.h"
 #include "object/camera.h"
 #include "yeti_stalactite.h"
+#include "bouncing_snowball.h"
+#include "game_session.h"
+#include "scripting/script_interpreter.h"
 
 static const float JUMP_VEL1 = 250;
 static const float JUMP_VEL2 = 700;
 static const float RUN_SPEED = 350;
 static const float JUMP_TIME = 1.6;
 static const float ANGRY_JUMP_WAIT = .5;
+/// the time we are safe when tux just hit us
+static const float SAFE_TIME = .5;
 static const int INITIAL_HITPOINTS = 3;
-static const int INITIAL_BULLET_HP = 10;
 
 Yeti::Yeti(const lisp::Lisp& reader)
 {
@@ -19,23 +43,31 @@ Yeti::Yeti(const lisp::Lisp& reader)
   reader.get("y", start_position.y);
   bbox.set_size(80, 120);
   sprite = sprite_manager->create("yeti");
+  sprite->set_action("right");
   state = INIT;
   side = LEFT;
-  hitpoints = INITIAL_HITPOINTS;
-  bullet_hitpoints = INITIAL_BULLET_HP;
-  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"));
+  sound_manager->preload_sound("yeti_gna");
+  sound_manager->preload_sound("yeti_roar");
+  hit_points = INITIAL_HITPOINTS;
+  reader.get("dead-script", dead_script);
 }
 
 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(global_time*40)%2)
+    return;
+
+  BadGuy::draw(context);
+}
+
+void
+Yeti::active_update(float elapsed_time)
 {
   switch(state) {
     case INIT:
@@ -53,7 +85,7 @@ Yeti::active_action(float elapsed_time)
     case ANGRY_JUMPING:
       if(timer.check()) {
         // jump
-        SoundManager::get()->play_sound(sound_gna);
+        sound_manager->play_sound("yeti_gna");
         physic.set_velocity_y(JUMP_VEL1);
       }
       break;
@@ -92,42 +124,50 @@ Yeti::angry_jumping()
   physic.set_velocity_x(0);
 }
 
-HitResponse
-Yeti::collision_player(Player& player, const CollisionHit& hit)
+void
+Yeti::summon_snowball()
 {
-  if(player.is_invincible()) {
-    kill_fall();
-    return ABORT_MOVE;
-  }
-  if(hit.normal.y > .9) {
-    hitpoints--;
-    bullet_hitpoints--;
-    SoundManager::get()->play_sound(sound_roar);
-    if(collision_squished(player))
-      return ABORT_MOVE;
-    else if (hitpoints <= 0) {
-      bullet_hitpoints = 0;
-      player.kill(Player::SHRINK);
-      return FORCE_MOVE;
-    }
-  }
-  std::cout << "COLLISION - HITPOINTS: " << hitpoints << ", BULLET HP: " << bullet_hitpoints << std::endl;
-  player.kill(Player::SHRINK);
-  return FORCE_MOVE;
+  Sector::current()->add_object(new BouncingSnowball(get_pos().x+(side == LEFT ? 64 : -64), get_pos().y, (side == LEFT ? RIGHT : LEFT)));
 }
 
 bool
 Yeti::collision_squished(Player& player)
 {
-  bool result = false;
+  if(safe_timer.started())
+    return true;
+
   player.bounce(*this);
-  if (hitpoints <= 0) {
-    bullet_hitpoints = 0;
-    //sprite->set_action("dead"); 
+  sound_manager->play_sound("yeti_roar");
+  hit_points--;
+  if(hit_points <= 0) {
+    sprite->set_action("dead");
     kill_squished(player);
-    result = true;
+
+    // start script
+    if(dead_script != "") {
+      try {
+        ScriptInterpreter* interpreter 
+          = new ScriptInterpreter(GameSession::current()->get_working_directory());
+        interpreter->register_sector(Sector::current());
+        std::istringstream in(dead_script);
+        interpreter->load_script(in, "Yeti - dead-script");
+        interpreter->start_script();
+        Sector::current()->add_object(interpreter);
+      } catch(std::exception& e) {
+        std::cerr << "Couldn't execute yeti dead script: " << e.what() << "\n";
+      }
+    }
+  } else {
+    safe_timer.start(SAFE_TIME);
   }
-  return result;
+  
+  return true;
+}
+
+void
+Yeti::kill_fall()
+{
+  // shooting bullets or being invincible won't work :)
 }
 
 void
@@ -168,9 +208,13 @@ Yeti::collision_solid(GameObject& , const CollisionHit& hit)
       go_right();
     } else if(state == GO_LEFT && !timer.started()) {
       side = LEFT;
+      summon_snowball();
+      sprite->set_action("right");
       angry_jumping();
     } else if(state == GO_RIGHT && !timer.started()) {
       side = RIGHT;
+      summon_snowball();
+      sprite->set_action("left");
       angry_jumping();
     } else if(state == ANGRY_JUMPING) {
       if(!timer.started()) {
@@ -197,19 +241,4 @@ Yeti::collision_solid(GameObject& , const CollisionHit& hit)
   return CONTINUE;
 }
 
-void
-Yeti::kill_fall()
-{
-  SoundManager::get()->play_sound(sound_roar);
-  bullet_hitpoints--;
-  if (bullet_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);
-  }
-  std::cout << "KILL_FALL - HITPOINTS: " << hitpoints << ", BULLET HP: " << bullet_hitpoints << std::endl;
-}
-
 IMPLEMENT_FACTORY(Yeti, "yeti")