fix mriceblock for now, something similar should be done for the other badguys...
[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 && 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   if(hit.top || hit.bottom) { // floor or roof
90     physic.set_velocity_y(0);
91     return;
92   }
93
94   // hit left or right
95   switch(ice_state) {
96     case ICESTATE_NORMAL:
97       if(hit.right && dir == RIGHT) {
98         dir = LEFT;
99       } else if(hit.left && dir == LEFT) {
100         dir = RIGHT;
101       }
102       sprite->set_action(dir == LEFT ? "left" : "right");
103       physic.set_velocity_x(-physic.get_velocity_x());       
104       break;
105     case ICESTATE_KICKED: {
106 #if 0
107       // TODO move these into bonusblock class
108       BonusBlock* bonusblock = dynamic_cast<BonusBlock*> (&object);
109       if(bonusblock) {
110         bonusblock->try_open();
111       }
112       Brick* brick = dynamic_cast<Brick*> (&object);
113       if(brick) {
114         brick->try_break();
115       }
116 #endif
117       if(hit.right && dir == RIGHT) {
118         dir = LEFT;
119         sound_manager->play("sounds/iceblock_bump.wav", get_pos());
120         physic.set_velocity_x(-KICKSPEED);
121       } else if(hit.left && dir == LEFT) {
122         dir = RIGHT;
123         sound_manager->play("sounds/iceblock_bump.wav", get_pos());
124         physic.set_velocity_x(KICKSPEED);
125       }
126       sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
127       break;
128     }
129     case ICESTATE_FLAT:
130       physic.set_velocity_x(0);
131       break;
132     case ICESTATE_GRABBED:
133       break;
134   }
135 }
136
137 HitResponse
138 MrIceBlock::collision(GameObject& object, const CollisionHit& hit)
139 {
140   if(ice_state == ICESTATE_GRABBED)
141     return FORCE_MOVE;
142
143   return BadGuy::collision(object, hit);
144 }
145
146 HitResponse
147 MrIceBlock::collision_player(Player& player, const CollisionHit& hit)
148 {
149   if(ice_state == ICESTATE_GRABBED)
150     return FORCE_MOVE;
151
152   // handle kicks from left or right side
153   if(ice_state == ICESTATE_FLAT && get_state() == STATE_ACTIVE) {
154     if(hit.left) {
155       dir = RIGHT;
156       player.kick();
157       set_state(ICESTATE_KICKED);
158       return FORCE_MOVE;
159     } else if(hit.right) {
160       dir = LEFT;
161       player.kick();
162       set_state(ICESTATE_KICKED);
163       return FORCE_MOVE;
164     }
165   }
166   
167   return BadGuy::collision_player(player, hit);
168 }
169
170 HitResponse
171 MrIceBlock::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
172 {
173   switch(ice_state) {
174     case ICESTATE_NORMAL:
175       if(hit.left || hit.right) {
176         dir = dir == LEFT ? RIGHT : LEFT;
177         sprite->set_action(dir == LEFT ? "left" : "right");
178         physic.set_velocity_x(-physic.get_velocity_x());               
179       }
180       return CONTINUE;
181     case ICESTATE_FLAT:
182       return FORCE_MOVE;
183     case ICESTATE_KICKED:
184       badguy.kill_fall();
185       return FORCE_MOVE;
186     default:
187       assert(false);
188   }
189
190   return ABORT_MOVE;
191 }
192
193 bool
194 MrIceBlock::collision_squished(Player& player)
195 {
196   switch(ice_state) {
197     case ICESTATE_KICKED:
198     case ICESTATE_NORMAL:
199       squishcount++;
200       if(squishcount >= MAXSQUISHES) {
201         kill_fall();
202         return true;
203       }
204
205       set_state(ICESTATE_FLAT);
206       break;
207     case ICESTATE_FLAT:
208       if(player.get_pos().x < get_pos().x) {
209         dir = RIGHT;
210       } else {
211         dir = LEFT;
212       }
213       set_state(ICESTATE_KICKED);
214       break;
215     case ICESTATE_GRABBED:
216       assert(false);
217       break;
218   }
219
220   player.bounce(*this);
221   return true;
222 }
223
224 void
225 MrIceBlock::set_state(IceState state)
226 {
227   if(ice_state == state)
228     return;
229   
230   if(state == ICESTATE_FLAT)
231     flags |= FLAG_PORTABLE;
232   else
233     flags &= ~FLAG_PORTABLE;
234
235   switch(state) {
236     case ICESTATE_NORMAL:
237       physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
238       sprite->set_action(dir == LEFT ? "left" : "right");
239       break;
240     case ICESTATE_FLAT:
241       sound_manager->play("sounds/stomp.wav", get_pos());
242       physic.set_velocity_x(0);
243       physic.set_velocity_y(0); 
244       
245       sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
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       sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
253       // we should slide above 1 block holes now...
254       bbox.set_size(34, 31.8);
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   sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
271   set_state(ICESTATE_GRABBED);
272   set_group(COLGROUP_DISABLED);
273 }
274
275 void
276 MrIceBlock::ungrab(MovingObject& , Direction dir)
277 {
278   this->dir = dir;
279   set_state(ICESTATE_KICKED);
280   set_group(COLGROUP_MOVING);
281 }
282
283 IMPLEMENT_FACTORY(MrIceBlock, "mriceblock")