4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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.
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.
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.
22 #include "mriceblock.hpp"
23 #include "object/block.hpp"
26 const float WALKSPEED = 80;
27 const float KICKSPEED = 500;
28 const int MAXSQUISHES = 10;
31 MrIceBlock::MrIceBlock(const lisp::Lisp& reader)
32 : BadGuy(reader, "images/creatures/mr_iceblock/mr_iceblock.sprite"), ice_state(ICESTATE_NORMAL), squishcount(0)
34 sound_manager->preload("sounds/iceblock_bump.wav");
35 sound_manager->preload("sounds/stomp.wav");
36 sound_manager->preload("sounds/kick.wav");
39 MrIceBlock::MrIceBlock(const Vector& pos, Direction d)
40 : BadGuy(pos, d, "images/creatures/mr_iceblock/mr_iceblock.sprite"), ice_state(ICESTATE_NORMAL), squishcount(0)
42 sound_manager->preload("sounds/iceblock_bump.wav");
43 sound_manager->preload("sounds/stomp.wav");
44 sound_manager->preload("sounds/kick.wav");
48 MrIceBlock::write(lisp::Writer& writer)
50 writer.start_list("mriceblock");
52 writer.write_float("x", start_position.x);
53 writer.write_float("y", start_position.y);
55 writer.end_list("mriceblock");
59 MrIceBlock::activate()
61 physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
62 sprite->set_action(dir == LEFT ? "left" : "right");
63 set_state(ICESTATE_NORMAL);
67 MrIceBlock::active_update(float elapsed_time)
69 if(ice_state == ICESTATE_GRABBED)
72 if(ice_state == ICESTATE_FLAT && flat_timer.check()) {
73 set_state(ICESTATE_NORMAL);
76 if (ice_state == ICESTATE_NORMAL && might_fall(601))
78 dir = (dir == LEFT ? RIGHT : LEFT);
79 sprite->set_action(dir == LEFT ? "left" : "right");
80 physic.set_velocity_x(-physic.get_velocity_x());
83 BadGuy::active_update(elapsed_time);
87 MrIceBlock::collision_solid(const CollisionHit& hit)
89 if(hit.top || hit.bottom) { // floor or roof
90 physic.set_velocity_y(0);
97 if(hit.right && dir == RIGHT) {
99 } else if(hit.left && dir == LEFT) {
102 sprite->set_action(dir == LEFT ? "left" : "right");
103 physic.set_velocity_x(-physic.get_velocity_x());
105 case ICESTATE_KICKED: {
107 // TODO move these into bonusblock class
108 BonusBlock* bonusblock = dynamic_cast<BonusBlock*> (&object);
110 bonusblock->try_open();
112 Brick* brick = dynamic_cast<Brick*> (&object);
117 if(hit.right && dir == RIGHT) {
119 sound_manager->play("sounds/iceblock_bump.wav", get_pos());
120 physic.set_velocity_x(-KICKSPEED);
121 } else if(hit.left && dir == LEFT) {
123 sound_manager->play("sounds/iceblock_bump.wav", get_pos());
124 physic.set_velocity_x(KICKSPEED);
126 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
130 physic.set_velocity_x(0);
132 case ICESTATE_GRABBED:
138 MrIceBlock::collision(GameObject& object, const CollisionHit& hit)
140 if(ice_state == ICESTATE_GRABBED)
143 return BadGuy::collision(object, hit);
147 MrIceBlock::collision_player(Player& player, const CollisionHit& hit)
149 if(ice_state == ICESTATE_GRABBED)
152 // handle kicks from left or right side
153 if(ice_state == ICESTATE_FLAT && get_state() == STATE_ACTIVE) {
157 set_state(ICESTATE_KICKED);
159 } else if(hit.right) {
162 set_state(ICESTATE_KICKED);
167 return BadGuy::collision_player(player, hit);
171 MrIceBlock::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
174 case ICESTATE_NORMAL:
175 if(hit.left || hit.right) {
176 dir = dir == LEFT ? RIGHT : LEFT;
177 sprite->set_action(dir == LEFT ? "left" : "right");
178 physic.set_velocity_x(-physic.get_velocity_x());
183 case ICESTATE_KICKED:
194 MrIceBlock::collision_squished(Player& player)
197 case ICESTATE_KICKED:
198 case ICESTATE_NORMAL:
200 if(squishcount >= MAXSQUISHES) {
205 set_state(ICESTATE_FLAT);
208 if(player.get_pos().x < get_pos().x) {
213 set_state(ICESTATE_KICKED);
215 case ICESTATE_GRABBED:
220 player.bounce(*this);
225 MrIceBlock::set_state(IceState state)
227 if(ice_state == state)
230 if(state == ICESTATE_FLAT)
231 flags |= FLAG_PORTABLE;
233 flags &= ~FLAG_PORTABLE;
236 case ICESTATE_NORMAL:
237 physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
238 sprite->set_action(dir == LEFT ? "left" : "right");
241 sound_manager->play("sounds/stomp.wav", get_pos());
242 physic.set_velocity_x(0);
243 physic.set_velocity_y(0);
245 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
248 case ICESTATE_KICKED:
249 sound_manager->play("sounds/kick.wav", get_pos());
251 physic.set_velocity_x(dir == LEFT ? -KICKSPEED : KICKSPEED);
252 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
253 // we should slide above 1 block holes now...
254 bbox.set_size(34, 31.8);
256 case ICESTATE_GRABBED:
266 MrIceBlock::grab(MovingObject&, const Vector& pos, Direction dir)
268 movement = pos - get_pos();
270 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
271 set_state(ICESTATE_GRABBED);
272 set_group(COLGROUP_DISABLED);
276 MrIceBlock::ungrab(MovingObject& , Direction dir)
279 set_state(ICESTATE_KICKED);
280 set_group(COLGROUP_MOVING);
283 IMPLEMENT_FACTORY(MrIceBlock, "mriceblock")