supertux/main, control/haptic_manager: Add SDL 1.2 compatibility code.
[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   // handle kicks from left or right side
138   if(ice_state == ICESTATE_FLAT && get_state() == STATE_ACTIVE) {
139     if(hit.left) {
140       dir = RIGHT;
141       player.kick();
142       set_state(ICESTATE_KICKED);
143       return FORCE_MOVE;
144     } else if(hit.right) {
145       dir = LEFT;
146       player.kick();
147       set_state(ICESTATE_KICKED);
148       return FORCE_MOVE;
149     }
150   }
151
152   return BadGuy::collision_player(player, hit);
153 }
154
155 HitResponse
156 MrIceBlock::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
157 {
158   switch(ice_state) {
159     case ICESTATE_NORMAL:
160       return WalkingBadguy::collision_badguy(badguy, hit);
161     case ICESTATE_FLAT:
162       return FORCE_MOVE;
163     case ICESTATE_KICKED:
164       badguy.kill_fall();
165       return FORCE_MOVE;
166     default:
167       assert(false);
168   }
169
170   return ABORT_MOVE;
171 }
172
173 bool
174 MrIceBlock::collision_squished(GameObject& object)
175 {
176   Player* player = dynamic_cast<Player*>(&object);
177   if(player && (player->does_buttjump || player->is_invincible())) {
178     player->bounce(*this);
179     kill_fall();
180     return true;
181   }
182
183   switch(ice_state) {
184     case ICESTATE_KICKED:
185     {
186       BadGuy* badguy = dynamic_cast<BadGuy*>(&object);
187       if (badguy) {
188         badguy->kill_fall();
189         break;
190       }
191     }
192
193     // fall through
194     case ICESTATE_NORMAL:
195     {
196       squishcount++;
197       if (squishcount >= MAXSQUISHES) {
198         kill_fall();
199         return true;
200       }
201     }
202
203     set_state(ICESTATE_FLAT);
204     nokick_timer.start(NOKICK_TIME);
205     break;
206     case ICESTATE_FLAT:
207     {
208       MovingObject* movingobject = dynamic_cast<MovingObject*>(&object);
209       if (movingobject && (movingobject->get_pos().x < get_pos().x)) {
210         dir = RIGHT;
211       } else {
212         dir = LEFT;
213       }
214     }
215     if (nokick_timer.check()) set_state(ICESTATE_KICKED);
216     break;
217     case ICESTATE_GRABBED:
218       assert(false);
219       break;
220   }
221
222   if (player) player->bounce(*this);
223   return true;
224 }
225
226 void
227 MrIceBlock::set_state(IceState state, bool up)
228 {
229   if(ice_state == state)
230     return;
231
232   switch(state) {
233     case ICESTATE_NORMAL:
234       WalkingBadguy::initialize();
235       break;
236     case ICESTATE_FLAT:
237       if(up) {
238         physic.set_velocity_y(-KICKSPEED);
239       } else {
240         sound_manager->play("sounds/stomp.wav", get_pos());
241         physic.set_velocity_x(0);
242         physic.set_velocity_y(0);
243       }
244       sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
245       flat_timer.start(4);
246       break;
247     case ICESTATE_KICKED:
248       sound_manager->play("sounds/kick.wav", get_pos());
249
250       physic.set_velocity_x(dir == LEFT ? -KICKSPEED : KICKSPEED);
251       sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
252       // we should slide above 1 block holes now...
253       bbox.set_size(34, 31.8f);
254       break;
255     case ICESTATE_GRABBED:
256       flat_timer.stop();
257       break;
258     default:
259       assert(false);
260   }
261   ice_state = state;
262 }
263
264 void
265 MrIceBlock::grab(MovingObject&, const Vector& pos, Direction dir)
266 {
267   movement = pos - get_pos();
268   this->dir = dir;
269   sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
270   set_state(ICESTATE_GRABBED);
271   set_colgroup_active(COLGROUP_DISABLED);
272 }
273
274 void
275 MrIceBlock::ungrab(MovingObject& , Direction dir)
276 {
277   if(dir == UP) {
278     set_state(ICESTATE_FLAT, true);
279   } else {
280     this->dir = dir;
281     set_state(ICESTATE_KICKED);
282   }
283   set_colgroup_active(COLGROUP_MOVING);
284 }
285
286 bool
287 MrIceBlock::is_portable() const
288 {
289   return ice_state == ICESTATE_FLAT;
290 }
291
292 /* EOF */