Bug 471: Fix upward release of ice block, and grabbing cleanups.
[supertux.git] / src / badguy / mriceblock.cpp
index 7b954bf..f4cacb5 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 "badguy/mriceblock.hpp"
 
-#include <config.h>
+#include "audio/sound_manager.hpp"
+#include "object/player.hpp"
+#include "sprite/sprite.hpp"
+#include "supertux/object_factory.hpp"
 
-#include "mriceblock.hpp"
-#include "object/block.hpp"
+#include <math.h>
 
 namespace {
-  const float KICKSPEED = 500;
-  const int MAXSQUISHES = 10;
+const float KICKSPEED = 500;
+const int MAXSQUISHES = 10;
+const float NOKICK_TIME = 0.1f;
 }
 
-MrIceBlock::MrIceBlock(const lisp::Lisp& reader)
-  : WalkingBadguy(reader, "images/creatures/mr_iceblock/mr_iceblock.sprite", "left", "right"), ice_state(ICESTATE_NORMAL), squishcount(0)
+MrIceBlock::MrIceBlock(const Reader& reader) :
+  WalkingBadguy(reader, "images/creatures/mr_iceblock/mr_iceblock.sprite", "left", "right"), 
+  ice_state(ICESTATE_NORMAL), 
+  nokick_timer(),
+  flat_timer(),
+  squishcount(0)
 {
   walk_speed = 80;
   max_drop_height = 600;
@@ -37,8 +43,12 @@ MrIceBlock::MrIceBlock(const lisp::Lisp& reader)
   sound_manager->preload("sounds/kick.wav");
 }
 
-MrIceBlock::MrIceBlock(const Vector& pos, Direction d)
-  : WalkingBadguy(pos, d, "images/creatures/mr_iceblock/mr_iceblock.sprite", "left", "right"), ice_state(ICESTATE_NORMAL), squishcount(0)
+MrIceBlock::MrIceBlock(const Vector& pos, Direction d) :
+  WalkingBadguy(pos, d, "images/creatures/mr_iceblock/mr_iceblock.sprite", "left", "right"), 
+  ice_state(ICESTATE_NORMAL), 
+  nokick_timer(),
+  flat_timer(),
+  squishcount(0)
 {
   walk_speed = 80;
   max_drop_height = 600;
@@ -48,17 +58,9 @@ MrIceBlock::MrIceBlock(const Vector& pos, Direction d)
 }
 
 void
-MrIceBlock::write(lisp::Writer& writer)
+MrIceBlock::initialize()
 {
-  writer.start_list("mriceblock");
-  WalkingBadguy::write(writer);
-  writer.end_list("mriceblock");
-}
-
-void
-MrIceBlock::activate()
-{
-  WalkingBadguy::activate();
+  WalkingBadguy::initialize();
   set_state(ICESTATE_NORMAL);
 }
 
@@ -83,7 +85,7 @@ MrIceBlock::active_update(float elapsed_time)
 
 bool
 MrIceBlock::can_break(){
-    return ice_state == ICESTATE_KICKED;
+  return ice_state == ICESTATE_KICKED;
 }
 
 void
