Add current level to another debug message
[supertux.git] / src / badguy / mriceblock.cpp
index 4d3bca7..f87539b 100644 (file)
 #include "audio/sound_manager.hpp"
 #include "object/player.hpp"
 #include "sprite/sprite.hpp"
+#include "sprite/sprite_manager.hpp"
 #include "supertux/object_factory.hpp"
 
+#include <math.h>
+
 namespace {
 const float KICKSPEED = 500;
 const int MAXSQUISHES = 10;
@@ -28,31 +31,31 @@ const float NOKICK_TIME = 0.1f;
 }
 
 MrIceBlock::MrIceBlock(const Reader& reader) :
-  WalkingBadguy(reader, "images/creatures/mr_iceblock/mr_iceblock.sprite", "left", "right"), 
-  ice_state(ICESTATE_NORMAL), 
+  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;
-  sound_manager->preload("sounds/iceblock_bump.wav");
-  sound_manager->preload("sounds/stomp.wav");
-  sound_manager->preload("sounds/kick.wav");
+  SoundManager::current()->preload("sounds/iceblock_bump.wav");
+  SoundManager::current()->preload("sounds/stomp.wav");
+  SoundManager::current()->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), 
+  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;
-  sound_manager->preload("sounds/iceblock_bump.wav");
-  sound_manager->preload("sounds/stomp.wav");
-  sound_manager->preload("sounds/kick.wav");
+  SoundManager::current()->preload("sounds/iceblock_bump.wav");
+  SoundManager::current()->preload("sounds/stomp.wav");
+  SoundManager::current()->preload("sounds/kick.wav");
 }
 
 void
@@ -93,7 +96,6 @@ MrIceBlock::collision_solid(const CollisionHit& hit)
 
   if(hit.top || hit.bottom) { // floor or roof
     physic.set_velocity_y(0);
-    return;
   }
 
   // hit left or right
@@ -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;
-        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);
+      if((hit.right && dir == RIGHT) || (hit.left && dir == LEFT)) {
+        dir = (dir == LEFT) ? RIGHT : LEFT;
+        SoundManager::current()->play("sounds/iceblock_bump.wav", get_pos());
+        physic.set_velocity_x(-physic.get_velocity_x()*.975);
       }
-      sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
+      this->set_action(dir == LEFT ? "flat-left" : "flat-right", /* loops = */ -1);
+      if(fabsf(physic.get_velocity_x()) < walk_speed*1.5)
+        set_state(ICESTATE_NORMAL);
       break;
     }
     case ICESTATE_FLAT:
@@ -134,10 +134,6 @@ MrIceBlock::collision(GameObject& object, const CollisionHit& hit)
 HitResponse
 MrIceBlock::collision_player(Player& player, const CollisionHit& hit)
 {
-  if(dir == UP) {
-    return FORCE_MOVE;
-  }
-
   // handle kicks from left or right side
   if(ice_state == ICESTATE_FLAT && get_state() == STATE_ACTIVE) {
     if(hit.left) {
@@ -177,6 +173,13 @@ MrIceBlock::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
 bool
 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:
     {
@@ -190,9 +193,8 @@ MrIceBlock::collision_squished(GameObject& object)
     // fall through
     case ICESTATE_NORMAL:
     {
-      Player* player = dynamic_cast<Player*>(&object);
       squishcount++;
-      if ((squishcount >= MAXSQUISHES) || (player && player->does_buttjump)) {
+      if (squishcount >= MAXSQUISHES) {
         kill_fall();
         return true;
       }
@@ -217,39 +219,37 @@ MrIceBlock::collision_squished(GameObject& object)
       break;
   }
 
-  Player* player = dynamic_cast<Player*>(&object);
   if (player) player->bounce(*this);
   return true;
 }
 
 void
-MrIceBlock::set_state(IceState state)
+MrIceBlock::set_state(IceState state_, bool up)
 {
-  if(ice_state == state)
+  if(ice_state == state_)
     return;
 
-  switch(state) {
+  switch(state_) {
     case ICESTATE_NORMAL:
+      this->set_action(dir == LEFT ? "left" : "right", /* loops = */ -1);
       WalkingBadguy::initialize();
       break;
     case ICESTATE_FLAT:
-      if(dir == UP) {
+      if(up) {
         physic.set_velocity_y(-KICKSPEED);
-        bbox.set_size(34, 31.8f);
       } else {
-        sound_manager->play("sounds/stomp.wav", get_pos());
+        SoundManager::current()->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");
       }
+      this->set_action(dir == LEFT ? "flat-left" : "flat-right", /* loops = */ -1);
       flat_timer.start(4);
       break;
     case ICESTATE_KICKED:
-      sound_manager->play("sounds/kick.wav", get_pos());
+      SoundManager::current()->play("sounds/kick.wav", get_pos());
 
       physic.set_velocity_x(dir == LEFT ? -KICKSPEED : KICKSPEED);
-      sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
+      this->set_action(dir == LEFT ? "flat-left" : "flat-right", /* loops = */ -1);
       // we should slide above 1 block holes now...
       bbox.set_size(34, 31.8f);
       break;
@@ -259,24 +259,28 @@ MrIceBlock::set_state(IceState state)
     default:
       assert(false);
   }
-  ice_state = state;
+  ice_state = state_;
 }
 
 void
-MrIceBlock::grab(MovingObject&, const Vector& pos, Direction dir)
+MrIceBlock::grab(MovingObject&, const Vector& pos, Direction dir_)
 {
   movement = pos - get_pos();
-  this->dir = dir;
-  sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
+  this->dir = dir_;
+  this->set_action(dir_ == LEFT ? "flat-left" : "flat-right", /* loops = */ -1);
   set_state(ICESTATE_GRABBED);
   set_colgroup_active(COLGROUP_DISABLED);
 }
 
 void
-MrIceBlock::ungrab(MovingObject& , Direction dir)
+MrIceBlock::ungrab(MovingObject& , Direction dir_)
 {
-  this->dir = dir;
-  set_state(dir == UP ? ICESTATE_FLAT : ICESTATE_KICKED);
+  if(dir_ == UP) {
+    set_state(ICESTATE_FLAT, true);
+  } else {
+    this->dir = dir_;
+    set_state(ICESTATE_KICKED);
+  }
   set_colgroup_active(COLGROUP_MOVING);
 }
 
@@ -286,6 +290,11 @@ MrIceBlock::is_portable() const
   return ice_state == ICESTATE_FLAT;
 }
 
-IMPLEMENT_FACTORY(MrIceBlock, "mriceblock");
+SmartBlock::SmartBlock(const Reader& reader) :
+  MrIceBlock(reader)
+{
+  max_drop_height = 16;
+  sprite = SpriteManager::current()->create("images/creatures/mr_iceblock/smart_block/smart_block.sprite");
+}
 
 /* EOF */