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 set_direction = false;
37 MrIceBlock::MrIceBlock(const Vector& pos, Direction d)
38 : BadGuy(pos, "images/creatures/mr_iceblock/mr_iceblock.sprite"), ice_state(ICESTATE_NORMAL), squishcount(0)
41 initial_direction = d;
45 MrIceBlock::write(lisp::Writer& writer)
47 writer.start_list("mriceblock");
49 writer.write_float("x", start_position.x);
50 writer.write_float("y", start_position.y);
52 writer.end_list("mriceblock");
56 MrIceBlock::activate()
59 dir = initial_direction;
62 physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
63 sprite->set_action(dir == LEFT ? "left" : "right");
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(GameObject& object, const CollisionHit& hit)
89 if(fabsf(hit.normal.y) > .5) { // floor or roof
90 physic.set_velocity_y(0);
96 dir = dir == LEFT ? RIGHT : LEFT;
97 sprite->set_action(dir == LEFT ? "left" : "right");
98 physic.set_velocity_x(-physic.get_velocity_x());
100 case ICESTATE_KICKED: {
101 BonusBlock* bonusblock = dynamic_cast<BonusBlock*> (&object);
103 bonusblock->try_open();
105 Brick* brick = dynamic_cast<Brick*> (&object);
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());
117 physic.set_velocity_x(0);
119 case ICESTATE_GRABBED:
127 MrIceBlock::collision(GameObject& object, const CollisionHit& hit)
129 if(ice_state == ICESTATE_GRABBED)
132 return BadGuy::collision(object, hit);
136 MrIceBlock::collision_player(Player& player, const CollisionHit& hit)
138 if(ice_state == ICESTATE_GRABBED)
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) {
147 set_state(ICESTATE_KICKED);
149 } else if(hit.normal.x < -0.7) {
152 set_state(ICESTATE_KICKED);
157 return BadGuy::collision_player(player, hit);
161 MrIceBlock::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
164 case ICESTATE_NORMAL:
165 if(fabsf(hit.normal.x) > .8) {
166 dir = dir == LEFT ? RIGHT : LEFT;
167 sprite->set_action(dir == LEFT ? "left" : "right");
168 physic.set_velocity_x(-physic.get_velocity_x());
173 case ICESTATE_KICKED:
184 MrIceBlock::collision_squished(Player& player)
187 case ICESTATE_KICKED:
188 case ICESTATE_NORMAL:
190 if(squishcount >= MAXSQUISHES) {
195 set_state(ICESTATE_FLAT);
198 if(player.get_pos().x < get_pos().x) {
203 set_state(ICESTATE_KICKED);
205 case ICESTATE_GRABBED:
210 player.bounce(*this);
215 MrIceBlock::set_state(IceState state)
217 if(ice_state == state)
220 if(state == ICESTATE_FLAT)
221 flags |= FLAG_PORTABLE;
223 flags &= ~FLAG_PORTABLE;
226 case ICESTATE_NORMAL:
227 physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
228 sprite->set_action(dir == LEFT ? "left" : "right");
231 sound_manager->play("sounds/stomp.wav", get_pos());
232 physic.set_velocity_x(0);
233 physic.set_velocity_y(0);
235 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
238 case ICESTATE_KICKED:
239 sound_manager->play("sounds/kick.wav", get_pos());
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.8);
246 case ICESTATE_GRABBED:
256 MrIceBlock::grab(MovingObject&, const Vector& pos, Direction dir)
258 movement = pos - get_pos();
260 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
261 set_state(ICESTATE_GRABBED);
262 set_group(COLGROUP_DISABLED);
266 MrIceBlock::ungrab(MovingObject& , Direction dir)
269 set_state(ICESTATE_KICKED);
270 set_group(COLGROUP_MOVING);
273 IMPLEMENT_FACTORY(MrIceBlock, "mriceblock")