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 stay_on_platform = false;
36 reader.get("stay-on-platform", stay_on_platform);
37 bbox.set_size(31.8, 31.8);
38 sprite = sprite_manager->create("images/creatures/mr_iceblock/mr_iceblock.sprite");
39 set_direction = false;
42 MrIceBlock::MrIceBlock(float pos_x, float pos_y, Direction d, bool stay_on_plat = false )
43 : ice_state(ICESTATE_NORMAL), squishcount(0)
45 start_position.x = pos_x;
46 start_position.y = pos_y;
47 stay_on_platform = stay_on_plat;
48 bbox.set_size(31.8, 31.8);
49 sprite = sprite_manager->create("images/creatures/mr_iceblock/mr_iceblock.sprite");
51 initial_direction = d;
55 MrIceBlock::write(lisp::Writer& writer)
57 writer.start_list("mriceblock");
59 writer.write_float("x", start_position.x);
60 writer.write_float("y", start_position.y);
61 writer.write_bool("stay-on-platform", stay_on_platform);
63 writer.end_list("mriceblock");
67 MrIceBlock::activate()
70 dir = initial_direction;
73 physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
74 sprite->set_action(dir == LEFT ? "left" : "right");
78 MrIceBlock::active_update(float elapsed_time)
80 if(ice_state == ICESTATE_GRABBED)
83 if(ice_state == ICESTATE_FLAT && flat_timer.check()) {
84 set_state(ICESTATE_NORMAL);
87 if (ice_state == ICESTATE_NORMAL &&
89 may_fall_off_platform())
91 dir = (dir == LEFT ? RIGHT : LEFT);
92 sprite->set_action(dir == LEFT ? "left" : "right");
93 physic.set_velocity_x(-physic.get_velocity_x());
96 BadGuy::active_update(elapsed_time);
100 MrIceBlock::collision_solid(GameObject& object, const CollisionHit& hit)
102 if(fabsf(hit.normal.y) > .5) { // floor or roof
103 physic.set_velocity_y(0);
108 case ICESTATE_NORMAL:
109 dir = dir == LEFT ? RIGHT : LEFT;
110 sprite->set_action(dir == LEFT ? "left" : "right");
111 physic.set_velocity_x(-physic.get_velocity_x());
113 case ICESTATE_KICKED: {
114 BonusBlock* bonusblock = dynamic_cast<BonusBlock*> (&object);
116 bonusblock->try_open();
118 Brick* brick = dynamic_cast<Brick*> (&object);
123 dir = dir == LEFT ? RIGHT : LEFT;
124 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
125 physic.set_velocity_x(-physic.get_velocity_x());
126 sound_manager->play("sounds/iceblock_bump.wav", get_pos());
130 physic.set_velocity_x(0);
132 case ICESTATE_GRABBED:
140 MrIceBlock::collision(GameObject& object, const CollisionHit& hit)
142 if(ice_state == ICESTATE_GRABBED)
145 return BadGuy::collision(object, hit);
149 MrIceBlock::collision_player(Player& player, const CollisionHit& hit)
151 if(ice_state == ICESTATE_GRABBED)
154 // handle kicks from left or right side
155 if(ice_state == ICESTATE_FLAT && get_state() == STATE_ACTIVE) {
156 // hit from left side
157 if(hit.normal.x > 0.7) {
160 set_state(ICESTATE_KICKED);
162 } else if(hit.normal.x < -0.7) {
165 set_state(ICESTATE_KICKED);
170 return BadGuy::collision_player(player, hit);
174 MrIceBlock::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
177 case ICESTATE_NORMAL:
178 if(fabsf(hit.normal.x) > .8) {
179 dir = dir == LEFT ? RIGHT : LEFT;
180 sprite->set_action(dir == LEFT ? "left" : "right");
181 physic.set_velocity_x(-physic.get_velocity_x());
186 case ICESTATE_KICKED:
197 MrIceBlock::collision_squished(Player& player)
200 case ICESTATE_KICKED:
201 case ICESTATE_NORMAL:
203 if(squishcount >= MAXSQUISHES) {
208 set_state(ICESTATE_FLAT);
211 if(player.get_pos().x < get_pos().x) {
216 set_state(ICESTATE_KICKED);
218 case ICESTATE_GRABBED:
223 player.bounce(*this);
228 MrIceBlock::set_state(IceState state)
230 if(ice_state == state)
233 if(state == ICESTATE_FLAT)
234 flags |= FLAG_PORTABLE;
236 flags &= ~FLAG_PORTABLE;
238 if(ice_state == ICESTATE_KICKED) {
239 bbox.set_size(31.8, 31.8);
243 case ICESTATE_NORMAL:
244 physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
245 sprite->set_action(dir == LEFT ? "left" : "right");
248 sound_manager->play("sounds/stomp.wav", get_pos());
249 physic.set_velocity_x(0);
250 physic.set_velocity_y(0);
252 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
255 case ICESTATE_KICKED:
256 sound_manager->play("sounds/kick.wav", get_pos());
258 physic.set_velocity_x(dir == LEFT ? -KICKSPEED : KICKSPEED);
259 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
260 // we should slide above 1 block holes now...
261 bbox.set_size(34, 31.8);
263 case ICESTATE_GRABBED:
273 MrIceBlock::grab(MovingObject&, const Vector& pos, Direction dir)
275 movement = pos - get_pos();
277 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
278 set_state(ICESTATE_GRABBED);
279 set_group(COLGROUP_DISABLED);
283 MrIceBlock::ungrab(MovingObject& , Direction dir)
286 set_state(ICESTATE_KICKED);
287 set_group(COLGROUP_MOVING);
290 IMPLEMENT_FACTORY(MrIceBlock, "mriceblock")