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"
24 #include "object/block.hpp"
25 #include "lisp/writer.hpp"
26 #include "object_factory.hpp"
27 #include "audio/sound_manager.hpp"
28 #include "object/player.hpp"
29 #include "sprite/sprite.hpp"
32 const float KICKSPEED = 500;
33 const int MAXSQUISHES = 10;
34 const float NOKICK_TIME = 0.1f;
37 MrIceBlock::MrIceBlock(const lisp::Lisp& reader)
38 : WalkingBadguy(reader, "images/creatures/mr_iceblock/mr_iceblock.sprite", "left", "right"), ice_state(ICESTATE_NORMAL), squishcount(0)
41 max_drop_height = 600;
42 sound_manager->preload("sounds/iceblock_bump.wav");
43 sound_manager->preload("sounds/stomp.wav");
44 sound_manager->preload("sounds/kick.wav");
47 MrIceBlock::MrIceBlock(const Vector& pos, Direction d)
48 : WalkingBadguy(pos, d, "images/creatures/mr_iceblock/mr_iceblock.sprite", "left", "right"), ice_state(ICESTATE_NORMAL), squishcount(0)
51 max_drop_height = 600;
52 sound_manager->preload("sounds/iceblock_bump.wav");
53 sound_manager->preload("sounds/stomp.wav");
54 sound_manager->preload("sounds/kick.wav");
58 MrIceBlock::write(lisp::Writer& writer)
60 writer.start_list("mriceblock");
61 WalkingBadguy::write(writer);
62 writer.end_list("mriceblock");
66 MrIceBlock::initialize()
68 WalkingBadguy::initialize();
69 set_state(ICESTATE_NORMAL);
73 MrIceBlock::active_update(float elapsed_time)
75 if(ice_state == ICESTATE_GRABBED)
78 if(ice_state == ICESTATE_FLAT && flat_timer.check()) {
79 set_state(ICESTATE_NORMAL);
82 if (ice_state == ICESTATE_NORMAL)
84 WalkingBadguy::active_update(elapsed_time);
88 BadGuy::active_update(elapsed_time);
92 MrIceBlock::can_break(){
93 return ice_state == ICESTATE_KICKED;
97 MrIceBlock::collision_solid(const CollisionHit& hit)
99 update_on_ground_flag(hit);
101 if(hit.top || hit.bottom) { // floor or roof
102 physic.set_velocity_y(0);
108 case ICESTATE_NORMAL:
109 WalkingBadguy::collision_solid(hit);
111 case ICESTATE_KICKED: {
112 if(hit.right && dir == RIGHT) {
114 sound_manager->play("sounds/iceblock_bump.wav", get_pos());
115 physic.set_velocity_x(-KICKSPEED);
116 } else if(hit.left && dir == LEFT) {
118 sound_manager->play("sounds/iceblock_bump.wav", get_pos());
119 physic.set_velocity_x(KICKSPEED);
121 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
125 physic.set_velocity_x(0);
127 case ICESTATE_GRABBED:
133 MrIceBlock::collision(GameObject& object, const CollisionHit& hit)
135 if(ice_state == ICESTATE_GRABBED)
138 return BadGuy::collision(object, hit);
142 MrIceBlock::collision_player(Player& player, const CollisionHit& hit)
144 if(ice_state == ICESTATE_GRABBED)
151 // handle kicks from left or right side
152 if(ice_state == ICESTATE_FLAT && get_state() == STATE_ACTIVE) {
156 set_state(ICESTATE_KICKED);
158 } else if(hit.right) {
161 set_state(ICESTATE_KICKED);
166 return BadGuy::collision_player(player, hit);
170 MrIceBlock::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
173 case ICESTATE_NORMAL:
174 return WalkingBadguy::collision_badguy(badguy, hit);
177 case ICESTATE_KICKED:
188 MrIceBlock::collision_squished(GameObject& object)
191 case ICESTATE_KICKED:
193 BadGuy* badguy = dynamic_cast<BadGuy*>(&object);
201 case ICESTATE_NORMAL:
203 Player* player = dynamic_cast<Player*>(&object);
205 if ((squishcount >= MAXSQUISHES) || (player && player->does_buttjump)) {
211 set_state(ICESTATE_FLAT);
212 nokick_timer.start(NOKICK_TIME);
216 MovingObject* movingobject = dynamic_cast<MovingObject*>(&object);
217 if (movingobject && (movingobject->get_pos().x < get_pos().x)) {
223 if (nokick_timer.check()) set_state(ICESTATE_KICKED);
225 case ICESTATE_GRABBED:
230 Player* player = dynamic_cast<Player*>(&object);
231 if (player) player->bounce(*this);
236 MrIceBlock::set_state(IceState state)
238 if(ice_state == state)
242 case ICESTATE_NORMAL:
243 WalkingBadguy::initialize();
247 physic.set_velocity_y(-KICKSPEED);
248 bbox.set_size(34, 31.8f);
250 sound_manager->play("sounds/stomp.wav", get_pos());
251 physic.set_velocity_x(0);
252 physic.set_velocity_y(0);
254 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
258 case ICESTATE_KICKED:
259 sound_manager->play("sounds/kick.wav", get_pos());
261 physic.set_velocity_x(dir == LEFT ? -KICKSPEED : KICKSPEED);
262 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
263 // we should slide above 1 block holes now...
264 bbox.set_size(34, 31.8f);
266 case ICESTATE_GRABBED:
276 MrIceBlock::grab(MovingObject&, const Vector& pos, Direction dir)
278 movement = pos - get_pos();
280 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
281 set_state(ICESTATE_GRABBED);
282 set_colgroup_active(COLGROUP_DISABLED);
286 MrIceBlock::ungrab(MovingObject& , Direction dir)
289 set_state(dir == UP ? ICESTATE_FLAT : ICESTATE_KICKED);
290 set_colgroup_active(COLGROUP_MOVING);
294 MrIceBlock::is_portable() const
296 return ice_state == ICESTATE_FLAT;
299 IMPLEMENT_FACTORY(MrIceBlock, "mriceblock")