Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / badguy / stalactite.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "badguy/stalactite.hpp"
18
19 #include "math/random_generator.hpp"
20 #include "object/player.hpp"
21 #include "sprite/sprite.hpp"
22 #include "supertux/object_factory.hpp"
23
24 static const int SHAKE_RANGE_X = 40;
25 static const float SHAKE_TIME = .8f;
26 static const float SQUISH_TIME = 2;
27 static const float SHAKE_RANGE_Y = 400;
28
29 Stalactite::Stalactite(const Reader& lisp) :
30   BadGuy(lisp, "images/creatures/stalactite/stalactite.sprite", LAYER_TILES - 1),
31   timer(),
32   state(STALACTITE_HANGING)
33 {
34   countMe = false;
35   set_colgroup_active(COLGROUP_TOUCHABLE);
36 }
37
38 void
39 Stalactite::active_update(float elapsed_time)
40 {
41   if(state == STALACTITE_HANGING) {
42     Player* player = this->get_nearest_player();
43     if (player) {
44       if(player->get_bbox().p2.x > bbox.p1.x - SHAKE_RANGE_X
45          && player->get_bbox().p1.x < bbox.p2.x + SHAKE_RANGE_X
46          && player->get_bbox().p2.y > bbox.p1.y
47          && player->get_bbox().p1.y < bbox.p2.y + SHAKE_RANGE_Y) {
48         timer.start(SHAKE_TIME);
49         state = STALACTITE_SHAKING;
50       }
51     }
52   } else if(state == STALACTITE_SHAKING) {
53     if(timer.check()) {
54       state = STALACTITE_FALLING;
55       physic.enable_gravity(true);
56       set_colgroup_active(COLGROUP_MOVING);
57     }
58   } else if(state == STALACTITE_FALLING || state == STALACTITE_SQUISHED) {
59     movement = physic.get_movement(elapsed_time);
60     if(state == STALACTITE_SQUISHED && timer.check())
61       remove_me();
62   }
63 }
64
65 void
66 Stalactite::squish()
67 {
68   state = STALACTITE_SQUISHED;
69   set_colgroup_active(COLGROUP_MOVING_ONLY_STATIC);
70   sprite->set_action("squished");
71   if(!timer.started())
72     timer.start(SQUISH_TIME);
73 }
74
75 void
76 Stalactite::collision_solid(const CollisionHit& hit)
77 {
78   if(state == STALACTITE_FALLING) {
79     if (hit.bottom) squish();
80   }
81   if(state == STALACTITE_SQUISHED) {
82     physic.set_velocity_y(0);
83   }
84 }
85
86 HitResponse
87 Stalactite::collision_player(Player& player, const CollisionHit& )
88 {
89   if(state != STALACTITE_SQUISHED) {
90     player.kill(false);
91   }
92
93   return FORCE_MOVE;
94 }
95
96 HitResponse
97 Stalactite::collision_badguy(BadGuy& other, const CollisionHit& hit)
98 {
99   if (state == STALACTITE_SQUISHED) return FORCE_MOVE;
100
101   // ignore other Stalactites
102   if (dynamic_cast<Stalactite*>(&other)) return FORCE_MOVE;
103
104   if (state != STALACTITE_FALLING) return BadGuy::collision_badguy(other, hit);
105
106   if (other.is_freezable()) {
107     other.freeze();
108   } else {
109     other.kill_fall();
110   }
111
112   remove_me();
113
114   return FORCE_MOVE;
115 }
116
117 void
118 Stalactite::kill_fall()
119 {
120 }
121
122 void
123 Stalactite::draw(DrawingContext& context)
124 {
125   if(get_state() != STATE_ACTIVE)
126     return;
127
128   if(state == STALACTITE_SQUISHED) {
129     sprite->draw(context, get_pos(), LAYER_OBJECTS);
130     return;
131   }
132
133   if(state == STALACTITE_SHAKING) {
134     sprite->draw(context, get_pos() + Vector(systemRandom.rand(-3,3), 0), layer);
135   } else {
136     sprite->draw(context, get_pos(), layer);
137   }
138 }
139
140 void
141 Stalactite::deactivate()
142 {
143   if(state != STALACTITE_HANGING)
144     remove_me();
145 }
146
147 IMPLEMENT_FACTORY(Stalactite, "stalactite");
148
149 /* EOF */