6742c7346c0b67e3d82b6247a7c0f202d346f357
[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 {
33   countMe = false;
34   set_colgroup_active(COLGROUP_TOUCHABLE);
35 }
36
37 void
38 Stalactite::active_update(float elapsed_time)
39 {
40   if(state == STALACTITE_HANGING) {
41     Player* player = this->get_nearest_player();
42     if (player) {
43       if(player->get_bbox().p2.x > bbox.p1.x - SHAKE_RANGE_X
44          && player->get_bbox().p1.x < bbox.p2.x + SHAKE_RANGE_X
45          && player->get_bbox().p2.y > bbox.p1.y
46          && player->get_bbox().p1.y < bbox.p2.y + SHAKE_RANGE_Y) {
47         timer.start(SHAKE_TIME);
48         state = STALACTITE_SHAKING;
49       }
50     }
51   } else if(state == STALACTITE_SHAKING) {
52     if(timer.check()) {
53       state = STALACTITE_FALLING;
54       physic.enable_gravity(true);
55       set_colgroup_active(COLGROUP_MOVING);
56     }
57   } else if(state == STALACTITE_FALLING) {
58     movement = physic.get_movement(elapsed_time);
59   }
60 }
61
62 void
63 Stalactite::squish()
64 {
65   state = STALACTITE_SQUISHED;
66   physic.enable_gravity(true);
67   physic.set_velocity_x(0);
68   physic.set_velocity_y(0);
69   set_state(STATE_SQUISHED);
70   set_group(COLGROUP_MOVING_ONLY_STATIC);
71   run_dead_script();
72 }
73
74 void
75 Stalactite::collision_solid(const CollisionHit& hit)
76 {
77   if(state == STALACTITE_FALLING) {
78     if (hit.bottom) squish();
79   }
80   if(state == STALACTITE_SQUISHED) {
81     physic.set_velocity_y(0);
82   }
83 }
84
85 HitResponse
86 Stalactite::collision_player(Player& player, const CollisionHit& )
87 {
88   if(state != STALACTITE_SQUISHED) {
89     player.kill(false);
90   }
91
92   return FORCE_MOVE;
93 }
94
95 HitResponse
96 Stalactite::collision_badguy(BadGuy& other, const CollisionHit& hit)
97 {
98   if (state == STALACTITE_SQUISHED) return FORCE_MOVE;
99
100   // ignore other Stalactites
101   if (dynamic_cast<Stalactite*>(&other)) return FORCE_MOVE;
102
103   if (state != STALACTITE_FALLING) return BadGuy::collision_badguy(other, hit);
104
105   if (other.is_freezable()) {
106     other.freeze();
107   } else {
108     other.kill_fall();
109   }
110
111   remove_me();
112
113   return FORCE_MOVE;
114 }
115
116 void
117 Stalactite::kill_fall()
118 {
119 }
120
121 void
122 Stalactite::draw(DrawingContext& context)
123 {
124   if(get_state() != STATE_ACTIVE)
125     return;
126
127   if(state == STALACTITE_SQUISHED) {
128     sprite->draw(context, get_pos(), LAYER_OBJECTS);
129     return;
130   }
131
132   if(state == STALACTITE_SHAKING) {
133     sprite->draw(context, get_pos() + Vector(systemRandom.rand(-3,3), 0), layer);
134   } else {
135     sprite->draw(context, get_pos(), layer);
136   }
137 }
138
139 void
140 Stalactite::deactivate()
141 {
142   if(state != STALACTITE_HANGING)
143     remove_me();
144 }
145
146 /* EOF */