Committing RandomGenerator patch from Allen King, with a few small changes
[supertux.git] / src / object / unstable_tile.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 #include <config.h>
21
22 #include "unstable_tile.hpp"
23 #include "lisp/lisp.hpp"
24 #include "object_factory.hpp"
25 #include "player.hpp"
26 #include "sector.hpp"
27 #include "resources.hpp"
28 #include "sprite/sprite_manager.hpp"
29 #include "sprite/sprite.hpp"
30 #include "random_generator.hpp"
31
32 static const float CRACKTIME = 0.3;
33 static const float FALLTIME = 0.8;
34
35 UnstableTile::UnstableTile(const lisp::Lisp& lisp)
36   : hit(false), falling(false)
37 {
38   lisp.get("x", bbox.p1.x);
39   lisp.get("y", bbox.p1.y);
40   bbox.set_size(32, 32);
41   sprite = sprite_manager->create("images/objects/unstable_tile/unstable_tile.sprite");
42   flags |= FLAG_SOLID;
43   set_group(COLGROUP_STATIC);
44 }
45
46 UnstableTile::~UnstableTile()
47 {
48   delete sprite;
49 }
50
51 HitResponse
52 UnstableTile::collision(GameObject& other, const CollisionHit& hitdata)
53 {
54   if(hitdata.normal.y < 0.8)
55     return FORCE_MOVE;
56
57   Player* player = dynamic_cast<Player*> (&other);
58   if(player)
59     hit = true;
60
61   return FORCE_MOVE;
62 }
63
64 void
65 UnstableTile::draw(DrawingContext& context)
66 {
67   Vector pos = get_pos();
68   // shacking
69   if(timer.get_timegone() > CRACKTIME) {
70     pos.x += systemRandom.rand(-3, 3);
71   } 
72
73   sprite->draw(context, pos, LAYER_TILES);
74 }
75
76 void
77 UnstableTile::update(float elapsed_time)
78 {
79   if(falling) {
80     movement = physic.get_movement(elapsed_time);
81     if(!Sector::current()->inside(bbox)) {
82       remove_me();
83       return;
84     }
85   } else if(hit) {
86     if(timer.check()) {
87       falling = true;
88       physic.enable_gravity(true);      
89       flags &= ~FLAG_SOLID;
90       timer.stop();
91     } else if(!timer.started()) {
92       timer.start(FALLTIME);
93     }
94   } else {
95     timer.stop();
96   }
97   hit = false;
98 }
99
100 IMPLEMENT_FACTORY(UnstableTile, "unstable_tile");