Experimental feature: Allow Tux to throw stuff straight up
[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 bool
85 MrIceBlock::can_break(){
86     return ice_state == ICESTATE_KICKED;
87 }
88
89 void
90 MrIceBlock::collision_solid(const CollisionHit& hit)
91 {
92   update_on_ground_flag(hit);
93
94   if(hit.top || hit.bottom) { // floor or roof
95     physic.set_velocity_y(0);
96     return;
97   }
98
99   // hit left or right
100   switch(ice_state) {
101     case ICESTATE_NORMAL:
102       WalkingBadguy::collision_solid(hit);
103       break;
104     case ICESTATE_KICKED: {
105       if(hit.right && dir == RIGHT) {
106         dir = LEFT;
107         sound_manager->play("sounds/iceblock_bump.wav", get_pos());
108         physic.set_velocity_x(-KICKSPEED);
109       } else if(hit.left && dir == LEFT) {
110         dir = RIGHT;
111         sound_manager->play("sounds/iceblock_bump.wav", get_pos());
112         physic.set_velocity_x(KICKSPEED);
113       }
114       sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
115       break;
116     }
117     case ICESTATE_FLAT:
118       physic.set_velocity_x(0);
119       break;
120     case ICESTATE_GRABBED:
121       break;
122   }
123 }
124
125 HitResponse
126 MrIceBlock::collision(GameObject& object, const CollisionHit& hit)
127 {
128   if(ice_state == ICESTATE_GRABBED)
129     return FORCE_MOVE;
130
131   return BadGuy::collision(object, hit);
132 }
133
134 HitResponse
135 MrIceBlock::collision_player(Player& player, const CollisionHit& hit)
136 {
137   if(ice_state == ICESTATE_GRABBED)
138     return FORCE_MOVE;
139
140   if(dir == UP) {
141     return FORCE_MOVE;
142   }
143
144   // handle kicks from left or right side
145   if(ice_state == ICESTATE_FLAT && get_state() == STATE_ACTIVE) {
146     if(hit.left) {
147       dir = RIGHT;
148       player.kick();
149       set_state(ICESTATE_KICKED);
150       return FORCE_MOVE;
151     } else if(hit.right) {
152       dir = LEFT;
153       player.kick();
154       set_state(ICESTATE_KICKED);
155       return FORCE_MOVE;
156     }
157   }
158
159   return BadGuy::collision_player(player, hit);
160 }
161
162 HitResponse
163 MrIceBlock::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
164 {
165   switch(ice_state) {
166     case ICESTATE_NORMAL:
167       return WalkingBadguy::collision_badguy(badguy, hit);
168     case ICESTATE_FLAT:
169       return FORCE_MOVE;
170     case ICESTATE_KICKED:
171       badguy.kill_fall();
172       return FORCE_MOVE;
173     default:
174       assert(false);
175   }
176
177   return ABORT_MOVE;
178 }
179
180 bool
181 MrIceBlock::collision_squished(GameObject& object)
182 {
183   switch(ice_state) {
184     case ICESTATE_KICKED:
185     case ICESTATE_NORMAL:
186       squishcount++;
187       if(squishcount >= MAXSQUISHES) {
188         kill_fall();
189         return true;
190       }
191
192       set_state(ICESTATE_FLAT);
193       break;
194     case ICESTATE_FLAT:
195       {
196         MovingObject* movingobject = dynamic_cast<MovingObject*>(&object);
197         if (movingobject && (movingobject->get_pos().x < get_pos().x)) {
198           dir = RIGHT;
199         } else {
200           dir = LEFT;
201         }
202       }
203       set_state(ICESTATE_KICKED);
204       break;
205     case ICESTATE_GRABBED:
206       assert(false);
207       break;
208   }
209
210   Player* player = dynamic_cast<Player*>(&object);
211   if (player) 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   switch(state) {
222     case ICESTATE_NORMAL:
223       WalkingBadguy::activate();
224       break;
225     case ICESTATE_FLAT:
226       if(dir == UP) {
227         physic.set_velocity_y(-KICKSPEED);
228         bbox.set_size(34, 31.8f);
229       } else {
230         sound_manager->play("sounds/stomp.wav", get_pos());
231         physic.set_velocity_x(0);
232         physic.set_velocity_y(0);
233
234         sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
235       }
236       flat_timer.start(4);
237       break;
238     case ICESTATE_KICKED:
239       sound_manager->play("sounds/kick.wav", get_pos());
240
241       physic.set_velocity_x(dir == LEFT ? -KICKSPEED : KICKSPEED);
242       sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
243       // we should slide above 1 block holes now...
244       bbox.set_size(34, 31.8f);
245       break;
246     case ICESTATE_GRABBED:
247       flat_timer.stop();
248       break;
249     default:
250       assert(false);
251   }
252   ice_state = state;
253 }
254
255 void
256 MrIceBlock::grab(MovingObject&, const Vector& pos, Direction dir)
257 {
258   movement = pos - get_pos();
259   this->dir = dir;
260   sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
261   set_state(ICESTATE_GRABBED);
262   set_group(COLGROUP_DISABLED);
263 }
264
265 void
266 MrIceBlock::ungrab(MovingObject& , Direction dir)
267 {
268   this->dir = dir;
269   set_state(dir == UP ? ICESTATE_FLAT : ICESTATE_KICKED);
270   set_group(COLGROUP_MOVING);
271 }
272
273 bool
274 MrIceBlock::is_portable() const
275 {
276   return ice_state == ICESTATE_FLAT;
277 }
278
279 IMPLEMENT_FACTORY(MrIceBlock, "mriceblock")