From: Ondřej Hošek Date: Wed, 11 Jan 2006 19:16:09 +0000 (+0000) Subject: Okay, I guess this is it. The "stay on platform" flag is back. Featuring floorf and... X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=209464e1ecefabfaa671c063195bad8b0bdf6d8f;p=supertux.git Okay, I guess this is it. The "stay on platform" flag is back. Featuring floorf and ceilf. SVN-Revision: 2982 --- diff --git a/src/badguy/badguy.cpp b/src/badguy/badguy.cpp index 36fe717db..7da606bea 100644 --- a/src/badguy/badguy.cpp +++ b/src/badguy/badguy.cpp @@ -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; +} diff --git a/src/badguy/badguy.hpp b/src/badguy/badguy.hpp index 775478ae6..6980ba9a1 100644 --- a/src/badguy/badguy.hpp +++ b/src/badguy/badguy.hpp @@ -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; diff --git a/src/badguy/mriceblock.cpp b/src/badguy/mriceblock.cpp index 04db0eef3..863fd9ed9 100644 --- a/src/badguy/mriceblock.cpp +++ b/src/badguy/mriceblock.cpp @@ -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); } diff --git a/src/badguy/mriceblock.hpp b/src/badguy/mriceblock.hpp index bd0ff53dc..a884aa8de 100644 --- a/src/badguy/mriceblock.hpp +++ b/src/badguy/mriceblock.hpp @@ -59,6 +59,7 @@ private: Timer flat_timer; int squishcount; bool set_direction; + bool stay_on_platform; Direction initial_direction; };