added unstable_tile object
authorMatthias Braun <matze@braunis.de>
Wed, 30 Mar 2005 03:02:01 +0000 (03:02 +0000)
committerMatthias Braun <matze@braunis.de>
Wed, 30 Mar 2005 03:02:01 +0000 (03:02 +0000)
SVN-Revision: 2296

data/images/shared/unstable_tile.png [new file with mode: 0644]
data/images/supertux.strf
src/object/unstable_tile.cpp [new file with mode: 0644]
src/object/unstable_tile.h [new file with mode: 0644]
src/timer.h

diff --git a/data/images/shared/unstable_tile.png b/data/images/shared/unstable_tile.png
new file mode 100644 (file)
index 0000000..f984771
Binary files /dev/null and b/data/images/shared/unstable_tile.png differ
index 608420d..e14e26a 100644 (file)
          )
        )
   )
+  
+  (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 (file)
index 0000000..eabbd2d
--- /dev/null
@@ -0,0 +1,79 @@
+#include <config.h>
+
+#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<Player*> (&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 (file)
index 0000000..3aca835
--- /dev/null
@@ -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
+
index 4640e57..9e93c90 100644 (file)
@@ -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