Okay, I guess this is it. The "stay on platform" flag is back. Featuring floorf and...
authorOndřej Hošek <ondra.hosek@gmail.com>
Wed, 11 Jan 2006 19:16:09 +0000 (19:16 +0000)
committerOndřej Hošek <ondra.hosek@gmail.com>
Wed, 11 Jan 2006 19:16:09 +0000 (19:16 +0000)
SVN-Revision: 2982

src/badguy/badguy.cpp
src/badguy/badguy.hpp
src/badguy/mriceblock.cpp
src/badguy/mriceblock.hpp

index 36fe717..7da606b 100644 (file)
@@ -305,3 +305,34 @@ BadGuy::try_activate()
     activate();
   } 
 }
+
+bool
+BadGuy::may_fall_off_platform()
+{
+  int tile_x, tile_y;
+  // First, let's say the badguy moves once its width in the
+  // direction it's heading.
+  Vector pos = get_pos();
+  pos.x += (dir == LEFT ? -bbox.get_width() : bbox.get_width());
+
+  // Now, snap the badguy's X coordinate to the 32x32/cell grid.
+  if (dir == LEFT) // use the ceiling
+    tile_x = (int)ceilf(pos.x/32.0f);
+  else // use the floor
+    tile_x = (int)floorf(pos.x/32.0f);
+
+  // We might be falling down, so use the ceiling to round upward and
+  // get the lower position. (Positive Y goes downward.)
+  tile_y = (int)ceilf(pos.y/32.0f);
+
+  // Now, if the badguy intersects with a tile, he won't fall off.
+  // If he doesn't intersect, he probably will.
+  if (Sector::current()->solids->get_tile(tile_x, tile_y)->getAttributes() & FLAG_SOLID)
+  {
+    // It's a solid tile. Good.
+    return false;
+  }
+
+  // Watch out there buddy, you might take a sticky end!
+  return true;
+}
index 775478a..6980ba9 100644 (file)
@@ -147,7 +147,11 @@ protected:
    * after being deactivated.
    */
   bool is_offscreen();
-  
+  /**
+   * Checks if the badguy may fall off a platform if continuing a given movement.
+   */
+  bool may_fall_off_platform();
+
   Vector start_position;
 
   Direction dir;
index 04db0ee..863fd9e 100644 (file)
@@ -32,6 +32,7 @@ MrIceBlock::MrIceBlock(const lisp::Lisp& reader)
 {
   reader.get("x", start_position.x);
   reader.get("y", start_position.y);
+  reader.get("stay-on-platform", stay_on_platform);
   bbox.set_size(31.8, 31.8);
   sprite = sprite_manager->create("mriceblock");
   set_direction = false;
@@ -79,7 +80,16 @@ MrIceBlock::active_update(float elapsed_time)
   if(ice_state == ICESTATE_FLAT && flat_timer.check()) {
     set_state(ICESTATE_NORMAL);
   }
-  
+
+  if (ice_state == ICESTATE_NORMAL &&
+      stay_on_platform &&
+      may_fall_off_platform())
+  {
+    dir = (dir == LEFT ? RIGHT : LEFT);
+    sprite->set_action(dir == LEFT ? "left" : "right");
+    physic.set_velocity_x(-physic.get_velocity_x());
+  }
+
   BadGuy::active_update(elapsed_time);
 }
 
index bd0ff53..a884aa8 100644 (file)
@@ -59,6 +59,7 @@ private:
   Timer flat_timer;
   int squishcount;
   bool set_direction;
+  bool stay_on_platform;
   Direction initial_direction;
 };