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 "audio/sound_manager.hpp"
20 #include "math/random_generator.hpp"
21 #include "object/bullet.hpp"
22 #include "object/player.hpp"
23 #include "sprite/sprite.hpp"
24 #include "supertux/object_factory.hpp"
26 static const int SHAKE_RANGE_X = 40;
27 static const float SHAKE_TIME = .8f;
28 static const float SHAKE_RANGE_Y = 400;
30 Stalactite::Stalactite(const Reader& lisp) :
31 BadGuy(lisp, "images/creatures/stalactite/stalactite.sprite", LAYER_TILES - 1),
33 state(STALACTITE_HANGING),
37 set_colgroup_active(COLGROUP_TOUCHABLE);
38 sound_manager->preload("sounds/cracking.wav");
39 sound_manager->preload("sounds/sizzle.ogg");
40 sound_manager->preload("sounds/icecrash.ogg");
44 Stalactite::active_update(float elapsed_time)
46 if(state == STALACTITE_HANGING) {
47 Player* player = this->get_nearest_player();
49 if(player->get_bbox().p2.x > bbox.p1.x - SHAKE_RANGE_X
50 && player->get_bbox().p1.x < bbox.p2.x + SHAKE_RANGE_X
51 && player->get_bbox().p2.y > bbox.p1.y
52 && player->get_bbox().p1.y < bbox.p2.y + SHAKE_RANGE_Y) {
53 timer.start(SHAKE_TIME);
54 state = STALACTITE_SHAKING;
55 sound_manager->play("sounds/cracking.wav", get_pos());
58 } else if(state == STALACTITE_SHAKING) {
59 shake_delta = Vector(graphicsRandom.rand(-3,3), 0);
61 state = STALACTITE_FALLING;
62 physic.enable_gravity(true);
63 set_colgroup_active(COLGROUP_MOVING);
65 } else if(state == STALACTITE_FALLING) {
66 movement = physic.get_movement(elapsed_time);
73 state = STALACTITE_SQUISHED;
74 physic.enable_gravity(true);
75 physic.set_velocity_x(0);
76 physic.set_velocity_y(0);
77 set_state(STATE_SQUISHED);
78 sprite->set_action("squished");
79 sound_manager->play("sounds/icecrash.ogg", get_pos());
80 set_group(COLGROUP_MOVING_ONLY_STATIC);
85 Stalactite::collision_solid(const CollisionHit& hit)
87 if(state == STALACTITE_FALLING) {
88 if (hit.bottom) squish();
90 if(state == STALACTITE_SQUISHED) {
91 physic.set_velocity_y(0);
96 Stalactite::collision_player(Player& player, const CollisionHit& )
98 if(state != STALACTITE_SQUISHED) {
106 Stalactite::collision_badguy(BadGuy& other, const CollisionHit& hit)
108 if (state == STALACTITE_SQUISHED) return FORCE_MOVE;
110 // ignore other Stalactites
111 if (dynamic_cast<Stalactite*>(&other)) return FORCE_MOVE;
113 if (state != STALACTITE_FALLING) return BadGuy::collision_badguy(other, hit);
115 if (other.is_freezable()) {
125 Stalactite::collision_bullet(Bullet& bullet, const CollisionHit& )
127 if(state == STALACTITE_HANGING) {
128 timer.start(SHAKE_TIME);
129 state = STALACTITE_SHAKING;
131 if(bullet.get_type() == FIRE_BONUS)
132 sound_manager->play("sounds/sizzle.ogg", get_pos());
133 sound_manager->play("sounds/cracking.wav", get_pos());
140 Stalactite::kill_fall()
145 Stalactite::draw(DrawingContext& context)
147 if(get_state() == STATE_INIT || get_state() == STATE_INACTIVE)
150 if(state == STALACTITE_SQUISHED) {
151 sprite->draw(context, get_pos(), LAYER_OBJECTS);
152 } else if(state == STALACTITE_SHAKING) {
153 sprite->draw(context, get_pos() + shake_delta, layer);
155 sprite->draw(context, get_pos(), layer);
160 Stalactite::deactivate()
162 if(state != STALACTITE_HANGING)