From: Matthias Braun Date: Wed, 30 Mar 2005 03:02:01 +0000 (+0000) Subject: added unstable_tile object X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=20f975e6fefc179e110cff27d424ec231b8d3801;p=supertux.git added unstable_tile object SVN-Revision: 2296 --- diff --git a/data/images/shared/unstable_tile.png b/data/images/shared/unstable_tile.png new file mode 100644 index 000000000..f98477131 Binary files /dev/null and b/data/images/shared/unstable_tile.png differ diff --git a/data/images/supertux.strf b/data/images/supertux.strf index 608420d1d..e14e26a93 100644 --- a/data/images/supertux.strf +++ b/data/images/supertux.strf @@ -1396,6 +1396,11 @@ ) ) ) + + (sprite (name "unstable_tile") + (action + (images "shared/unstable_tile.png")) + ) ) ;; EOF ;; diff --git a/src/object/unstable_tile.cpp b/src/object/unstable_tile.cpp new file mode 100644 index 000000000..eabbd2dbb --- /dev/null +++ b/src/object/unstable_tile.cpp @@ -0,0 +1,79 @@ +#include + +#include "unstable_tile.h" +#include "lisp/lisp.h" +#include "object_factory.h" +#include "player.h" +#include "sector.h" +#include "resources.h" +#include "special/sprite_manager.h" +#include "special/sprite.h" + +static const float CRACKTIME = 1; +static const float FALLTIME = 1.5; + +UnstableTile::UnstableTile(const lisp::Lisp& lisp) + : hit(false), falling(false) +{ + lisp.get("x", bbox.p1.x); + lisp.get("y", bbox.p1.y); + bbox.set_size(32, 32); + sprite = sprite_manager->create("unstable_tile"); + flags |= FLAG_SOLID; +} + +UnstableTile::~UnstableTile() +{ + delete sprite; +} + +HitResponse +UnstableTile::collision(GameObject& other, const CollisionHit& hitdata) +{ + if(hitdata.normal.y < 0.8) + return FORCE_MOVE; + + Player* player = dynamic_cast (&other); + if(player) + hit = true; + + return FORCE_MOVE; +} + +void +UnstableTile::draw(DrawingContext& context) +{ + Vector pos = get_pos(); + // shacking + if(timer.get_timegone() > CRACKTIME) { + pos.x += (rand() % 6) - 3; + } + + sprite->draw(context, pos, LAYER_TILES); +} + +void +UnstableTile::action(float elapsed_time) +{ + if(falling) { + movement = physic.get_movement(elapsed_time); + if(!Sector::current()->inside(bbox)) { + remove_me(); + return; + } + } else if(hit) { + if(timer.check()) { + falling = true; + physic.enable_gravity(true); + flags &= ~FLAG_SOLID; + timer.stop(); + } else if(!timer.started()) { + timer.start(FALLTIME); + } + } else { + timer.stop(); + } + hit = false; +} + +IMPLEMENT_FACTORY(UnstableTile, "unstable_tile"); diff --git a/src/object/unstable_tile.h b/src/object/unstable_tile.h new file mode 100644 index 000000000..3aca83571 --- /dev/null +++ b/src/object/unstable_tile.h @@ -0,0 +1,36 @@ +#ifndef __UNSTABLE_TILE_H__ +#define __UNSTABLE_TILE_H__ + +#include "special/moving_object.h" +#include "lisp/lisp.h" +#include "math/physic.h" +#include "timer.h" + +namespace SuperTux { + class Sprite; +} +class Player; + +using namespace SuperTux; + +/** A tile that starts falling down if tux stands to long on it */ +class UnstableTile : public MovingObject +{ +public: + UnstableTile(const lisp::Lisp& lisp); + ~UnstableTile(); + + HitResponse collision(GameObject& other, const CollisionHit& hit); + void action(float elapsed_time); + void draw(DrawingContext& context); + +private: + Physic physic; + Sprite* sprite; + Timer2 timer; + bool hit; + bool falling; +}; + +#endif + diff --git a/src/timer.h b/src/timer.h index 4640e575b..9e93c9032 100644 --- a/src/timer.h +++ b/src/timer.h @@ -21,6 +21,9 @@ public: * successfull check */ bool check(); + /** stop the timer */ + void stop() + { start(0); } /** returns the period of the timer or 0 if it isn't started */ float get_period() const