2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "badguy/mriceblock.hpp"
19 #include "audio/sound_manager.hpp"
20 #include "object/player.hpp"
21 #include "sprite/sprite.hpp"
22 #include "supertux/object_factory.hpp"
25 const float KICKSPEED = 500;
26 const int MAXSQUISHES = 10;
27 const float NOKICK_TIME = 0.1f;
30 MrIceBlock::MrIceBlock(const Reader& reader) :
31 WalkingBadguy(reader, "images/creatures/mr_iceblock/mr_iceblock.sprite", "left", "right"),
32 ice_state(ICESTATE_NORMAL),
38 max_drop_height = 600;
39 sound_manager->preload("sounds/iceblock_bump.wav");
40 sound_manager->preload("sounds/stomp.wav");
41 sound_manager->preload("sounds/kick.wav");
44 MrIceBlock::MrIceBlock(const Vector& pos, Direction d) :
45 WalkingBadguy(pos, d, "images/creatures/mr_iceblock/mr_iceblock.sprite", "left", "right"),
46 ice_state(ICESTATE_NORMAL),
52 max_drop_height = 600;
53 sound_manager->preload("sounds/iceblock_bump.wav");
54 sound_manager->preload("sounds/stomp.wav");
55 sound_manager->preload("sounds/kick.wav");
59 MrIceBlock::initialize()
61 WalkingBadguy::initialize();
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:
186 BadGuy* badguy = dynamic_cast<BadGuy*>(&object);
194 case ICESTATE_NORMAL:
196 Player* player = dynamic_cast<Player*>(&object);
198 if ((squishcount >= MAXSQUISHES) || (player && player->does_buttjump)) {
204 set_state(ICESTATE_FLAT);
205 nokick_timer.start(NOKICK_TIME);
209 MovingObject* movingobject = dynamic_cast<MovingObject*>(&object);
210 if (movingobject && (movingobject->get_pos().x < get_pos().x)) {
216 if (nokick_timer.check()) set_state(ICESTATE_KICKED);
218 case ICESTATE_GRABBED:
223 Player* player = dynamic_cast<Player*>(&object);
224 if (player) player->bounce(*this);
229 MrIceBlock::set_state(IceState state)
231 if(ice_state == state)
235 case ICESTATE_NORMAL:
236 WalkingBadguy::initialize();
240 physic.set_velocity_y(-KICKSPEED);
241 bbox.set_size(34, 31.8f);
243 sound_manager->play("sounds/stomp.wav", get_pos());
244 physic.set_velocity_x(0);
245 physic.set_velocity_y(0);
247 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
251 case ICESTATE_KICKED:
252 sound_manager->play("sounds/kick.wav", get_pos());
254 physic.set_velocity_x(dir == LEFT ? -KICKSPEED : KICKSPEED);
255 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
256 // we should slide above 1 block holes now...
257 bbox.set_size(34, 31.8f);
259 case ICESTATE_GRABBED:
269 MrIceBlock::grab(MovingObject&, const Vector& pos, Direction dir)
271 movement = pos - get_pos();
273 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
274 set_state(ICESTATE_GRABBED);
275 set_colgroup_active(COLGROUP_DISABLED);
279 MrIceBlock::ungrab(MovingObject& , Direction dir)
282 set_state(dir == UP ? ICESTATE_FLAT : ICESTATE_KICKED);
283 set_colgroup_active(COLGROUP_MOVING);
287 MrIceBlock::is_portable() const
289 return ice_state == ICESTATE_FLAT;
292 IMPLEMENT_FACTORY(MrIceBlock, "mriceblock");