oops forgot 2 files
[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   reader.get("stay-on-platform", stay_on_platform);
36   bbox.set_size(31.8, 31.8);
37   sprite = sprite_manager->create("mriceblock");
38   set_direction = false;
39 }
40
41 MrIceBlock::MrIceBlock(float pos_x, float pos_y, Direction d)
42   : ice_state(ICESTATE_NORMAL), squishcount(0)
43 {
44   start_position.x = pos_x;
45   start_position.y = pos_y;
46   bbox.set_size(31.8, 31.8);
47   sprite = sprite_manager->create("mriceblock");
48   set_direction = true;
49   initial_direction = d;
50 }
51
52 void
53 MrIceBlock::write(lisp::Writer& writer)
54 {
55   writer.start_list("mriceblock");
56
57   writer.write_float("x", start_position.x);
58   writer.write_float("y", start_position.y);
59
60   writer.end_list("mriceblock");
61 }
62
63 void
64 MrIceBlock::activate()
65 {
66   if (set_direction) {
67     dir = initial_direction;
68   }
69
70   physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
71   sprite->set_action(dir == LEFT ? "left" : "right");
72 }
73
74 void
75 MrIceBlock::active_update(float elapsed_time)
76 {
77   if(ice_state == ICESTATE_GRABBED)
78     return;
79
80   if(ice_state == ICESTATE_FLAT && flat_timer.check()) {
81     set_state(ICESTATE_NORMAL);
82   }
83
84   if (ice_state == ICESTATE_NORMAL &&
85       stay_on_platform &&
86       may_fall_off_platform())
87   {
88     dir = (dir == LEFT ? RIGHT : LEFT);
89     sprite->set_action(dir == LEFT ? "left" : "right");
90     physic.set_velocity_x(-physic.get_velocity_x());
91   }
92
93   BadGuy::active_update(elapsed_time);
94 }
95
96 HitResponse
97 MrIceBlock::collision_solid(GameObject& object, const CollisionHit& hit)
98 {
99   if(fabsf(hit.normal.y) > .5) { // floor or roof
100     physic.set_velocity_y(0);
101     return CONTINUE;
102   }
103   // hit left or right
104   switch(ice_state) {
105     case ICESTATE_NORMAL:
106       dir = dir == LEFT ? RIGHT : LEFT;
107       sprite->set_action(dir == LEFT ? "left" : "right");
108       physic.set_velocity_x(-physic.get_velocity_x());       
109       break;
110     case ICESTATE_KICKED: {
111       BonusBlock* bonusblock = dynamic_cast<BonusBlock*> (&object);
112       if(bonusblock) {
113         bonusblock->try_open();
114       }
115       Brick* brick = dynamic_cast<Brick*> (&object);
116       if(brick) {
117         brick->try_break();
118       }
119       
120       dir = dir == LEFT ? RIGHT : LEFT;
121       sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
122       physic.set_velocity_x(-physic.get_velocity_x());
123       sound_manager->play("sounds/iceblock_bump.wav", get_pos());
124       break;
125     }
126     case ICESTATE_FLAT:
127       physic.set_velocity_x(0);
128       break;
129     case ICESTATE_GRABBED:
130       return FORCE_MOVE;
131   }
132
133   return CONTINUE;
134 }
135
136 HitResponse
137 MrIceBlock::collision(GameObject& object, const CollisionHit& hit)
138 {
139   if(ice_state == ICESTATE_GRABBED)
140     return FORCE_MOVE;
141
142   return BadGuy::collision(object, hit);
143 }
144
145 HitResponse
146 MrIceBlock::collision_player(Player& player, const CollisionHit& hit)
147 {
148   if(ice_state == ICESTATE_GRABBED)
149     return FORCE_MOVE;
150
151   // handle kicks from left or right side
152   if(ice_state == ICESTATE_FLAT && get_state() == STATE_ACTIVE) {
153     // hit from left side
154     if(hit.normal.x > 0.7) {
155       dir = RIGHT;
156       player.kick();
157       set_state(ICESTATE_KICKED);
158       return FORCE_MOVE;
159     } else if(hit.normal.x < -0.7) {
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(fabsf(hit.normal.x) > .8) {
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   if(ice_state == ICESTATE_KICKED) {
236     bbox.set_size(31.8, 31.8);
237   }
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")