eed83fed7c44844f47d697918246c0e680424e1c
[supertux.git] / src / badguy / mriceblock.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/mriceblock.hpp"
18
19 #include "audio/sound_manager.hpp"
20 #include "object/player.hpp"
21 #include "sprite/sprite.hpp"
22 #include "supertux/object_factory.hpp"
23
24 #include <math.h>
25
26 namespace {
27 const float KICKSPEED = 500;
28 const int MAXSQUISHES = 10;
29 const float NOKICK_TIME = 0.1f;
30 }
31
32 MrIceBlock::MrIceBlock(const Reader& reader) :
33   WalkingBadguy(reader, "images/creatures/mr_iceblock/mr_iceblock.sprite", "left", "right"), 
34   ice_state(ICESTATE_NORMAL), 
35   nokick_timer(),
36   flat_timer(),
37   squishcount(0)
38 {
39   walk_speed = 80;
40   max_drop_height = 600;
41   sound_manager->preload("sounds/iceblock_bump.wav");
42   sound_manager->preload("sounds/stomp.wav");
43   sound_manager->preload("sounds/kick.wav");
44 }
45
46 MrIceBlock::MrIceBlock(const Vector& pos, Direction d) :
47   WalkingBadguy(pos, d, "images/creatures/mr_iceblock/mr_iceblock.sprite", "left", "right"), 
48   ice_state(ICESTATE_NORMAL), 
49   nokick_timer(),
50   flat_timer(),
51   squishcount(0)
52 {
53   walk_speed = 80;
54   max_drop_height = 600;
55   sound_manager->preload("sounds/iceblock_bump.wav");
56   sound_manager->preload("sounds/stomp.wav");
57   sound_manager->preload("sounds/kick.wav");
58 }
59
60 void
61 MrIceBlock::initialize()
62 {
63   WalkingBadguy::initialize();
64   set_state(ICESTATE_NORMAL);
65 }
66
67 void
68 MrIceBlock::active_update(float elapsed_time)
69 {
70   if(ice_state == ICESTATE_GRABBED)
71     return;
72
73   if(ice_state == ICESTATE_FLAT && flat_timer.check()) {
74     set_state(ICESTATE_NORMAL);
75   }
76
77   if (ice_state == ICESTATE_NORMAL)
78   {
79     WalkingBadguy::active_update(elapsed_time);
80     return;
81   }
82
83   BadGuy::active_update(elapsed_time);
84 }
85
86 bool
87 MrIceBlock::can_break(){
88   return ice_state == ICESTATE_KICKED;
89 }
90
91 void
92 MrIceBlock::collision_solid(const CollisionHit& hit)
93 {
94   update_on_ground_flag(hit);
95
96   if(hit.top || hit.bottom) { // floor or roof
97     physic.set_velocity_y(0);
98     return;
99   }
100
101   // hit left or right
102   switch(ice_state) {
103     case ICESTATE_NORMAL:
104       WalkingBadguy::collision_solid(hit);
105       break;
106     case ICESTATE_KICKED: {
107       if((hit.right && dir == RIGHT) || (hit.left && dir == LEFT)) {
108         dir = (dir == LEFT) ? RIGHT : LEFT;
109         sound_manager->play("sounds/iceblock_bump.wav", get_pos());
110         physic.set_velocity_x(-physic.get_velocity_x()*.975);
111       }
112       sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
113       if(fabsf(physic.get_velocity_x()) < walk_speed*1.5)
114         set_state(ICESTATE_NORMAL);
115       break;
116     }
117     case ICESTATE_FLAT:
118       physic.set_velocity_x(0);
119       break;
120     case ICESTATE_GRABBED:
121       break;
122   }
123 }
124
125 HitResponse
126 MrIceBlock::collision(GameObject& object, const CollisionHit& hit)
127 {
128   if(ice_state == ICESTATE_GRABBED)
129     return FORCE_MOVE;
130
131   return BadGuy::collision(object, hit);
132 }
133
134 HitResponse
135 MrIceBlock::collision_player(Player& player, const CollisionHit& hit)
136 {
137   if(dir == UP) {
138     return FORCE_MOVE;
139   }
140
141   // handle kicks from left or right side
142   if(ice_state == ICESTATE_FLAT && get_state() == STATE_ACTIVE) {
143     if(hit.left) {
144       dir = RIGHT;
145       player.kick();
146       set_state(ICESTATE_KICKED);
147       return FORCE_MOVE;
148     } else if(hit.right) {
149       dir = LEFT;
150       player.kick();
151       set_state(ICESTATE_KICKED);
152       return FORCE_MOVE;
153     }
154   }
155
156   return BadGuy::collision_player(player, hit);
157 }
158
159 HitResponse
160 MrIceBlock::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
161 {
162   switch(ice_state) {
163     case ICESTATE_NORMAL:
164       return WalkingBadguy::collision_badguy(badguy, hit);
165     case ICESTATE_FLAT:
166       return FORCE_MOVE;
167     case ICESTATE_KICKED:
168       badguy.kill_fall();
169       return FORCE_MOVE;
170     default:
171       assert(false);
172   }
173
174   return ABORT_MOVE;
175 }
176
177 bool
178 MrIceBlock::collision_squished(GameObject& object)
179 {
180   Player* player = dynamic_cast<Player*>(&object);
181   if(player && (player->does_buttjump || player->is_invincible())) {
182     player->bounce(*this);
183     kill_fall();
184     return true;
185   }
186
187   switch(ice_state) {
188     case ICESTATE_KICKED:
189     {
190       BadGuy* badguy = dynamic_cast<BadGuy*>(&object);
191       if (badguy) {
192         badguy->kill_fall();
193         break;
194       }
195     }
196
197     // fall through
198     case ICESTATE_NORMAL:
199     {
200       squishcount++;
201       if (squishcount >= MAXSQUISHES) {
202         kill_fall();
203         return true;
204       }
205     }
206
207     set_state(ICESTATE_FLAT);
208     nokick_timer.start(NOKICK_TIME);
209     break;
210     case ICESTATE_FLAT:
211     {
212       MovingObject* movingobject = dynamic_cast<MovingObject*>(&object);
213       if (movingobject && (movingobject->get_pos().x < get_pos().x)) {
214         dir = RIGHT;
215       } else {
216         dir = LEFT;
217       }
218     }
219     if (nokick_timer.check()) set_state(ICESTATE_KICKED);
220     break;
221     case ICESTATE_GRABBED:
222       assert(false);
223       break;
224   }
225
226   if (player) player->bounce(*this);
227   return true;
228 }
229
230 void
231 MrIceBlock::set_state(IceState state)
232 {
233   if(ice_state == state)
234     return;
235
236   switch(state) {
237     case ICESTATE_NORMAL:
238       WalkingBadguy::initialize();
239       break;
240     case ICESTATE_FLAT:
241       if(dir == UP) {
242         physic.set_velocity_y(-KICKSPEED);
243         bbox.set_size(34, 31.8f);
244       } else {
245         sound_manager->play("sounds/stomp.wav", get_pos());
246         physic.set_velocity_x(0);
247         physic.set_velocity_y(0);
248
249         sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
250       }
251       flat_timer.start(4);
252       break;
253     case ICESTATE_KICKED:
254       sound_manager->play("sounds/kick.wav", get_pos());
255
256       physic.set_velocity_x(dir == LEFT ? -KICKSPEED : KICKSPEED);
257       sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
258       // we should slide above 1 block holes now...
259       bbox.set_size(34, 31.8f);
260       break;
261     case ICESTATE_GRABBED:
262       flat_timer.stop();
263       break;
264     default:
265       assert(false);
266   }
267   ice_state = state;
268 }
269
270 void
271 MrIceBlock::grab(MovingObject&, const Vector& pos, Direction dir)
272 {
273   movement = pos - get_pos();
274   this->dir = dir;
275   sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
276   set_state(ICESTATE_GRABBED);
277   set_colgroup_active(COLGROUP_DISABLED);
278 }
279
280 void
281 MrIceBlock::ungrab(MovingObject& , Direction dir)
282 {
283   this->dir = dir;
284   set_state(dir == UP ? ICESTATE_FLAT : ICESTATE_KICKED);
285   set_colgroup_active(COLGROUP_MOVING);
286 }
287
288 bool
289 MrIceBlock::is_portable() const
290 {
291   return ice_state == ICESTATE_FLAT;
292 }
293
294 /* EOF */