Fix coverity issues (uninitialized members)
[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 "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"
25
26 static const int SHAKE_RANGE_X = 40;
27 static const float SHAKE_TIME = .8f;
28 static const float SHAKE_RANGE_Y = 400;
29
30 Stalactite::Stalactite(const Reader& lisp) :
31   BadGuy(lisp, "images/creatures/stalactite/stalactite.sprite", LAYER_TILES - 1),
32   timer(),
33   state(STALACTITE_HANGING),
34   shake_delta()
35 {
36   countMe = false;
37   set_colgroup_active(COLGROUP_TOUCHABLE);
38   SoundManager::current()->preload("sounds/cracking.wav");
39   SoundManager::current()->preload("sounds/sizzle.ogg");
40   SoundManager::current()->preload("sounds/icecrash.ogg");
41 }
42
43 void
44 Stalactite::active_update(float elapsed_time)
45 {
46   if(state == STALACTITE_HANGING) {
47     Player* player = this->get_nearest_player();
48     if (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         SoundManager::current()->play("sounds/cracking.wav", get_pos());
56       }
57     }
58   } else if(state == STALACTITE_SHAKING) {
59     shake_delta = Vector(graphicsRandom.rand(-3,3), 0);
60     if(timer.check()) {
61       state = STALACTITE_FALLING;
62       physic.enable_gravity(true);
63       set_colgroup_active(COLGROUP_MOVING);
64     }
65   } else if(state == STALACTITE_FALLING) {
66     movement = physic.get_movement(elapsed_time);
67   }
68 }
69
70 void
71 Stalactite::squish()
72 {
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   SoundManager::current()->play("sounds/icecrash.ogg", get_pos());
80   set_group(COLGROUP_MOVING_ONLY_STATIC);
81   run_dead_script();
82 }
83
84 void
85 Stalactite::collision_solid(const CollisionHit& hit)
86 {
87   if(state == STALACTITE_FALLING) {
88     if (hit.bottom) squish();
89   }
90   if(state == STALACTITE_SQUISHED) {
91     physic.set_velocity_y(0);
92   }
93 }
94
95 HitResponse
96 Stalactite::collision_player(Player& player, const CollisionHit& )
97 {
98   if(state != STALACTITE_SQUISHED) {
99     player.kill(false);
100   }
101
102   return FORCE_MOVE;
103 }
104
105 HitResponse
106 Stalactite::collision_badguy(BadGuy& other, const CollisionHit& hit)
107 {
108   if (state == STALACTITE_SQUISHED) return FORCE_MOVE;
109
110   // ignore other Stalactites
111   if (dynamic_cast<Stalactite*>(&other)) return FORCE_MOVE;
112
113   if (state != STALACTITE_FALLING) return BadGuy::collision_badguy(other, hit);
114
115   if (other.is_freezable()) {
116     other.freeze();
117   } else {
118     other.kill_fall();
119   }
120
121   return FORCE_MOVE;
122 }
123
124 HitResponse
125 Stalactite::collision_bullet(Bullet& bullet, const CollisionHit& )
126 {
127   if(state == STALACTITE_HANGING) {
128     timer.start(SHAKE_TIME);
129     state = STALACTITE_SHAKING;
130     bullet.remove_me();
131     if(bullet.get_type() == FIRE_BONUS)
132       SoundManager::current()->play("sounds/sizzle.ogg", get_pos());
133     SoundManager::current()->play("sounds/cracking.wav", get_pos());
134   }
135
136   return FORCE_MOVE;
137 }
138
139 void
140 Stalactite::kill_fall()
141 {
142 }
143
144 void
145 Stalactite::draw(DrawingContext& context)
146 {
147   if(get_state() == STATE_INIT || get_state() == STATE_INACTIVE)
148     return;
149
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);
154   } else {
155     sprite->draw(context, get_pos(), layer);
156   }
157 }
158
159 void
160 Stalactite::deactivate()
161 {
162   if(state != STALACTITE_HANGING)
163     remove_me();
164 }
165
166 /* EOF */