1d0895846aa7bbda17dd9855f3a6942f877b7ca7
[supertux.git] / src / object / unstable_tile.cpp
1 //  $Id$
2 //
3 //  SuperTux - Unstable Tile
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
6 //
7 //  This program is free software; you can redistribute it and/or
8 //  modify it under the terms of the GNU General Public License
9 //  as published by the Free Software Foundation; either version 2
10 //  of the License, or (at your option) any later version.
11 //
12 //  This program is distributed in the hope that it will be useful,
13 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 //  GNU General Public License for more details.
16 //
17 //  You should have received a copy of the GNU General Public License
18 //  along with this program; if not, write to the Free Software
19 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20
21 #include <config.h>
22
23 #include "unstable_tile.hpp"
24 #include "lisp/lisp.hpp"
25 #include "object_factory.hpp"
26 #include "player.hpp"
27 #include "sector.hpp"
28 #include "resources.hpp"
29 #include "sprite/sprite.hpp"
30 #include "random_generator.hpp"
31 #include "object/bullet.hpp"
32
33 UnstableTile::UnstableTile(const lisp::Lisp& lisp)
34   : MovingSprite(lisp, LAYER_TILES, COLGROUP_STATIC), state(STATE_NORMAL)
35 {
36   sprite->set_action("normal");
37   flags |= FLAG_SOLID;
38 }
39
40 HitResponse
41 UnstableTile::collision(GameObject& other, const CollisionHit& hit)
42 {
43   switch (state) {
44
45     case STATE_NORMAL:
46       if ((hit.normal.y >= 0.8 ) && (dynamic_cast<Player*>(&other))) {
47         state = STATE_CRUMBLING;
48         sprite->set_action("crumbling", 1);
49         return FORCE_MOVE;
50       }
51       return FORCE_MOVE;
52       break;
53
54     case STATE_CRUMBLING:
55       return FORCE_MOVE;
56       break;
57
58     case STATE_DISINTEGRATING:
59       return FORCE_MOVE;
60       break;
61
62   }
63
64   log_debug << "unhandled state" << std::endl;
65   return FORCE_MOVE;
66 }
67
68 void
69 UnstableTile::update(float elapsed_time)
70 {
71   switch (state) {
72
73     case STATE_NORMAL:
74       break;
75
76     case STATE_CRUMBLING:
77       if (sprite->animation_done()) {
78         state = STATE_DISINTEGRATING;
79         sprite->set_action("disintegrating", 1);
80         flags &= ~FLAG_SOLID;
81         set_group(COLGROUP_DISABLED);
82         physic.enable_gravity(true);
83       }
84       break;
85
86     case STATE_DISINTEGRATING:
87       movement = physic.get_movement(elapsed_time);
88       if (sprite->animation_done()) {
89         remove_me();
90         return;
91       }
92       break;
93
94   }
95 }
96
97 IMPLEMENT_FACTORY(UnstableTile, "unstable_tile");