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"
25 static const float WALKSPEED = 80;
26 static const float KICKSPEED = 500;
27 static const int MAXSQUISHES = 10;
29 MrIceBlock::MrIceBlock(const lisp::Lisp& reader)
30 : ice_state(ICESTATE_NORMAL), squishcount(0)
32 reader.get("x", start_position.x);
33 reader.get("y", start_position.y);
34 stay_on_platform = false;
35 reader.get("stay-on-platform", stay_on_platform);
36 bbox.set_size(31.8, 31.8);
37 sprite = sprite_manager->create("images/creatures/mr_iceblock/mr_iceblock.sprite");
38 set_direction = false;
41 MrIceBlock::MrIceBlock(float pos_x, float pos_y, Direction d, bool stay_on_plat = false )
42 : ice_state(ICESTATE_NORMAL), squishcount(0)
44 start_position.x = pos_x;
45 start_position.y = pos_y;
46 stay_on_platform = stay_on_plat;
47 bbox.set_size(31.8, 31.8);
48 sprite = sprite_manager->create("images/creatures/mr_iceblock/mr_iceblock.sprite");
50 initial_direction = d;
54 MrIceBlock::write(lisp::Writer& writer)
56 writer.start_list("mriceblock");
58 writer.write_float("x", start_position.x);
59 writer.write_float("y", start_position.y);
60 writer.write_bool("stay-on-platform", stay_on_platform);
62 writer.end_list("mriceblock");
66 MrIceBlock::activate()
69 dir = initial_direction;
72 physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
73 sprite->set_action(dir == LEFT ? "left" : "right");
77 MrIceBlock::active_update(float elapsed_time)
79 if(ice_state == ICESTATE_GRABBED)
82 if(ice_state == ICESTATE_FLAT && flat_timer.check()) {
83 set_state(ICESTATE_NORMAL);
86 if (ice_state == ICESTATE_NORMAL &&
88 may_fall_off_platform())
90 dir = (dir == LEFT ? RIGHT : LEFT);
91 sprite->set_action(dir == LEFT ? "left" : "right");
92 physic.set_velocity_x(-physic.get_velocity_x());
95 BadGuy::active_update(elapsed_time);
99 MrIceBlock::collision_solid(GameObject& object, const CollisionHit& hit)
101 if(fabsf(hit.normal.y) > .5) { // floor or roof
102 physic.set_velocity_y(0);
107 case ICESTATE_NORMAL:
108 dir = dir == LEFT ? RIGHT : LEFT;
109 sprite->set_action(dir == LEFT ? "left" : "right");
110 physic.set_velocity_x(-physic.get_velocity_x());
112 case ICESTATE_KICKED: {
113 BonusBlock* bonusblock = dynamic_cast<BonusBlock*> (&object);
115 bonusblock->try_open();
117 Brick* brick = dynamic_cast<Brick*> (&object);
122 dir = dir == LEFT ? RIGHT : LEFT;
123 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
124 physic.set_velocity_x(-physic.get_velocity_x());
125 sound_manager->play("sounds/iceblock_bump.wav", get_pos());
129 physic.set_velocity_x(0);
131 case ICESTATE_GRABBED:
139 MrIceBlock::collision(GameObject& object, const CollisionHit& hit)
141 if(ice_state == ICESTATE_GRABBED)
144 return BadGuy::collision(object, hit);
148 MrIceBlock::collision_player(Player& player, const CollisionHit& hit)
150 if(ice_state == ICESTATE_GRABBED)
153 // handle kicks from left or right side
154 if(ice_state == ICESTATE_FLAT && get_state() == STATE_ACTIVE) {
155 // hit from left side
156 if(hit.normal.x > 0.7) {
159 set_state(ICESTATE_KICKED);
161 } else if(hit.normal.x < -0.7) {
164 set_state(ICESTATE_KICKED);
169 return BadGuy::collision_player(player, hit);
173 MrIceBlock::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
176 case ICESTATE_NORMAL:
177 if(fabsf(hit.normal.x) > .8) {
178 dir = dir == LEFT ? RIGHT : LEFT;
179 sprite->set_action(dir == LEFT ? "left" : "right");
180 physic.set_velocity_x(-physic.get_velocity_x());
185 case ICESTATE_KICKED:
196 MrIceBlock::collision_squished(Player& player)
199 case ICESTATE_KICKED:
200 case ICESTATE_NORMAL:
202 if(squishcount >= MAXSQUISHES) {
207 set_state(ICESTATE_FLAT);
210 if(player.get_pos().x < get_pos().x) {
215 set_state(ICESTATE_KICKED);
217 case ICESTATE_GRABBED:
222 player.bounce(*this);
227 MrIceBlock::set_state(IceState state)
229 if(ice_state == state)
232 if(state == ICESTATE_FLAT)
233 flags |= FLAG_PORTABLE;
235 flags &= ~FLAG_PORTABLE;
237 if(ice_state == ICESTATE_KICKED) {
238 bbox.set_size(31.8, 31.8);
242 case ICESTATE_NORMAL:
243 physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
244 sprite->set_action(dir == LEFT ? "left" : "right");
247 sound_manager->play("sounds/stomp.wav", get_pos());
248 physic.set_velocity_x(0);
249 physic.set_velocity_y(0);
251 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
254 case ICESTATE_KICKED:
255 sound_manager->play("sounds/kick.wav", get_pos());
257 physic.set_velocity_x(dir == LEFT ? -KICKSPEED : KICKSPEED);
258 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
259 // we should slide above 1 block holes now...
260 bbox.set_size(34, 31.8);
262 case ICESTATE_GRABBED:
272 MrIceBlock::grab(MovingObject&, const Vector& pos, Direction dir)
274 movement = pos - get_pos();
276 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
277 set_state(ICESTATE_GRABBED);
278 set_group(COLGROUP_DISABLED);
282 MrIceBlock::ungrab(MovingObject& , Direction dir)
285 set_state(ICESTATE_KICKED);
286 set_group(COLGROUP_MOVING);
289 IMPLEMENT_FACTORY(MrIceBlock, "mriceblock")