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