more work on MrIceBlock, you can carry him around and kick him again
[supertux.git] / src / badguy / mriceblock.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2005 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
19 //  02111-1307, USA.
20
21 #include <config.h>
22
23 #include "mriceblock.hpp"
24 #include "object/block.hpp"
25
26 static const float WALKSPEED = 80;
27 static const float KICKSPEED = 500;
28 static const int MAXSQUISHES = 10;
29
30 MrIceBlock::MrIceBlock(const lisp::Lisp& reader)
31   : ice_state(ICESTATE_NORMAL), squishcount(0)
32 {
33   reader.get("x", start_position.x);
34   reader.get("y", start_position.y);
35   bbox.set_size(31.8, 31.8);
36   sprite = sprite_manager->create("mriceblock");
37   set_direction = false;
38 }
39
40 MrIceBlock::MrIceBlock(float pos_x, float pos_y, Direction d)
41   : ice_state(ICESTATE_NORMAL), squishcount(0)
42 {
43   start_position.x = pos_x;
44   start_position.y = pos_y;
45   bbox.set_size(31.8, 31.8);
46   sprite = sprite_manager->create("mriceblock");
47   set_direction = true;
48   initial_direction = d;
49 }
50
51 void
52 MrIceBlock::write(lisp::Writer& writer)
53 {
54   writer.start_list("mriceblock");
55
56   writer.write_float("x", start_position.x);
57   writer.write_float("y", start_position.y);
58
59   writer.end_list("mriceblock");
60 }
61
62 void
63 MrIceBlock::activate()
64 {
65   if (set_direction) {
66     dir = initial_direction;
67   }
68
69   physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
70   sprite->set_action(dir == LEFT ? "left" : "right");
71 }
72
73 void
74 MrIceBlock::active_update(float elapsed_time)
75 {
76   if(ice_state == ICESTATE_GRABBED)
77     return;
78
79   if(ice_state == ICESTATE_FLAT && flat_timer.check()) {
80     set_state(ICESTATE_NORMAL);
81   }
82   
83   BadGuy::active_update(elapsed_time);
84 }
85
86 HitResponse
87 MrIceBlock::collision_solid(GameObject& object, const CollisionHit& hit)
88 {
89   if(fabsf(hit.normal.y) > .5) { // floor or roof
90     physic.set_velocity_y(0);
91     return CONTINUE;
92   }
93   // hit left or right
94   switch(ice_state) {
95     case ICESTATE_NORMAL:
96       dir = dir == LEFT ? RIGHT : LEFT;
97       sprite->set_action(dir == LEFT ? "left" : "right");
98       physic.set_velocity_x(-physic.get_velocity_x());       
99       break;
100     case ICESTATE_KICKED: {
101       BonusBlock* bonusblock = dynamic_cast<BonusBlock*> (&object);
102       if(bonusblock) {
103         bonusblock->try_open();
104       }
105       Brick* brick = dynamic_cast<Brick*> (&object);
106       if(brick) {
107         brick->try_break();
108       }
109       
110       dir = dir == LEFT ? RIGHT : LEFT;
111       sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
112       physic.set_velocity_x(-physic.get_velocity_x());
113       sound_manager->play("sounds/iceblock_bump.wav", get_pos());
114       break;
115     }
116     case ICESTATE_FLAT:
117       physic.set_velocity_x(0);
118       break;
119     case ICESTATE_GRABBED:
120       return FORCE_MOVE;
121   }
122
123   return CONTINUE;
124 }
125
126 HitResponse
127 MrIceBlock::collision(GameObject& object, const CollisionHit& hit)
128 {
129   if(ice_state == ICESTATE_GRABBED)
130     return FORCE_MOVE;
131
132   return BadGuy::collision(object, hit);
133 }
134
135 HitResponse
136 MrIceBlock::collision_player(Player& player, const CollisionHit& hit)
137 {
138   if(ice_state == ICESTATE_GRABBED)
139     return FORCE_MOVE;
140
141   // handle kicks from left or right side
142   if(ice_state == ICESTATE_FLAT && get_state() == STATE_ACTIVE) {
143     // hit from left side
144     if(hit.normal.x > 0.7) {
145       dir = RIGHT;
146       player.kick();
147       set_state(ICESTATE_KICKED);
148       return FORCE_MOVE;
149     } else if(hit.normal.x < -0.7) {
150       dir = LEFT;
151       player.kick();
152       set_state(ICESTATE_KICKED);
153       return FORCE_MOVE;
154     }
155   }
156   
157   return BadGuy::collision_player(player, hit);
158 }
159
160 HitResponse
161 MrIceBlock::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
162 {
163   switch(ice_state) {
164     case ICESTATE_NORMAL:
165       if(fabsf(hit.normal.x) > .8) {
166         dir = dir == LEFT ? RIGHT : LEFT;
167         sprite->set_action(dir == LEFT ? "left" : "right");
168         physic.set_velocity_x(-physic.get_velocity_x());               
169       }
170       return CONTINUE;
171     case ICESTATE_FLAT:
172       return FORCE_MOVE;
173     case ICESTATE_KICKED:
174       badguy.kill_fall();
175       return FORCE_MOVE;
176     default:
177       assert(false);
178   }
179
180   return ABORT_MOVE;
181 }
182
183 bool
184 MrIceBlock::collision_squished(Player& player)
185 {
186   switch(ice_state) {
187     case ICESTATE_KICKED:
188     case ICESTATE_NORMAL:
189       squishcount++;
190       if(squishcount >= MAXSQUISHES) {
191         kill_fall();
192         return true;
193       }
194
195       set_state(ICESTATE_FLAT);
196       break;
197     case ICESTATE_FLAT:
198       if(player.get_pos().x < get_pos().x) {
199         dir = RIGHT;
200       } else {
201         dir = LEFT;
202       }
203       set_state(ICESTATE_KICKED);
204       break;
205     case ICESTATE_GRABBED:
206       assert(false);
207       break;
208   }
209
210   player.bounce(*this);
211   return true;
212 }
213
214 void
215 MrIceBlock::set_state(IceState state)
216 {
217   if(ice_state == state)
218     return;
219   
220   if(state == ICESTATE_FLAT)
221     flags |= FLAG_PORTABLE;
222   else
223     flags &= ~FLAG_PORTABLE;
224
225   if(ice_state == ICESTATE_KICKED) {
226     bbox.set_size(31.8, 31.8);
227   }
228
229   switch(state) {
230     case ICESTATE_NORMAL:
231       physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
232       sprite->set_action(dir == LEFT ? "left" : "right");
233       break;
234     case ICESTATE_FLAT:
235       sound_manager->play("sounds/stomp.wav", get_pos());
236       physic.set_velocity_x(0);
237       physic.set_velocity_y(0); 
238       
239       sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
240       flat_timer.start(4);
241       break;
242     case ICESTATE_KICKED:
243       sound_manager->play("sounds/kick.wav", get_pos());
244
245       physic.set_velocity_x(dir == LEFT ? -KICKSPEED : KICKSPEED);
246       sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
247       // we should slide above 1 block holes now...
248       bbox.set_size(32.5, 31.8);
249       break;
250     case ICESTATE_GRABBED:
251       flat_timer.stop();
252       break;
253     default:
254       assert(false);
255   }
256   ice_state = state;
257 }
258
259 void
260 MrIceBlock::grab(MovingObject&, const Vector& pos, Direction dir)
261 {
262   movement = pos - get_pos();
263   this->dir = dir;
264   sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
265   set_state(ICESTATE_GRABBED);
266   set_group(COLGROUP_DISABLED);
267 }
268
269 void
270 MrIceBlock::ungrab(MovingObject& , Direction dir)
271 {
272   this->dir = dir;
273   set_state(ICESTATE_KICKED);
274   set_group(COLGROUP_MOVING);
275 }
276
277 IMPLEMENT_FACTORY(MrIceBlock, "mriceblock")