Stay-on-platform Badguys no longer go crazy when falling down
[supertux.git] / src / badguy / mriceblock.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 #include <config.h>
21
22 #include "mriceblock.hpp"
23 #include "object/block.hpp"
24
25 namespace {
26   const float WALKSPEED = 80;
27   const float KICKSPEED = 500;
28   const int MAXSQUISHES = 10;
29 }
30
31 MrIceBlock::MrIceBlock(const lisp::Lisp& reader)
32   : BadGuy(reader, "images/creatures/mr_iceblock/mr_iceblock.sprite"), ice_state(ICESTATE_NORMAL), squishcount(0)
33 {
34   sound_manager->preload("sounds/iceblock_bump.wav");
35   sound_manager->preload("sounds/stomp.wav");
36   sound_manager->preload("sounds/kick.wav");
37 }
38
39 MrIceBlock::MrIceBlock(const Vector& pos, Direction d)
40   : BadGuy(pos, d, "images/creatures/mr_iceblock/mr_iceblock.sprite"), ice_state(ICESTATE_NORMAL), squishcount(0)
41 {
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 void
48 MrIceBlock::write(lisp::Writer& writer)
49 {
50   writer.start_list("mriceblock");
51
52   writer.write_float("x", start_position.x);
53   writer.write_float("y", start_position.y);
54
55   writer.end_list("mriceblock");
56 }
57
58 void
59 MrIceBlock::activate()
60 {
61   physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
62   sprite->set_action(dir == LEFT ? "left" : "right");
63   set_state(ICESTATE_NORMAL);
64 }
65
66 void
67 MrIceBlock::active_update(float elapsed_time)
68 {
69   if(ice_state == ICESTATE_GRABBED)
70     return;
71
72   if(ice_state == ICESTATE_FLAT && flat_timer.check()) {
73     set_state(ICESTATE_NORMAL);
74   }
75
76   if (ice_state == ICESTATE_NORMAL && on_ground() && might_fall(601))
77   {
78     dir = (dir == LEFT ? RIGHT : LEFT);
79     sprite->set_action(dir == LEFT ? "left" : "right");
80     physic.set_velocity_x(-physic.get_velocity_x());
81   }
82
83   BadGuy::active_update(elapsed_time);
84 }
85
86 void
87 MrIceBlock::collision_solid(const CollisionHit& hit)
88 {
89   update_on_ground_flag(hit);
90
91   if(hit.top || hit.bottom) { // floor or roof
92     physic.set_velocity_y(0);
93     return;
94   }
95
96   // hit left or right
97   switch(ice_state) {
98     case ICESTATE_NORMAL:
99       if(hit.right && dir == RIGHT) {
100         dir = LEFT;
101         sprite->set_action(dir == LEFT ? "left" : "right");
102         physic.set_velocity_x(-physic.get_velocity_x());       
103       } else if(hit.left && dir == LEFT) {
104         dir = RIGHT;
105         sprite->set_action(dir == LEFT ? "left" : "right");
106         physic.set_velocity_x(-physic.get_velocity_x());       
107       }
108       break;
109     case ICESTATE_KICKED: {
110 #if 0
111       // TODO move these into bonusblock class
112       BonusBlock* bonusblock = dynamic_cast<BonusBlock*> (&object);
113       if(bonusblock) {
114         bonusblock->try_open();
115       }
116       Brick* brick = dynamic_cast<Brick*> (&object);
117       if(brick) {
118         brick->try_break();
119       }
120 #endif
121       if(hit.right && dir == RIGHT) {
122         dir = LEFT;
123         sound_manager->play("sounds/iceblock_bump.wav", get_pos());
124         physic.set_velocity_x(-KICKSPEED);
125       } else if(hit.left && dir == LEFT) {
126         dir = RIGHT;
127         sound_manager->play("sounds/iceblock_bump.wav", get_pos());
128         physic.set_velocity_x(KICKSPEED);
129       }
130       sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
131       break;
132     }
133     case ICESTATE_FLAT:
134       physic.set_velocity_x(0);
135       break;
136     case ICESTATE_GRABBED:
137       break;
138   }
139 }
140
141 HitResponse
142 MrIceBlock::collision(GameObject& object, const CollisionHit& hit)
143 {
144   if(ice_state == ICESTATE_GRABBED)
145     return FORCE_MOVE;
146
147   return BadGuy::collision(object, hit);
148 }
149
150 HitResponse
151 MrIceBlock::collision_player(Player& player, const CollisionHit& hit)
152 {
153   if(ice_state == ICESTATE_GRABBED)
154     return FORCE_MOVE;
155
156   // handle kicks from left or right side
157   if(ice_state == ICESTATE_FLAT && get_state() == STATE_ACTIVE) {
158     if(hit.left) {
159       dir = RIGHT;
160       player.kick();
161       set_state(ICESTATE_KICKED);
162       return FORCE_MOVE;
163     } else if(hit.right) {
164       dir = LEFT;
165       player.kick();
166       set_state(ICESTATE_KICKED);
167       return FORCE_MOVE;
168     }
169   }
170   
171   return BadGuy::collision_player(player, hit);
172 }
173
174 HitResponse
175 MrIceBlock::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
176 {
177   switch(ice_state) {
178     case ICESTATE_NORMAL:
179       if(hit.left || hit.right) {
180         dir = dir == LEFT ? RIGHT : LEFT;
181         sprite->set_action(dir == LEFT ? "left" : "right");
182         physic.set_velocity_x(-physic.get_velocity_x());               
183       }
184       return CONTINUE;
185     case ICESTATE_FLAT:
186       return FORCE_MOVE;
187     case ICESTATE_KICKED:
188       badguy.kill_fall();
189       return FORCE_MOVE;
190     default:
191       assert(false);
192   }
193
194   return ABORT_MOVE;
195 }
196
197 bool
198 MrIceBlock::collision_squished(Player& player)
199 {
200   switch(ice_state) {
201     case ICESTATE_KICKED:
202     case ICESTATE_NORMAL:
203       squishcount++;
204       if(squishcount >= MAXSQUISHES) {
205         kill_fall();
206         return true;
207       }
208
209       set_state(ICESTATE_FLAT);
210       break;
211     case ICESTATE_FLAT:
212       if(player.get_pos().x < get_pos().x) {
213         dir = RIGHT;
214       } else {
215         dir = LEFT;
216       }
217       set_state(ICESTATE_KICKED);
218       break;
219     case ICESTATE_GRABBED:
220       assert(false);
221       break;
222   }
223
224   player.bounce(*this);
225   return true;
226 }
227
228 void
229 MrIceBlock::set_state(IceState state)
230 {
231   if(ice_state == state)
232     return;
233   
234   if(state == ICESTATE_FLAT)
235     flags |= FLAG_PORTABLE;
236   else
237     flags &= ~FLAG_PORTABLE;
238
239   switch(state) {
240     case ICESTATE_NORMAL:
241       physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
242       sprite->set_action(dir == LEFT ? "left" : "right");
243       break;
244     case ICESTATE_FLAT:
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       flat_timer.start(4);
251       break;
252     case ICESTATE_KICKED:
253       sound_manager->play("sounds/kick.wav", get_pos());
254
255       physic.set_velocity_x(dir == LEFT ? -KICKSPEED : KICKSPEED);
256       sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
257       // we should slide above 1 block holes now...
258       bbox.set_size(34, 31.8);
259       break;
260     case ICESTATE_GRABBED:
261       flat_timer.stop();
262       break;
263     default:
264       assert(false);
265   }
266   ice_state = state;
267 }
268
269 void
270 MrIceBlock::grab(MovingObject&, const Vector& pos, Direction dir)
271 {
272   movement = pos - get_pos();
273   this->dir = dir;
274   sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
275   set_state(ICESTATE_GRABBED);
276   set_group(COLGROUP_DISABLED);
277 }
278
279 void
280 MrIceBlock::ungrab(MovingObject& , Direction dir)
281 {
282   this->dir = dir;
283   set_state(ICESTATE_KICKED);
284   set_group(COLGROUP_MOVING);
285 }
286
287 IMPLEMENT_FACTORY(MrIceBlock, "mriceblock")