2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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.
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.
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/>.
17 #include "badguy/stalactite.hpp"
19 #include "math/random_generator.hpp"
20 #include "object/bullet.hpp"
21 #include "object/player.hpp"
22 #include "sprite/sprite.hpp"
23 #include "supertux/object_factory.hpp"
25 static const int SHAKE_RANGE_X = 40;
26 static const float SHAKE_TIME = .8f;
27 static const float SHAKE_RANGE_Y = 400;
29 Stalactite::Stalactite(const Reader& lisp) :
30 BadGuy(lisp, "images/creatures/stalactite/stalactite.sprite", LAYER_TILES - 1),
32 state(STALACTITE_HANGING),
36 set_colgroup_active(COLGROUP_TOUCHABLE);
40 Stalactite::active_update(float elapsed_time)
42 if(state == STALACTITE_HANGING) {
43 Player* player = this->get_nearest_player();
45 if(player->get_bbox().p2.x > bbox.p1.x - SHAKE_RANGE_X
46 && player->get_bbox().p1.x < bbox.p2.x + SHAKE_RANGE_X
47 && player->get_bbox().p2.y > bbox.p1.y
48 && player->get_bbox().p1.y < bbox.p2.y + SHAKE_RANGE_Y) {
49 timer.start(SHAKE_TIME);
50 state = STALACTITE_SHAKING;
53 } else if(state == STALACTITE_SHAKING) {
54 shake_delta = Vector(graphicsRandom.rand(-3,3), 0);
56 state = STALACTITE_FALLING;
57 physic.enable_gravity(true);
58 set_colgroup_active(COLGROUP_MOVING);
60 } else if(state == STALACTITE_FALLING) {
61 movement = physic.get_movement(elapsed_time);
68 state = STALACTITE_SQUISHED;
69 physic.enable_gravity(true);
70 physic.set_velocity_x(0);
71 physic.set_velocity_y(0);
72 set_state(STATE_SQUISHED);
73 sprite->set_action("squished");
74 set_group(COLGROUP_MOVING_ONLY_STATIC);
79 Stalactite::collision_solid(const CollisionHit& hit)
81 if(state == STALACTITE_FALLING) {
82 if (hit.bottom) squish();
84 if(state == STALACTITE_SQUISHED) {
85 physic.set_velocity_y(0);
90 Stalactite::collision_player(Player& player, const CollisionHit& )
92 if(state != STALACTITE_SQUISHED) {
100 Stalactite::collision_badguy(BadGuy& other, const CollisionHit& hit)
102 if (state == STALACTITE_SQUISHED) return FORCE_MOVE;
104 // ignore other Stalactites
105 if (dynamic_cast<Stalactite*>(&other)) return FORCE_MOVE;
107 if (state != STALACTITE_FALLING) return BadGuy::collision_badguy(other, hit);
109 if (other.is_freezable()) {
119 Stalactite::collision_bullet(Bullet& bullet, const CollisionHit& )
121 if(state == STALACTITE_HANGING) {
122 timer.start(SHAKE_TIME);
123 state = STALACTITE_SHAKING;
131 Stalactite::kill_fall()
136 Stalactite::draw(DrawingContext& context)
138 if(get_state() == STATE_INIT || get_state() == STATE_INACTIVE)
141 if(state == STALACTITE_SQUISHED) {
142 sprite->draw(context, get_pos(), LAYER_OBJECTS);
143 } else if(state == STALACTITE_SHAKING) {
144 sprite->draw(context, get_pos() + shake_delta, layer);
146 sprite->draw(context, get_pos(), layer);
151 Stalactite::deactivate()
153 if(state != STALACTITE_HANGING)