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;
30 MrIceBlock::MrIceBlock(const lisp::Lisp& reader)
31 : WalkingBadguy(reader, "images/creatures/mr_iceblock/mr_iceblock.sprite", "left", "right"), ice_state(ICESTATE_NORMAL), squishcount(0)
34 max_drop_height = 600;
35 sound_manager->preload("sounds/iceblock_bump.wav");
36 sound_manager->preload("sounds/stomp.wav");
37 sound_manager->preload("sounds/kick.wav");
40 MrIceBlock::MrIceBlock(const Vector& pos, Direction d)
41 : WalkingBadguy(pos, d, "images/creatures/mr_iceblock/mr_iceblock.sprite", "left", "right"), ice_state(ICESTATE_NORMAL), squishcount(0)
44 max_drop_height = 600;
45 sound_manager->preload("sounds/iceblock_bump.wav");
46 sound_manager->preload("sounds/stomp.wav");
47 sound_manager->preload("sounds/kick.wav");
51 MrIceBlock::write(lisp::Writer& writer)
53 writer.start_list("mriceblock");
54 WalkingBadguy::write(writer);
55 writer.end_list("mriceblock");
59 MrIceBlock::activate()
61 WalkingBadguy::activate();
62 set_state(ICESTATE_NORMAL);
66 MrIceBlock::active_update(float elapsed_time)
68 if(ice_state == ICESTATE_GRABBED)
71 if(ice_state == ICESTATE_FLAT && flat_timer.check()) {
72 set_state(ICESTATE_NORMAL);
75 if (ice_state == ICESTATE_NORMAL)
77 WalkingBadguy::active_update(elapsed_time);
81 BadGuy::active_update(elapsed_time);
85 MrIceBlock::can_break(){
86 return ice_state == ICESTATE_KICKED;
90 MrIceBlock::collision_solid(const CollisionHit& hit)
92 update_on_ground_flag(hit);
94 if(hit.top || hit.bottom) { // floor or roof
95 physic.set_velocity_y(0);
101 case ICESTATE_NORMAL:
102 WalkingBadguy::collision_solid(hit);
104 case ICESTATE_KICKED: {
105 if(hit.right && dir == RIGHT) {
107 sound_manager->play("sounds/iceblock_bump.wav", get_pos());
108 physic.set_velocity_x(-KICKSPEED);
109 } else if(hit.left && dir == LEFT) {
111 sound_manager->play("sounds/iceblock_bump.wav", get_pos());
112 physic.set_velocity_x(KICKSPEED);
114 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
118 physic.set_velocity_x(0);
120 case ICESTATE_GRABBED:
126 MrIceBlock::collision(GameObject& object, const CollisionHit& hit)
128 if(ice_state == ICESTATE_GRABBED)
131 return BadGuy::collision(object, hit);
135 MrIceBlock::collision_player(Player& player, const CollisionHit& hit)
137 if(ice_state == ICESTATE_GRABBED)
144 // handle kicks from left or right side
145 if(ice_state == ICESTATE_FLAT && get_state() == STATE_ACTIVE) {
149 set_state(ICESTATE_KICKED);
151 } else if(hit.right) {
154 set_state(ICESTATE_KICKED);
159 return BadGuy::collision_player(player, hit);
163 MrIceBlock::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
166 case ICESTATE_NORMAL:
167 return WalkingBadguy::collision_badguy(badguy, hit);
170 case ICESTATE_KICKED:
181 MrIceBlock::collision_squished(GameObject& object)
184 case ICESTATE_KICKED:
185 case ICESTATE_NORMAL:
187 if(squishcount >= MAXSQUISHES) {
192 set_state(ICESTATE_FLAT);
196 MovingObject* movingobject = dynamic_cast<MovingObject*>(&object);
197 if (movingobject && (movingobject->get_pos().x < get_pos().x)) {
203 set_state(ICESTATE_KICKED);
205 case ICESTATE_GRABBED:
210 Player* player = dynamic_cast<Player*>(&object);
211 if (player) player->bounce(*this);
216 MrIceBlock::set_state(IceState state)
218 if(ice_state == state)
222 case ICESTATE_NORMAL:
223 WalkingBadguy::activate();
227 physic.set_velocity_y(-KICKSPEED);
228 bbox.set_size(34, 31.8f);
230 sound_manager->play("sounds/stomp.wav", get_pos());
231 physic.set_velocity_x(0);
232 physic.set_velocity_y(0);
234 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
238 case ICESTATE_KICKED:
239 sound_manager->play("sounds/kick.wav", get_pos());
241 physic.set_velocity_x(dir == LEFT ? -KICKSPEED : KICKSPEED);
242 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
243 // we should slide above 1 block holes now...
244 bbox.set_size(34, 31.8f);
246 case ICESTATE_GRABBED:
256 MrIceBlock::grab(MovingObject&, const Vector& pos, Direction dir)
258 movement = pos - get_pos();
260 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
261 set_state(ICESTATE_GRABBED);
262 set_group(COLGROUP_DISABLED);
266 MrIceBlock::ungrab(MovingObject& , Direction dir)
269 set_state(dir == UP ? ICESTATE_FLAT : ICESTATE_KICKED);
270 set_group(COLGROUP_MOVING);
274 MrIceBlock::is_portable() const
276 return ice_state == ICESTATE_FLAT;
279 IMPLEMENT_FACTORY(MrIceBlock, "mriceblock")