Hardcoded stay-on-platform behaviour as follows: Mr. Bomb, Mr. Tree and the Totem...
[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 static const float WALKSPEED = 80;
26 static const float KICKSPEED = 500;
27 static const int MAXSQUISHES = 10;
28
29 MrIceBlock::MrIceBlock(const lisp::Lisp& reader)
30   : ice_state(ICESTATE_NORMAL), squishcount(0)
31 {
32   reader.get("x", start_position.x);
33   reader.get("y", start_position.y);
34   bbox.set_size(31.8, 31.8);
35   sprite = sprite_manager->create("images/creatures/mr_iceblock/mr_iceblock.sprite");
36   set_direction = false;
37 }
38
39 MrIceBlock::MrIceBlock(float pos_x, float pos_y, Direction d)
40   : ice_state(ICESTATE_NORMAL), squishcount(0)
41 {
42   start_position.x = pos_x;
43   start_position.y = pos_y;
44   bbox.set_size(31.8, 31.8);
45   sprite = sprite_manager->create("images/creatures/mr_iceblock/mr_iceblock.sprite");
46   set_direction = true;
47   initial_direction = d;
48 }
49
50 void
51 MrIceBlock::write(lisp::Writer& writer)
52 {
53   writer.start_list("mriceblock");
54
55   writer.write_float("x", start_position.x);
56   writer.write_float("y", start_position.y);
57
58   writer.end_list("mriceblock");
59 }
60
61 void
62 MrIceBlock::activate()
63 {
64   if (set_direction) {
65     dir = initial_direction;
66   }
67
68   physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
69   sprite->set_action(dir == LEFT ? "left" : "right");
70 }
71
72 void
73 MrIceBlock::active_update(float elapsed_time)
74 {
75   if(ice_state == ICESTATE_GRABBED)
76     return;
77
78   if(ice_state == ICESTATE_FLAT && flat_timer.check()) {
79     set_state(ICESTATE_NORMAL);
80   }
81
82   if (ice_state == ICESTATE_NORMAL && might_fall(601))
83   {
84     dir = (dir == LEFT ? RIGHT : LEFT);
85     sprite->set_action(dir == LEFT ? "left" : "right");
86     physic.set_velocity_x(-physic.get_velocity_x());
87   }
88
89   BadGuy::active_update(elapsed_time);
90 }
91
92 HitResponse
93 MrIceBlock::collision_solid(GameObject& object, const CollisionHit& hit)
94 {
95   if(fabsf(hit.normal.y) > .5) { // floor or roof
96     physic.set_velocity_y(0);
97     return CONTINUE;
98   }
99   // hit left or right
100   switch(ice_state) {
101     case ICESTATE_NORMAL:
102       dir = dir == LEFT ? RIGHT : LEFT;
103       sprite->set_action(dir == LEFT ? "left" : "right");
104       physic.set_velocity_x(-physic.get_velocity_x());       
105       break;
106     case ICESTATE_KICKED: {
107       BonusBlock* bonusblock = dynamic_cast<BonusBlock*> (&object);
108       if(bonusblock) {
109         bonusblock->try_open();
110       }
111       Brick* brick = dynamic_cast<Brick*> (&object);
112       if(brick) {
113         brick->try_break();
114       }
115       
116       dir = dir == LEFT ? RIGHT : LEFT;
117       sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
118       physic.set_velocity_x(-physic.get_velocity_x());
119       sound_manager->play("sounds/iceblock_bump.wav", get_pos());
120       break;
121     }
122     case ICESTATE_FLAT:
123       physic.set_velocity_x(0);
124       break;
125     case ICESTATE_GRABBED:
126       return FORCE_MOVE;
127   }
128
129   return CONTINUE;
130 }
131
132 HitResponse
133 MrIceBlock::collision(GameObject& object, const CollisionHit& hit)
134 {
135   if(ice_state == ICESTATE_GRABBED)
136     return FORCE_MOVE;
137
138   return BadGuy::collision(object, hit);
139 }
140
141 HitResponse
142 MrIceBlock::collision_player(Player& player, const CollisionHit& hit)
143 {
144   if(ice_state == ICESTATE_GRABBED)
145     return FORCE_MOVE;
146
147   // handle kicks from left or right side
148   if(ice_state == ICESTATE_FLAT && get_state() == STATE_ACTIVE) {
149     // hit from left side
150     if(hit.normal.x > 0.7) {
151       dir = RIGHT;
152       player.kick();
153       set_state(ICESTATE_KICKED);
154       return FORCE_MOVE;
155     } else if(hit.normal.x < -0.7) {
156       dir = LEFT;
157       player.kick();
158       set_state(ICESTATE_KICKED);
159       return FORCE_MOVE;
160     }
161   }
162   
163   return BadGuy::collision_player(player, hit);
164 }
165
166 HitResponse
167 MrIceBlock::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
168 {
169   switch(ice_state) {
170     case ICESTATE_NORMAL:
171       if(fabsf(hit.normal.x) > .8) {
172         dir = dir == LEFT ? RIGHT : LEFT;
173         sprite->set_action(dir == LEFT ? "left" : "right");
174         physic.set_velocity_x(-physic.get_velocity_x());               
175       }
176       return CONTINUE;
177     case ICESTATE_FLAT:
178       return FORCE_MOVE;
179     case ICESTATE_KICKED:
180       badguy.kill_fall();
181       return FORCE_MOVE;
182     default:
183       assert(false);
184   }
185
186   return ABORT_MOVE;
187 }
188
189 bool
190 MrIceBlock::collision_squished(Player& player)
191 {
192   switch(ice_state) {
193     case ICESTATE_KICKED:
194     case ICESTATE_NORMAL:
195       squishcount++;
196       if(squishcount >= MAXSQUISHES) {
197         kill_fall();
198         return true;
199       }
200
201       set_state(ICESTATE_FLAT);
202       break;
203     case ICESTATE_FLAT:
204       if(player.get_pos().x < get_pos().x) {
205         dir = RIGHT;
206       } else {
207         dir = LEFT;
208       }
209       set_state(ICESTATE_KICKED);
210       break;
211     case ICESTATE_GRABBED:
212       assert(false);
213       break;
214   }
215
216   player.bounce(*this);
217   return true;
218 }
219
220 void
221 MrIceBlock::set_state(IceState state)
222 {
223   if(ice_state == state)
224     return;
225   
226   if(state == ICESTATE_FLAT)
227     flags |= FLAG_PORTABLE;
228   else
229     flags &= ~FLAG_PORTABLE;
230
231   if(ice_state == ICESTATE_KICKED) {
232     bbox.set_size(31.8, 31.8);
233   }
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")