4 // Copyright (C) 2005 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
23 #include "mriceblock.hpp"
24 #include "object/block.hpp"
26 static const float WALKSPEED = 80;
27 static const float KICKSPEED = 500;
28 static const int MAXSQUISHES = 10;
30 MrIceBlock::MrIceBlock(const lisp::Lisp& reader)
31 : ice_state(ICESTATE_NORMAL), squishcount(0)
33 reader.get("x", start_position.x);
34 reader.get("y", start_position.y);
35 reader.get("stay-on-platform", stay_on_platform);
36 bbox.set_size(31.8, 31.8);
37 sprite = sprite_manager->create("mriceblock");
38 set_direction = false;
41 MrIceBlock::MrIceBlock(float pos_x, float pos_y, Direction d)
42 : ice_state(ICESTATE_NORMAL), squishcount(0)
44 start_position.x = pos_x;
45 start_position.y = pos_y;
46 bbox.set_size(31.8, 31.8);
47 sprite = sprite_manager->create("mriceblock");
49 initial_direction = d;
53 MrIceBlock::write(lisp::Writer& writer)
55 writer.start_list("mriceblock");
57 writer.write_float("x", start_position.x);
58 writer.write_float("y", start_position.y);
60 writer.end_list("mriceblock");
64 MrIceBlock::activate()
67 dir = initial_direction;
70 physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
71 sprite->set_action(dir == LEFT ? "left" : "right");
75 MrIceBlock::active_update(float elapsed_time)
77 if(ice_state == ICESTATE_GRABBED)
80 if(ice_state == ICESTATE_FLAT && flat_timer.check()) {
81 set_state(ICESTATE_NORMAL);
84 if (ice_state == ICESTATE_NORMAL &&
86 may_fall_off_platform())
88 dir = (dir == LEFT ? RIGHT : LEFT);
89 sprite->set_action(dir == LEFT ? "left" : "right");
90 physic.set_velocity_x(-physic.get_velocity_x());
93 BadGuy::active_update(elapsed_time);
97 MrIceBlock::collision_solid(GameObject& object, const CollisionHit& hit)
99 if(fabsf(hit.normal.y) > .5) { // floor or roof
100 physic.set_velocity_y(0);
105 case ICESTATE_NORMAL:
106 dir = dir == LEFT ? RIGHT : LEFT;
107 sprite->set_action(dir == LEFT ? "left" : "right");
108 physic.set_velocity_x(-physic.get_velocity_x());
110 case ICESTATE_KICKED: {
111 BonusBlock* bonusblock = dynamic_cast<BonusBlock*> (&object);
113 bonusblock->try_open();
115 Brick* brick = dynamic_cast<Brick*> (&object);
120 dir = dir == LEFT ? RIGHT : LEFT;
121 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
122 physic.set_velocity_x(-physic.get_velocity_x());
123 sound_manager->play("sounds/iceblock_bump.wav", get_pos());
127 physic.set_velocity_x(0);
129 case ICESTATE_GRABBED:
137 MrIceBlock::collision(GameObject& object, const CollisionHit& hit)
139 if(ice_state == ICESTATE_GRABBED)
142 return BadGuy::collision(object, hit);
146 MrIceBlock::collision_player(Player& player, const CollisionHit& hit)
148 if(ice_state == ICESTATE_GRABBED)
151 // handle kicks from left or right side
152 if(ice_state == ICESTATE_FLAT && get_state() == STATE_ACTIVE) {
153 // hit from left side
154 if(hit.normal.x > 0.7) {
157 set_state(ICESTATE_KICKED);
159 } else if(hit.normal.x < -0.7) {
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(fabsf(hit.normal.x) > .8) {
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;
235 if(ice_state == ICESTATE_KICKED) {
236 bbox.set_size(31.8, 31.8);
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")