@@ -102,16 +104,14 @@ MrIceBlock::collision_solid(const CollisionHit& hit)
       WalkingBadguy::collision_solid(hit);
       break;
     case ICESTATE_KICKED: {
-      if(hit.right && dir == RIGHT) {
-        dir = LEFT;
+      if((hit.right && dir == RIGHT) || (hit.left && dir == LEFT)) {
+        dir = (dir == LEFT) ? RIGHT : LEFT;
         sound_manager->play("sounds/iceblock_bump.wav", get_pos());
-        physic.set_velocity_x(-KICKSPEED);
-      } else if(hit.left && dir == LEFT) {
-        dir = RIGHT;
-        sound_manager->play("sounds/iceblock_bump.wav", get_pos());
-        physic.set_velocity_x(KICKSPEED);
+        physic.set_velocity_x(-physic.get_velocity_x()*.975);
       }
       sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
+      if(fabsf(physic.get_velocity_x()) < walk_speed*1.5)
+        set_state(ICESTATE_NORMAL);
       break;
     }
     case ICESTATE_FLAT:
@@ -134,9 +134,6 @@ MrIceBlock::collision(GameObject& object, const CollisionHit& hit)
 HitResponse
 MrIceBlock::collision_player(Player& player, const CollisionHit& hit)
 {
-  if(ice_state == ICESTATE_GRABBED)
-    return FORCE_MOVE;
-
   // handle kicks from left or right side
   if(ice_state == ICESTATE_FLAT && get_state() == STATE_ACTIVE) {
     if(hit.left) {
@@ -174,51 +171,76 @@ MrIceBlock::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
 }
 
 bool
-MrIceBlock::collision_squished(Player& player)
+MrIceBlock::collision_squished(GameObject& object)
 {
+  Player* player = dynamic_cast<Player*>(&object);
+  if(player && (player->does_buttjump || player->is_invincible())) {
+    player->bounce(*this);
+    kill_fall();
+    return true;
+  }
+
   switch(ice_state) {
     case ICESTATE_KICKED:
+    {
+      BadGuy* badguy = dynamic_cast<BadGuy*>(&object);
+      if (badguy) {
+        badguy->kill_fall();
+        break;
+      }
+    }
+
+    // fall through
     case ICESTATE_NORMAL:
+    {
       squishcount++;
-      if(squishcount >= MAXSQUISHES) {
+      if (squishcount >= MAXSQUISHES) {
         kill_fall();
         return true;
       }
+    }
 
-      set_state(ICESTATE_FLAT);
-      break;
+    set_state(ICESTATE_FLAT);
+    nokick_timer.start(NOKICK_TIME);
+    break;
     case ICESTATE_FLAT:
-      if(player.get_pos().x < get_pos().x) {
+    {
+      MovingObject* movingobject = dynamic_cast<MovingObject*>(&object);
+      if (movingobject && (movingobject->get_pos().x < get_pos().x)) {
         dir = RIGHT;
       } else {
         dir = LEFT;
       }
-      set_state(ICESTATE_KICKED);
-      break;
+    }
+    if (nokick_timer.check()) set_state(ICESTATE_KICKED);
+    break;
     case ICESTATE_GRABBED:
       assert(false);
       break;
   }
 
-  player.bounce(*this);
+  if (player) player->bounce(*this);
   return true;
 }
 
 void
-MrIceBlock::set_state(IceState state)
+MrIceBlock::set_state(IceState state, bool up)
 {
   if(ice_state == state)
     return;
 
   switch(state) {
     case ICESTATE_NORMAL:
-      WalkingBadguy::activate();
+      WalkingBadguy::initialize();
       break;
     case ICESTATE_FLAT:
-      sound_manager->play("sounds/stomp.wav", get_pos());
-      physic.set_velocity_x(0);
-      physic.set_velocity_y(0);
-
+      if(up) {
+        physic.set_velocity_y(-KICKSPEED);
+      } else {
+        sound_manager->play("sounds/stomp.wav", get_pos());
+        physic.set_velocity_x(0);
+        physic.set_velocity_y(0);
+      }
       sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
       flat_timer.start(4);
       break;
@@ -228,7 +250,7 @@ MrIceBlock::set_state(IceState state)
       physic.set_velocity_x(dir == LEFT ? -KICKSPEED : KICKSPEED);
       sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
       // we should slide above 1 block holes now...
-      bbox.set_size(34, 31.8);
+      bbox.set_size(34, 31.8f);
       break;
     case ICESTATE_GRABBED:
       flat_timer.stop();
@@ -246,15 +268,19 @@ MrIceBlock::grab(MovingObject&, const Vector& pos, Direction dir)
   this->dir = dir;
   sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
   set_state(ICESTATE_GRABBED);
-  set_group(COLGROUP_DISABLED);
+  set_colgroup_active(COLGROUP_DISABLED);
 }
 
 void
 MrIceBlock::ungrab(MovingObject& , Direction dir)
 {
-  this->dir = dir;
-  set_state(ICESTATE_KICKED);
-  set_group(COLGROUP_MOVING);
+  if(dir == UP) {
+    set_state(ICESTATE_FLAT, true);
+  } else {
+    this->dir = dir;
+    set_state(ICESTATE_KICKED);
+  }
+  set_colgroup_active(COLGROUP_MOVING);
 }
 
 bool
@@ -263,4 +289,4 @@ MrIceBlock::is_portable() const
   return ice_state == ICESTATE_FLAT;
 }
 
-IMPLEMENT_FACTORY(MrIceBlock, "mriceblock")
+/* EOF */