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 KICKSPEED = 500;
27 const int MAXSQUISHES = 10;
28 const float NOKICK_TIME = 0.1f;
31 MrIceBlock::MrIceBlock(const lisp::Lisp& reader)
32 : WalkingBadguy(reader, "images/creatures/mr_iceblock/mr_iceblock.sprite", "left", "right"), ice_state(ICESTATE_NORMAL), squishcount(0)
35 max_drop_height = 600;
36 sound_manager->preload("sounds/iceblock_bump.wav");
37 sound_manager->preload("sounds/stomp.wav");
38 sound_manager->preload("sounds/kick.wav");
41 MrIceBlock::MrIceBlock(const Vector& pos, Direction d)
42 : WalkingBadguy(pos, d, "images/creatures/mr_iceblock/mr_iceblock.sprite", "left", "right"), ice_state(ICESTATE_NORMAL), squishcount(0)
45 max_drop_height = 600;
46 sound_manager->preload("sounds/iceblock_bump.wav");
47 sound_manager->preload("sounds/stomp.wav");
48 sound_manager->preload("sounds/kick.wav");
52 MrIceBlock::write(lisp::Writer& writer)
54 writer.start_list("mriceblock");
55 WalkingBadguy::write(writer);
56 writer.end_list("mriceblock");
60 MrIceBlock::initialize()
62 WalkingBadguy::initialize();
63 set_state(ICESTATE_NORMAL);
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)
78 WalkingBadguy::active_update(elapsed_time);
82 BadGuy::active_update(elapsed_time);
86 MrIceBlock::can_break(){
87 return ice_state == ICESTATE_KICKED;
91 MrIceBlock::collision_solid(const CollisionHit& hit)
93 update_on_ground_flag(hit);
95 if(hit.top || hit.bottom) { // floor or roof
96 physic.set_velocity_y(0);
102 case ICESTATE_NORMAL:
103 WalkingBadguy::collision_solid(hit);
105 case ICESTATE_KICKED: {
106 if(hit.right && dir == RIGHT) {
108 sound_manager->play("sounds/iceblock_bump.wav", get_pos());
109 if(++squishcount >= MAXSQUISHES) { kill_fall(); break; }
110 physic.set_velocity_x(-KICKSPEED);
111 } else if(hit.left && dir == LEFT) {
113 sound_manager->play("sounds/iceblock_bump.wav", get_pos());
114 if(++squishcount >= MAXSQUISHES) { kill_fall(); break; }
115 physic.set_velocity_x(KICKSPEED);
117 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
121 physic.set_velocity_x(0);
123 case ICESTATE_GRABBED:
129 MrIceBlock::collision(GameObject& object, const CollisionHit& hit)
131 if(ice_state == ICESTATE_GRABBED)
134 return BadGuy::collision(object, hit);
138 MrIceBlock::collision_player(Player& player, const CollisionHit& hit)
140 if(ice_state == ICESTATE_GRABBED)
147 // handle kicks from left or right side
148 if(ice_state == ICESTATE_FLAT && get_state() == STATE_ACTIVE) {
152 set_state(ICESTATE_KICKED);
154 } else if(hit.right) {
157 set_state(ICESTATE_KICKED);
162 return BadGuy::collision_player(player, hit);
166 MrIceBlock::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
169 case ICESTATE_NORMAL:
170 return WalkingBadguy::collision_badguy(badguy, hit);
173 case ICESTATE_KICKED:
184 MrIceBlock::collision_squished(GameObject& object)
187 case ICESTATE_KICKED:
188 case ICESTATE_NORMAL:
190 Player* player = dynamic_cast<Player*>(&object);
192 if ((squishcount >= MAXSQUISHES) || (player && player->does_buttjump)) {
198 set_state(ICESTATE_FLAT);
199 nokick_timer.start(NOKICK_TIME);
203 MovingObject* movingobject = dynamic_cast<MovingObject*>(&object);
204 if (movingobject && (movingobject->get_pos().x < get_pos().x)) {
210 if (nokick_timer.check()) set_state(ICESTATE_KICKED);
212 case ICESTATE_GRABBED:
217 Player* player = dynamic_cast<Player*>(&object);
218 if (player) player->bounce(*this);
223 MrIceBlock::set_state(IceState state)
225 if(ice_state == state)
229 case ICESTATE_NORMAL:
230 WalkingBadguy::initialize();
234 physic.set_velocity_y(-KICKSPEED);
235 bbox.set_size(34, 31.8f);
237 sound_manager->play("sounds/stomp.wav", get_pos());
238 physic.set_velocity_x(0);
239 physic.set_velocity_y(0);
241 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
245 case ICESTATE_KICKED:
246 sound_manager->play("sounds/kick.wav", get_pos());
248 physic.set_velocity_x(dir == LEFT ? -KICKSPEED : KICKSPEED);
249 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
250 // we should slide above 1 block holes now...
251 bbox.set_size(34, 31.8f);
253 case ICESTATE_GRABBED:
263 MrIceBlock::grab(MovingObject&, const Vector& pos, Direction dir)
265 movement = pos - get_pos();
267 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
268 set_state(ICESTATE_GRABBED);
269 set_colgroup_active(COLGROUP_DISABLED);
273 MrIceBlock::ungrab(MovingObject& , Direction dir)
276 set_state(dir == UP ? ICESTATE_FLAT : ICESTATE_KICKED);
277 set_colgroup_active(COLGROUP_MOVING);
281 MrIceBlock::is_portable() const
283 return ice_state == ICESTATE_FLAT;
286 IMPLEMENT_FACTORY(MrIceBlock, "mriceblock")