changed worldmap format a bit to be more consistent with level format
[supertux.git] / src / object / unstable_tile.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2005 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
19 //  02111-1307, USA.
20
21 #include <config.h>
22
23 #include "unstable_tile.h"
24 #include "lisp/lisp.h"
25 #include "object_factory.h"
26 #include "player.h"
27 #include "sector.h"
28 #include "resources.h"
29 #include "sprite/sprite_manager.h"
30 #include "sprite/sprite.h"
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("unstable_tile");
42   flags |= FLAG_SOLID;
43 }
44
45 UnstableTile::~UnstableTile()
46 {
47   delete sprite;
48 }
49
50 HitResponse
51 UnstableTile::collision(GameObject& other, const CollisionHit& hitdata)
52 {
53   if(hitdata.normal.y < 0.8)
54     return FORCE_MOVE;
55
56   Player* player = dynamic_cast<Player*> (&other);
57   if(player)
58     hit = true;
59
60   return FORCE_MOVE;
61 }
62
63 void
64 UnstableTile::draw(DrawingContext& context)
65 {
66   Vector pos = get_pos();
67   // shacking
68   if(timer.get_timegone() > CRACKTIME) {
69     pos.x += (rand() % 6) - 3;
70   } 
71
72   sprite->draw(context, pos, LAYER_TILES);
73 }
74
75 void
76 UnstableTile::update(float elapsed_time)
77 {
78   if(falling) {
79     movement = physic.get_movement(elapsed_time);
80     if(!Sector::current()->inside(bbox)) {
81       remove_me();
82       return;
83     }
84   } else if(hit) {
85     if(timer.check()) {
86       falling = true;
87       physic.enable_gravity(true);      
88       flags &= ~FLAG_SOLID;
89       timer.stop();
90     } else if(!timer.started()) {
91       timer.start(FALLTIME);
92     }
93   } else {
94     timer.stop();
95   }
96   hit = false;
97 }
98
99 IMPLEMENT_FACTORY(UnstableTile, "unstable_tile");