Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / badguy / mriceblock.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "badguy/mriceblock.hpp"
18
19 #include "audio/sound_manager.hpp"
20 #include "object/player.hpp"
21 #include "sprite/sprite.hpp"
22 #include "supertux/object_factory.hpp"
23
24 namespace {
25 const float KICKSPEED = 500;
26 const int MAXSQUISHES = 10;
27 const float NOKICK_TIME = 0.1f;
28 }
29
30 MrIceBlock::MrIceBlock(const Reader& reader) :
31   WalkingBadguy(reader, "images/creatures/mr_iceblock/mr_iceblock.sprite", "left", "right"), 
32   ice_state(ICESTATE_NORMAL), 
33   nokick_timer(),
34   flat_timer(),
35   squishcount(0)
36 {
37   walk_speed = 80;
38   max_drop_height = 600;
39   sound_manager->preload("sounds/iceblock_bump.wav");
40   sound_manager->preload("sounds/stomp.wav");
41   sound_manager->preload("sounds/kick.wav");
42 }
43
44 MrIceBlock::MrIceBlock(const Vector& pos, Direction d) :
45   WalkingBadguy(pos, d, "images/creatures/mr_iceblock/mr_iceblock.sprite", "left", "right"), 
46   ice_state(ICESTATE_NORMAL), 
47   nokick_timer(),
48   flat_timer(),
49   squishcount(0)
50 {
51   walk_speed = 80;
52   max_drop_height = 600;
53   sound_manager->preload("sounds/iceblock_bump.wav");
54   sound_manager->preload("sounds/stomp.wav");
55   sound_manager->preload("sounds/kick.wav");
56 }
57
58 void
59 MrIceBlock::initialize()
60 {
61   WalkingBadguy::initialize();
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     {
186       BadGuy* badguy = dynamic_cast<BadGuy*>(&object);
187       if (badguy) {
188         badguy->kill_fall();
189         break;
190       }
191     }
192
193     // fall through
194     case ICESTATE_NORMAL:
195     {
196       Player* player = dynamic_cast<Player*>(&object);
197       squishcount++;
198       if ((squishcount >= MAXSQUISHES) || (player && player->does_buttjump)) {
199         kill_fall();
200         return true;
201       }
202     }
203
204     set_state(ICESTATE_FLAT);
205     nokick_timer.start(NOKICK_TIME);
206     break;
207     case ICESTATE_FLAT:
208     {
209       MovingObject* movingobject = dynamic_cast<MovingObject*>(&object);
210       if (movingobject && (movingobject->get_pos().x < get_pos().x)) {
211         dir = RIGHT;
212       } else {
213         dir = LEFT;
214       }
215     }
216     if (nokick_timer.check()) set_state(ICESTATE_KICKED);
217     break;
218     case ICESTATE_GRABBED:
219       assert(false);
220       break;
221   }
222
223   Player* player = dynamic_cast<Player*>(&object);
224   if (player) player->bounce(*this);
225   return true;
226 }
227
228 void
229 MrIceBlock::set_state(IceState state)
230 {
231   if(ice_state == state)
232     return;
233
234   switch(state) {
235     case ICESTATE_NORMAL:
236       WalkingBadguy::initialize();
237       break;
238     case ICESTATE_FLAT:
239       if(dir == UP) {
240         physic.set_velocity_y(-KICKSPEED);
241         bbox.set_size(34, 31.8f);
242       } else {
243         sound_manager->play("sounds/stomp.wav", get_pos());
244         physic.set_velocity_x(0);
245         physic.set_velocity_y(0);
246
247         sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
248       }
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.8f);
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_colgroup_active(COLGROUP_DISABLED);
276 }
277
278 void
279 MrIceBlock::ungrab(MovingObject& , Direction dir)
280 {
281   this->dir = dir;
282   set_state(dir == UP ? ICESTATE_FLAT : ICESTATE_KICKED);
283   set_colgroup_active(COLGROUP_MOVING);
284 }
285
286 bool
287 MrIceBlock::is_portable() const
288 {
289   return ice_state == ICESTATE_FLAT;
290 }
291
292 IMPLEMENT_FACTORY(MrIceBlock, "mriceblock");
293
294 /* EOF */