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