20eea8c598f7aca018566a32c2e6d4a4df42f4bd
[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 SHAKE_RANGE_Y = 400;
27
28 Stalactite::Stalactite(const Reader& lisp) :
29   BadGuy(lisp, "images/creatures/stalactite/stalactite.sprite", LAYER_TILES - 1),
30   timer(),
31   state(STALACTITE_HANGING),
32   shake_delta()
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     shake_delta = Vector(systemRandom.rand(-3,3), 0);
54     if(timer.check()) {
55       state = STALACTITE_FALLING;
56       physic.enable_gravity(true);
57       set_colgroup_active(COLGROUP_MOVING);
58     }
59   } else if(state == STALACTITE_FALLING) {
60     movement = physic.get_movement(elapsed_time);
61   }
62 }
63
64 void
65 Stalactite::squish()
66 {
67   state = STALACTITE_SQUISHED;
68   physic.enable_gravity(true);
69   physic.set_velocity_x(0);
70   physic.set_velocity_y(0);
71   set_state(STATE_SQUISHED);
72   set_group(COLGROUP_MOVING_ONLY_STATIC);
73   run_dead_script();
74 }
75
76 void
77 Stalactite::collision_solid(const CollisionHit& hit)
78 {
79   if(state == STALACTITE_FALLING) {
80     if (hit.bottom) squish();
81   }
82   if(state == STALACTITE_SQUISHED) {
83     physic.set_velocity_y(0);
84   }
85 }
86
87 HitResponse
88 Stalactite::collision_player(Player& player, const CollisionHit& )
89 {
90   if(state != STALACTITE_SQUISHED) {
91     player.kill(false);
92   }
93
94   return FORCE_MOVE;
95 }
96
97 HitResponse
98 Stalactite::collision_badguy(BadGuy& other, const CollisionHit& hit)
99 {
100   if (state == STALACTITE_SQUISHED) return FORCE_MOVE;
101
102   // ignore other Stalactites
103   if (dynamic_cast<Stalactite*>(&other)) return FORCE_MOVE;
104
105   if (state != STALACTITE_FALLING) return BadGuy::collision_badguy(other, hit);
106
107   if (other.is_freezable()) {
108     other.freeze();
109   } else {
110     other.kill_fall();
111   }
112
113   remove_me();
114
115   return FORCE_MOVE;
116 }
117
118 void
119 Stalactite::kill_fall()
120 {
121 }
122
123 void
124 Stalactite::draw(DrawingContext& context)
125 {
126   if(get_state() != STATE_ACTIVE)
127     return;
128
129   if(state == STALACTITE_SQUISHED) {
130     sprite->draw(context, get_pos(), LAYER_OBJECTS);
131     return;
132   }
133
134   if(state == STALACTITE_SHAKING) {
135     sprite->draw(context, get_pos() + shake_delta, layer);
136   } else {
137     sprite->draw(context, get_pos(), layer);
138   }
139 }
140
141 void
142 Stalactite::deactivate()
143 {
144   if(state != STALACTITE_HANGING)
145     remove_me();
146 }
147
148 /* EOF */