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