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)
141 // handle kicks from left or right side
142 if(ice_state == ICESTATE_FLAT && get_state() == STATE_ACTIVE) {
146 set_state(ICESTATE_KICKED);
148 } else if(hit.right) {
151 set_state(ICESTATE_KICKED);
156 return BadGuy::collision_player(player, hit);
160 MrIceBlock::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
163 case ICESTATE_NORMAL:
164 return WalkingBadguy::collision_badguy(badguy, hit);
167 case ICESTATE_KICKED:
178 MrIceBlock::collision_squished(GameObject& object)
181 case ICESTATE_KICKED:
183 BadGuy* badguy = dynamic_cast<BadGuy*>(&object);
191 case ICESTATE_NORMAL:
193 Player* player = dynamic_cast<Player*>(&object);
195 if ((squishcount >= MAXSQUISHES) || (player && player->does_buttjump)) {
201 set_state(ICESTATE_FLAT);
202 nokick_timer.start(NOKICK_TIME);
206 MovingObject* movingobject = dynamic_cast<MovingObject*>(&object);
207 if (movingobject && (movingobject->get_pos().x < get_pos().x)) {
213 if (nokick_timer.check()) set_state(ICESTATE_KICKED);
215 case ICESTATE_GRABBED:
220 Player* player = dynamic_cast<Player*>(&object);
221 if (player) player->bounce(*this);
226 MrIceBlock::set_state(IceState state)
228 if(ice_state == state)
232 case ICESTATE_NORMAL:
233 WalkingBadguy::initialize();
237 physic.set_velocity_y(-KICKSPEED);
238 bbox.set_size(34, 31.8f);
240 sound_manager->play("sounds/stomp.wav", get_pos());
241 physic.set_velocity_x(0);
242 physic.set_velocity_y(0);
244 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
248 case ICESTATE_KICKED:
249 sound_manager->play("sounds/kick.wav", get_pos());
251 physic.set_velocity_x(dir == LEFT ? -KICKSPEED : KICKSPEED);
252 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
253 // we should slide above 1 block holes now...
254 bbox.set_size(34, 31.8f);
256 case ICESTATE_GRABBED:
266 MrIceBlock::grab(MovingObject&, const Vector& pos, Direction dir)
268 movement = pos - get_pos();
270 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
271 set_state(ICESTATE_GRABBED);
272 set_colgroup_active(COLGROUP_DISABLED);
276 MrIceBlock::ungrab(MovingObject& , Direction dir)
279 set_state(dir == UP ? ICESTATE_FLAT : ICESTATE_KICKED);
280 set_colgroup_active(COLGROUP_MOVING);
284 MrIceBlock::is_portable() const
286 return ice_state == ICESTATE_FLAT;