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