74b515dda1a6cce42f52260fb22dc4e23c02486f
[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   : ice_state(ICESTATE_NORMAL), squishcount(0)
33 {
34   reader.get("x", start_position.x);
35   reader.get("y", start_position.y);
36   bbox.set_size(31.8, 31.8);
37   sprite = sprite_manager->create("images/creatures/mr_iceblock/mr_iceblock.sprite");
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("images/creatures/mr_iceblock/mr_iceblock.sprite");
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 && might_fall(601))
85   {
86     dir = (dir == LEFT ? RIGHT : LEFT);
87     sprite->set_action(dir == LEFT ? "left" : "right");
88     physic.set_velocity_x(-physic.get_velocity_x());
89   }
90
91   BadGuy::active_update(elapsed_time);
92 }
93
94 HitResponse
95 MrIceBlock::collision_solid(GameObject& object, const CollisionHit& hit)
96 {
97   if(fabsf(hit.normal.y) > .5) { // floor or roof
98     physic.set_velocity_y(0);
99     return CONTINUE;
100   }
101   // hit left or right
102   switch(ice_state) {
103     case ICESTATE_NORMAL:
104       dir = dir == LEFT ? RIGHT : LEFT;
105       sprite->set_action(dir == LEFT ? "left" : "right");
106       physic.set_velocity_x(-physic.get_velocity_x());       
107       break;
108     case ICESTATE_KICKED: {
109       BonusBlock* bonusblock = dynamic_cast<BonusBlock*> (&object);
110       if(bonusblock) {
111         bonusblock->try_open();
112       }
113       Brick* brick = dynamic_cast<Brick*> (&object);
114       if(brick) {
115         brick->try_break();
116       }
117       
118       dir = dir == LEFT ? RIGHT : LEFT;
119       sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
120       physic.set_velocity_x(-physic.get_velocity_x());
121       sound_manager->play("sounds/iceblock_bump.wav", get_pos());
122       break;
123     }
124     case ICESTATE_FLAT:
125       physic.set_velocity_x(0);
126       break;
127     case ICESTATE_GRABBED:
128       return FORCE_MOVE;
129   }
130
131   return CONTINUE;
132 }
133
134 HitResponse
135 MrIceBlock::collision(GameObject& object, const CollisionHit& hit)
136 {
137   if(ice_state == ICESTATE_GRABBED)
138     return FORCE_MOVE;
139
140   return BadGuy::collision(object, hit);
141 }
142
143 HitResponse
144 MrIceBlock::collision_player(Player& player, const CollisionHit& hit)
145 {
146   if(ice_state == ICESTATE_GRABBED)
147     return FORCE_MOVE;
148
149   // handle kicks from left or right side
150   if(ice_state == ICESTATE_FLAT && get_state() == STATE_ACTIVE) {
151     // hit from left side
152     if(hit.normal.x > 0.7 &&
153             !player.get_controller()->hold(Controller::ACTION)) {
154       dir = RIGHT;
155       player.kick();
156       set_state(ICESTATE_KICKED);
157       return FORCE_MOVE;
158     }
159     else if(hit.normal.x < -0.7 &&
160             !player.get_controller()->hold(Controller::ACTION)) {
161       dir = LEFT;
162       player.kick();
163       set_state(ICESTATE_KICKED);
164       return FORCE_MOVE;
165     }
166   }
167   
168   return BadGuy::collision_player(player, hit);
169 }
170
171 HitResponse
172 MrIceBlock::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
173 {
174   switch(ice_state) {
175     case ICESTATE_NORMAL:
176       if(fabsf(hit.normal.x) > .8) {
177         dir = dir == LEFT ? RIGHT : LEFT;
178         sprite->set_action(dir == LEFT ? "left" : "right");
179         physic.set_velocity_x(-physic.get_velocity_x());               
180       }
181       return CONTINUE;
182     case ICESTATE_FLAT:
183       return FORCE_MOVE;
184     case ICESTATE_KICKED:
185       badguy.kill_fall();
186       return FORCE_MOVE;
187     default:
188       assert(false);
189   }
190
191   return ABORT_MOVE;
192 }
193
194 bool
195 MrIceBlock::collision_squished(Player& player)
196 {
197   switch(ice_state) {
198     case ICESTATE_KICKED:
199     case ICESTATE_NORMAL:
200       squishcount++;
201       if(squishcount >= MAXSQUISHES) {
202         kill_fall();
203         return true;
204       }
205
206       set_state(ICESTATE_FLAT);
207       break;
208     case ICESTATE_FLAT:
209       if(player.get_pos().x < get_pos().x) {
210         dir = RIGHT;
211       } else {
212         dir = LEFT;
213       }
214       set_state(ICESTATE_KICKED);
215       break;
216     case ICESTATE_GRABBED:
217       assert(false);
218       break;
219   }
220
221   player.bounce(*this);
222   return true;
223 }
224
225 void
226 MrIceBlock::set_state(IceState state)
227 {
228   if(ice_state == state)
229     return;
230   
231   if(state == ICESTATE_FLAT)
232     flags |= FLAG_PORTABLE;
233   else
234     flags &= ~FLAG_PORTABLE;
235
236   if(ice_state == ICESTATE_KICKED) {
237     bbox.set_size(31.8, 31.8);
238   }
239
240   switch(state) {
241     case ICESTATE_NORMAL:
242       physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
243       sprite->set_action(dir == LEFT ? "left" : "right");
244       break;
245     case ICESTATE_FLAT:
246       sound_manager->play("sounds/stomp.wav", get_pos());
247       physic.set_velocity_x(0);
248       physic.set_velocity_y(0); 
249       
250       sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
251       flat_timer.start(4);
252       break;
253     case ICESTATE_KICKED:
254       sound_manager->play("sounds/kick.wav", get_pos());
255
256       physic.set_velocity_x(dir == LEFT ? -KICKSPEED : KICKSPEED);
257       sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
258       // we should slide above 1 block holes now...
259       bbox.set_size(34, 31.8);
260       break;
261     case ICESTATE_GRABBED:
262       flat_timer.stop();
263       break;
264     default:
265       assert(false);
266   }
267   ice_state = state;
268 }
269
270 void
271 MrIceBlock::grab(MovingObject&, const Vector& pos, Direction dir)
272 {
273   movement = pos - get_pos();
274   this->dir = dir;
275   sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
276   set_state(ICESTATE_GRABBED);
277   set_group(COLGROUP_DISABLED);
278 }
279
280 void
281 MrIceBlock::ungrab(MovingObject& , Direction dir)
282 {
283   this->dir = dir;
284   set_state(ICESTATE_KICKED);
285   set_group(COLGROUP_MOVING);
286 }
287
288 IMPLEMENT_FACTORY(MrIceBlock, "mriceblock")