#include "object/block.hpp"
namespace {
- const float WALKSPEED = 80;
const float KICKSPEED = 500;
const int MAXSQUISHES = 10;
+ const float NOKICK_TIME = 0.1f;
}
MrIceBlock::MrIceBlock(const lisp::Lisp& reader)
- : BadGuy(reader, "images/creatures/mr_iceblock/mr_iceblock.sprite"), ice_state(ICESTATE_NORMAL), squishcount(0)
+ : WalkingBadguy(reader, "images/creatures/mr_iceblock/mr_iceblock.sprite", "left", "right"), ice_state(ICESTATE_NORMAL), squishcount(0)
{
- set_direction = false;
+ walk_speed = 80;
+ max_drop_height = 600;
+ sound_manager->preload("sounds/iceblock_bump.wav");
+ sound_manager->preload("sounds/stomp.wav");
+ sound_manager->preload("sounds/kick.wav");
}
MrIceBlock::MrIceBlock(const Vector& pos, Direction d)
- : BadGuy(pos, "images/creatures/mr_iceblock/mr_iceblock.sprite"), ice_state(ICESTATE_NORMAL), squishcount(0)
+ : WalkingBadguy(pos, d, "images/creatures/mr_iceblock/mr_iceblock.sprite", "left", "right"), ice_state(ICESTATE_NORMAL), squishcount(0)
{
- set_direction = true;
- initial_direction = d;
+ walk_speed = 80;
+ max_drop_height = 600;
+ sound_manager->preload("sounds/iceblock_bump.wav");
+ sound_manager->preload("sounds/stomp.wav");
+ sound_manager->preload("sounds/kick.wav");
}
void
MrIceBlock::write(lisp::Writer& writer)
{
writer.start_list("mriceblock");
-
- writer.write_float("x", start_position.x);
- writer.write_float("y", start_position.y);
-
+ WalkingBadguy::write(writer);
writer.end_list("mriceblock");
}
void
-MrIceBlock::activate()
+MrIceBlock::initialize()
{
- if (set_direction) {
- dir = initial_direction;
- }
-
- physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
- sprite->set_action(dir == LEFT ? "left" : "right");
+ WalkingBadguy::initialize();
+ set_state(ICESTATE_NORMAL);
}
void
set_state(ICESTATE_NORMAL);
}
- if (ice_state == ICESTATE_NORMAL && might_fall(601))
+ if (ice_state == ICESTATE_NORMAL)
{
- dir = (dir == LEFT ? RIGHT : LEFT);
- sprite->set_action(dir == LEFT ? "left" : "right");
- physic.set_velocity_x(-physic.get_velocity_x());
+ WalkingBadguy::active_update(elapsed_time);
+ return;
}
BadGuy::active_update(elapsed_time);
}
-HitResponse
-MrIceBlock::collision_solid(GameObject& object, const CollisionHit& hit)
+bool
+MrIceBlock::can_break(){
+ return ice_state == ICESTATE_KICKED;
+}
+
+void
+MrIceBlock::collision_solid(const CollisionHit& hit)
{
- if(fabsf(hit.normal.y) > .5) { // floor or roof
+ update_on_ground_flag(hit);
+
+ if(hit.top || hit.bottom) { // floor or roof
physic.set_velocity_y(0);
- return CONTINUE;
+ return;
}
+
// hit left or right
switch(ice_state) {
case ICESTATE_NORMAL:
- dir = dir == LEFT ? RIGHT : LEFT;
- sprite->set_action(dir == LEFT ? "left" : "right");
- physic.set_velocity_x(-physic.get_velocity_x());
+ WalkingBadguy::collision_solid(hit);
break;
case ICESTATE_KICKED: {
- BonusBlock* bonusblock = dynamic_cast<BonusBlock*> (&object);
- if(bonusblock) {
- bonusblock->try_open();
- }
- Brick* brick = dynamic_cast<Brick*> (&object);
- if(brick) {
- brick->try_break();
+ if(hit.right && dir == RIGHT) {
+ dir = LEFT;
+ sound_manager->play("sounds/iceblock_bump.wav", get_pos());
+ physic.set_velocity_x(-KICKSPEED);
+ } else if(hit.left && dir == LEFT) {
+ dir = RIGHT;
+ sound_manager->play("sounds/iceblock_bump.wav", get_pos());
+ physic.set_velocity_x(KICKSPEED);
}
-
- dir = dir == LEFT ? RIGHT : LEFT;
sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
- physic.set_velocity_x(-physic.get_velocity_x());
- sound_manager->play("sounds/iceblock_bump.wav", get_pos());
break;
}
case ICESTATE_FLAT:
physic.set_velocity_x(0);
break;
case ICESTATE_GRABBED:
- return FORCE_MOVE;
+ break;
}
-
- return CONTINUE;
}
HitResponse
if(ice_state == ICESTATE_GRABBED)
return FORCE_MOVE;
+ if(dir == UP) {
+ return FORCE_MOVE;
+ }
+
// handle kicks from left or right side
if(ice_state == ICESTATE_FLAT && get_state() == STATE_ACTIVE) {
- // hit from left side
- if(hit.normal.x > 0.7 &&
- !player.get_controller()->hold(Controller::ACTION)) {
+ if(hit.left) {
dir = RIGHT;
player.kick();
set_state(ICESTATE_KICKED);
return FORCE_MOVE;
- }
- else if(hit.normal.x < -0.7 &&
- !player.get_controller()->hold(Controller::ACTION)) {
+ } else if(hit.right) {
dir = LEFT;
player.kick();
set_state(ICESTATE_KICKED);
return FORCE_MOVE;
}
}
-
+
return BadGuy::collision_player(player, hit);
}
{
switch(ice_state) {
case ICESTATE_NORMAL:
- if(fabsf(hit.normal.x) > .8) {
- dir = dir == LEFT ? RIGHT : LEFT;
- sprite->set_action(dir == LEFT ? "left" : "right");
- physic.set_velocity_x(-physic.get_velocity_x());
- }
- return CONTINUE;
+ return WalkingBadguy::collision_badguy(badguy, hit);
case ICESTATE_FLAT:
return FORCE_MOVE;
case ICESTATE_KICKED:
}
bool
-MrIceBlock::collision_squished(Player& player)
+MrIceBlock::collision_squished(GameObject& object)
{
switch(ice_state) {
case ICESTATE_KICKED:
+ {
+ BadGuy* badguy = dynamic_cast<BadGuy*>(&object);
+ if (badguy) {
+ badguy->kill_fall();
+ break;
+ }
+ }
+
+ // fall through
case ICESTATE_NORMAL:
- squishcount++;
- if(squishcount >= MAXSQUISHES) {
- kill_fall();
- return true;
+ {
+ Player* player = dynamic_cast<Player*>(&object);
+ squishcount++;
+ if ((squishcount >= MAXSQUISHES) || (player && player->does_buttjump)) {
+ kill_fall();
+ return true;
+ }
}
set_state(ICESTATE_FLAT);
+ nokick_timer.start(NOKICK_TIME);
break;
case ICESTATE_FLAT:
- if(player.get_pos().x < get_pos().x) {
- dir = RIGHT;
- } else {
- dir = LEFT;
+ {
+ MovingObject* movingobject = dynamic_cast<MovingObject*>(&object);
+ if (movingobject && (movingobject->get_pos().x < get_pos().x)) {
+ dir = RIGHT;
+ } else {
+ dir = LEFT;
+ }
}
- set_state(ICESTATE_KICKED);
+ if (nokick_timer.check()) set_state(ICESTATE_KICKED);
break;
case ICESTATE_GRABBED:
assert(false);
break;
}
- player.bounce(*this);
+ Player* player = dynamic_cast<Player*>(&object);
+ if (player) player->bounce(*this);
return true;
}
{
if(ice_state == state)
return;
-
- if(state == ICESTATE_FLAT)
- flags |= FLAG_PORTABLE;
- else
- flags &= ~FLAG_PORTABLE;
switch(state) {
case ICESTATE_NORMAL:
- physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
- sprite->set_action(dir == LEFT ? "left" : "right");
+ WalkingBadguy::initialize();
break;
case ICESTATE_FLAT:
- sound_manager->play("sounds/stomp.wav", get_pos());
- physic.set_velocity_x(0);
- physic.set_velocity_y(0);
-
- sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
+ if(dir == UP) {
+ physic.set_velocity_y(-KICKSPEED);
+ bbox.set_size(34, 31.8f);
+ } else {
+ sound_manager->play("sounds/stomp.wav", get_pos());
+ physic.set_velocity_x(0);
+ physic.set_velocity_y(0);
+
+ sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
+ }
flat_timer.start(4);
break;
case ICESTATE_KICKED:
physic.set_velocity_x(dir == LEFT ? -KICKSPEED : KICKSPEED);
sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
// we should slide above 1 block holes now...
- bbox.set_size(34, 31.8);
+ bbox.set_size(34, 31.8f);
break;
case ICESTATE_GRABBED:
flat_timer.stop();
this->dir = dir;
sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
set_state(ICESTATE_GRABBED);
- set_group(COLGROUP_DISABLED);
+ set_colgroup_active(COLGROUP_DISABLED);
}
void
MrIceBlock::ungrab(MovingObject& , Direction dir)
{
this->dir = dir;
- set_state(ICESTATE_KICKED);
- set_group(COLGROUP_MOVING);
+ set_state(dir == UP ? ICESTATE_FLAT : ICESTATE_KICKED);
+ set_colgroup_active(COLGROUP_MOVING);
+}
+
+bool
+MrIceBlock::is_portable() const
+{
+ return ice_state == ICESTATE_FLAT;
}
IMPLEMENT_FACTORY(MrIceBlock, "mriceblock")