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