fa8e0efd38c94ffcb98dae6be04f9fc9e3b3b241
[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 "sprite/sprite_manager.hpp"
23 #include "supertux/object_factory.hpp"
24
25 #include <math.h>
26
27 namespace {
28 const float KICKSPEED = 500;
29 const int MAXSQUISHES = 10;
30 const float NOKICK_TIME = 0.1f;
31 }
32
33 MrIceBlock::MrIceBlock(const Reader& reader) :
34   WalkingBadguy(reader, "images/creatures/mr_iceblock/mr_iceblock.sprite", "left", "right"), 
35   ice_state(ICESTATE_NORMAL), 
36   nokick_timer(),
37   flat_timer(),
38   squishcount(0)
39 {
40   walk_speed = 80;
41   max_drop_height = 600;
42   sound_manager->preload("sounds/iceblock_bump.wav");
43   sound_manager->preload("sounds/stomp.wav");
44   sound_manager->preload("sounds/kick.wav");
45 }
46
47 MrIceBlock::MrIceBlock(const Vector& pos, Direction d) :
48   WalkingBadguy(pos, d, "images/creatures/mr_iceblock/mr_iceblock.sprite", "left", "right"), 
49   ice_state(ICESTATE_NORMAL), 
50   nokick_timer(),
51   flat_timer(),
52   squishcount(0)
53 {
54   walk_speed = 80;
55   max_drop_height = 600;
56   sound_manager->preload("sounds/iceblock_bump.wav");
57   sound_manager->preload("sounds/stomp.wav");
58   sound_manager->preload("sounds/kick.wav");
59 }
60
61 void
62 MrIceBlock::initialize()
63 {
64   WalkingBadguy::initialize();
65   set_state(ICESTATE_NORMAL);
66 }
67
68 void
69 MrIceBlock::active_update(float elapsed_time)
70 {
71   if(ice_state == ICESTATE_GRABBED)
72     return;
73
74   if(ice_state == ICESTATE_FLAT && flat_timer.check()) {
75     set_state(ICESTATE_NORMAL);
76   }
77
78   if (ice_state == ICESTATE_NORMAL)
79   {
80     WalkingBadguy::active_update(elapsed_time);
81     return;
82   }
83
84   BadGuy::active_update(elapsed_time);
85 }
86
87 bool
88 MrIceBlock::can_break(){
89   return ice_state == ICESTATE_KICKED;
90 }
91
92 void
93 MrIceBlock::collision_solid(const CollisionHit& hit)
94 {
95   update_on_ground_flag(hit);
96
97   if(hit.top || hit.bottom) { // floor or roof
98     physic.set_velocity_y(0);
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       this->set_action(dir == LEFT ? "flat-left" : "flat-right", /* loops = */ -1);
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       this->set_action(dir == LEFT ? "left" : "right", /* loops = */ -1);
235       WalkingBadguy::initialize();
236       break;
237     case ICESTATE_FLAT:
238       if(up) {
239         physic.set_velocity_y(-KICKSPEED);
240       } else {
241         sound_manager->play("sounds/stomp.wav", get_pos());
242         physic.set_velocity_x(0);
243         physic.set_velocity_y(0);
244       }
245       this->set_action(dir == LEFT ? "flat-left" : "flat-right", /* loops = */ -1);
246       flat_timer.start(4);
247       break;
248     case ICESTATE_KICKED:
249       sound_manager->play("sounds/kick.wav", get_pos());
250
251       physic.set_velocity_x(dir == LEFT ? -KICKSPEED : KICKSPEED);
252       this->set_action(dir == LEFT ? "flat-left" : "flat-right", /* loops = */ -1);
253       // we should slide above 1 block holes now...
254       bbox.set_size(34, 31.8f);
255       break;
256     case ICESTATE_GRABBED:
257       flat_timer.stop();
258       break;
259     default:
260       assert(false);
261   }
262   ice_state = state;
263 }
264
265 void
266 MrIceBlock::grab(MovingObject&, const Vector& pos, Direction dir)
267 {
268   movement = pos - get_pos();
269   this->dir = dir;
270   this->set_action(dir == LEFT ? "flat-left" : "flat-right", /* loops = */ -1);
271   set_state(ICESTATE_GRABBED);
272   set_colgroup_active(COLGROUP_DISABLED);
273 }
274
275 void
276 MrIceBlock::ungrab(MovingObject& , Direction dir)
277 {
278   if(dir == UP) {
279     set_state(ICESTATE_FLAT, true);
280   } else {
281     this->dir = dir;
282     set_state(ICESTATE_KICKED);
283   }
284   set_colgroup_active(COLGROUP_MOVING);
285 }
286
287 bool
288 MrIceBlock::is_portable() const
289 {
290   return ice_state == ICESTATE_FLAT;
291 }
292
293 SmartBlock::SmartBlock(const Reader& reader) :
294   MrIceBlock(reader)
295 {
296   max_drop_height = 16;
297   sprite = sprite_manager->create("images/creatures/mr_iceblock/smart_block/smart_block.sprite");
298 }
299
300 /* EOF */