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 && on_ground() && 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 update_on_ground_flag(hit);
91 if(hit.top || hit.bottom) { // floor or roof
92 physic.set_velocity_y(0);
99 if(hit.right && dir == RIGHT) {
101 sprite->set_action(dir == LEFT ? "left" : "right");
102 physic.set_velocity_x(-physic.get_velocity_x());
103 } else if(hit.left && dir == LEFT) {
105 sprite->set_action(dir == LEFT ? "left" : "right");
106 physic.set_velocity_x(-physic.get_velocity_x());
109 case ICESTATE_KICKED: {
111 // TODO move these into bonusblock class
112 BonusBlock* bonusblock = dynamic_cast<BonusBlock*> (&object);
114 bonusblock->try_open();
116 Brick* brick = dynamic_cast<Brick*> (&object);
121 if(hit.right && dir == RIGHT) {
123 sound_manager->play("sounds/iceblock_bump.wav", get_pos());
124 physic.set_velocity_x(-KICKSPEED);
125 } else if(hit.left && dir == LEFT) {
127 sound_manager->play("sounds/iceblock_bump.wav", get_pos());
128 physic.set_velocity_x(KICKSPEED);
130 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
134 physic.set_velocity_x(0);
136 case ICESTATE_GRABBED:
142 MrIceBlock::collision(GameObject& object, const CollisionHit& hit)
144 if(ice_state == ICESTATE_GRABBED)
147 return BadGuy::collision(object, hit);
151 MrIceBlock::collision_player(Player& player, const CollisionHit& hit)
153 if(ice_state == ICESTATE_GRABBED)
156 // handle kicks from left or right side
157 if(ice_state == ICESTATE_FLAT && get_state() == STATE_ACTIVE) {
161 set_state(ICESTATE_KICKED);
163 } else if(hit.right) {
166 set_state(ICESTATE_KICKED);
171 return BadGuy::collision_player(player, hit);
175 MrIceBlock::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
178 case ICESTATE_NORMAL:
179 if(hit.left || hit.right) {
180 dir = dir == LEFT ? RIGHT : LEFT;
181 sprite->set_action(dir == LEFT ? "left" : "right");
182 physic.set_velocity_x(-physic.get_velocity_x());
187 case ICESTATE_KICKED:
198 MrIceBlock::collision_squished(Player& player)
201 case ICESTATE_KICKED:
202 case ICESTATE_NORMAL:
204 if(squishcount >= MAXSQUISHES) {
209 set_state(ICESTATE_FLAT);
212 if(player.get_pos().x < get_pos().x) {
217 set_state(ICESTATE_KICKED);
219 case ICESTATE_GRABBED:
224 player.bounce(*this);
229 MrIceBlock::set_state(IceState state)
231 if(ice_state == state)
234 if(state == ICESTATE_FLAT)
235 flags |= FLAG_PORTABLE;
237 flags &= ~FLAG_PORTABLE;
240 case ICESTATE_NORMAL:
241 physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
242 sprite->set_action(dir == LEFT ? "left" : "right");
245 sound_manager->play("sounds/stomp.wav", get_pos());
246 physic.set_velocity_x(0);
247 physic.set_velocity_y(0);
249 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
252 case ICESTATE_KICKED:
253 sound_manager->play("sounds/kick.wav", get_pos());
255 physic.set_velocity_x(dir == LEFT ? -KICKSPEED : KICKSPEED);
256 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
257 // we should slide above 1 block holes now...
258 bbox.set_size(34, 31.8);
260 case ICESTATE_GRABBED:
270 MrIceBlock::grab(MovingObject&, const Vector& pos, Direction dir)
272 movement = pos - get_pos();
274 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
275 set_state(ICESTATE_GRABBED);
276 set_group(COLGROUP_DISABLED);
280 MrIceBlock::ungrab(MovingObject& , Direction dir)
283 set_state(ICESTATE_KICKED);
284 set_group(COLGROUP_MOVING);
287 IMPLEMENT_FACTORY(MrIceBlock, "mriceblock")