Make it build with -DCOMPILE_AMALGATION=ON. Still not certain how intern_draw/next_po...
[supertux.git] / src / badguy / stalactite.cpp
index f53a944..6742c73 100644 (file)
@@ -1,12 +1,10 @@
-//  $Id$
-//
 //  SuperTux
 //  Copyright (C) 2006 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 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
 //  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 <http://www.gnu.org/licenses/>.
 
-#include <config.h>
+#include "badguy/stalactite.hpp"
 
-#include "stalactite.hpp"
-#include "random_generator.hpp"
+#include "math/random_generator.hpp"
+#include "object/player.hpp"
+#include "sprite/sprite.hpp"
+#include "supertux/object_factory.hpp"
 
 static const int SHAKE_RANGE_X = 40;
-static const float SHAKE_TIME = .8;
-static const float SQUISH_TIME = 2;
+static const float SHAKE_TIME = .8f;
 static const float SHAKE_RANGE_Y = 400;
 
-Stalactite::Stalactite(const lisp::Lisp& lisp)
-       : BadGuy(lisp, "images/creatures/stalactite/stalactite.sprite"), state(STALACTITE_HANGING)
+Stalactite::Stalactite(const Reader& lisp) :
+  BadGuy(lisp, "images/creatures/stalactite/stalactite.sprite", LAYER_TILES - 1),
+  timer(),
+  state(STALACTITE_HANGING)
 {
   countMe = false;
-}
-
-void
-Stalactite::write(lisp::Writer& writer)
-{
-  writer.start_list("stalactite");
-  writer.write_float("x", start_position.x);
-  writer.write_float("y", start_position.y);
-  writer.end_list("stalactite");
+  set_colgroup_active(COLGROUP_TOUCHABLE);
 }
 
 void
@@ -49,9 +41,9 @@ Stalactite::active_update(float elapsed_time)
     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) {
+         && 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;
       }
@@ -60,30 +52,34 @@ Stalactite::active_update(float elapsed_time)
     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()
+{
+  state = STALACTITE_SQUISHED;
+  physic.enable_gravity(true);
+  physic.set_velocity_x(0);
+  physic.set_velocity_y(0);
+  set_state(STATE_SQUISHED);
+  set_group(COLGROUP_MOVING_ONLY_STATIC);
+  run_dead_script();
+}
+
+void
+Stalactite::collision_solid(const CollisionHit& hit)
 {
-  if(state != STALACTITE_FALLING && state != STALACTITE_SQUISHED)
-    return FORCE_MOVE;
-  
-  if(hit.normal.y < .9) { // hit floor?
-    state = STALACTITE_SQUISHED;
-    set_group(COLGROUP_MOVING_ONLY_STATIC);
+  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
@@ -96,6 +92,27 @@ Stalactite::collision_player(Player& player, const CollisionHit& )
   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<Stalactite*>(&other)) return FORCE_MOVE;
+
+  if (state != STALACTITE_FALLING) return BadGuy::collision_badguy(other, hit);
+
+  if (other.is_freezable()) {
+    other.freeze();
+  } else {
+    other.kill_fall();
+  }
+
+  remove_me();
+
+  return FORCE_MOVE;
+}
+
 void
 Stalactite::kill_fall()
 {
@@ -106,11 +123,16 @@ Stalactite::draw(DrawingContext& context)
 {
   if(get_state() != STATE_ACTIVE)
     return;
-    
+
+  if(state == STALACTITE_SQUISHED) {
+    sprite->draw(context, get_pos(), LAYER_OBJECTS);
+    return;
+  }
+
   if(state == STALACTITE_SHAKING) {
-    sprite->draw(context, get_pos() + Vector(systemRandom.rand(-3,3), 0), LAYER_OBJECTS);
+    sprite->draw(context, get_pos() + Vector(systemRandom.rand(-3,3), 0), layer);
   } else {
-    sprite->draw(context, get_pos(), LAYER_OBJECTS);
+    sprite->draw(context, get_pos(), layer);
   }
 }
 
@@ -121,4 +143,4 @@ Stalactite::deactivate()
     remove_me();
 }
 
-IMPLEMENT_FACTORY(Stalactite, "stalactite")
+/* EOF */