#include "object/block.hpp"
namespace {
- const float WALKSPEED = 80;
const float KICKSPEED = 500;
const int MAXSQUISHES = 10;
const float KICKSPEED_Y = -500; /**< y-velocity gained when kicked */
}
Snail::Snail(const lisp::Lisp& reader)
- : BadGuy(reader, "images/creatures/snail/snail.sprite"), state(STATE_NORMAL), squishcount(0)
+ : WalkingBadguy(reader, "images/creatures/snail/snail.sprite", "left", "right"), state(STATE_NORMAL), squishcount(0)
{
- reader.get("direction", direction);
- set_direction = false;
- if( direction != "auto" && direction != ""){
- set_direction = true;
- initial_direction = str2dir( direction );
- }
+ 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");
}
Snail::Snail(const Vector& pos, Direction d)
- : BadGuy(pos, "images/creatures/snail/snail.sprite"), state(STATE_NORMAL), squishcount(0)
+ : WalkingBadguy(pos, d, "images/creatures/snail/snail.sprite", "left", "right"), state(STATE_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");
Snail::write(lisp::Writer& writer)
{
writer.start_list("snail");
-
- writer.write_string("direction", direction);
- writer.write_float("x", start_position.x);
- writer.write_float("y", start_position.y);
-
+ WalkingBadguy::write(writer);
writer.end_list("snail");
}
void
Snail::activate()
{
- if (set_direction) {dir = initial_direction;}
-
+ WalkingBadguy::activate();
be_normal();
}
void
Snail::be_normal()
{
- state = STATE_NORMAL;
- sprite->set_action(dir == LEFT ? "left" : "right");
+ if (state == STATE_NORMAL) return;
- physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
+ state = STATE_NORMAL;
+ WalkingBadguy::activate();
}
void
state = STATE_FLAT;
sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
sprite->set_fps(64);
-
+
physic.set_velocity_x(0);
- physic.set_velocity_y(0);
-
+ physic.set_velocity_y(0);
+
flat_timer.start(4);
}
sprite->set_fps(64);
physic.set_velocity_x(0);
- physic.set_velocity_y(0);
-
+ physic.set_velocity_y(0);
+
// start a timer to delay addition of upward movement until we are (hopefully) out from under the player
- kicked_delay_timer.start(0.05);
+ kicked_delay_timer.start(0.05f);
}
+bool
+Snail::can_break(){
+ return state == STATE_KICKED;
+}
void
Snail::active_update(float elapsed_time)
switch (state) {
case STATE_NORMAL:
- if (might_fall(601)) {
- 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);
break;
case STATE_FLAT:
if (flat_timer.check()) {
be_normal();
}
+ BadGuy::active_update(elapsed_time);
break;
case STATE_KICKED_DELAY:
physic.set_velocity_y(KICKSPEED_Y);
state = STATE_KICKED;
}
+ BadGuy::active_update(elapsed_time);
break;
case STATE_KICKED:
physic.set_velocity_x(physic.get_velocity_x() * pow(0.99, elapsed_time/0.02));
- if (fabsf(physic.get_velocity_x()) < WALKSPEED) be_normal();
+ if (fabsf(physic.get_velocity_x()) < walk_speed) be_normal();
+ BadGuy::active_update(elapsed_time);
break;
}
- BadGuy::active_update(elapsed_time);
}
-HitResponse
-Snail::collision_solid(GameObject& object, const CollisionHit& hit)
+void
+Snail::collision_solid(const CollisionHit& hit)
{
- if(fabsf(hit.normal.y) > .5) { // floor or roof
- physic.set_velocity_y(0);
-
- switch (state) {
- case STATE_NORMAL:
- case STATE_FLAT:
- case STATE_KICKED_DELAY:
- break;
- case STATE_KICKED:
- break;
- }
-
- return CONTINUE;
- }
- // hit left or right
- switch(state) {
-
+ update_on_ground_flag(hit);
+
+ switch (state) {
case STATE_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 STATE_FLAT:
- case STATE_KICKED_DELAY:
- physic.set_velocity_x(0);
+ if(hit.top || hit.bottom) {
+ physic.set_velocity_y(0);
+ }
+ if(hit.left || hit.right) {
+ }
break;
-
- case STATE_KICKED: {
- sound_manager->play("sounds/iceblock_bump.wav", get_pos());
-
- // open bonusblocks, crash bricks
- BonusBlock* bonusblock = dynamic_cast<BonusBlock*> (&object);
- if(bonusblock) {
- bonusblock->try_open();
+ case STATE_KICKED_DELAY:
+ if(hit.top || hit.bottom) {
+ physic.set_velocity_y(0);
}
- Brick* brick = dynamic_cast<Brick*> (&object);
- if(brick) {
- brick->try_break();
+ if(hit.left || hit.right) {
+ physic.set_velocity_x(0);
}
-
- dir = (dir == LEFT) ? RIGHT : LEFT;
- sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
-
- physic.set_velocity_x(-physic.get_velocity_x()*0.75);
- if (fabsf(physic.get_velocity_x()) < WALKSPEED) be_normal();
break;
+ case STATE_KICKED:
+ if(hit.top || hit.bottom) {
+ physic.set_velocity_y(0);
+ }
+ if(hit.left || hit.right) {
+ sound_manager->play("sounds/iceblock_bump.wav", get_pos());
- }
+ if( ( dir == LEFT && hit.left ) || ( dir == RIGHT && hit.right) ){
+ dir = (dir == LEFT) ? RIGHT : LEFT;
+ sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
+
+ physic.set_velocity_x(-physic.get_velocity_x()*0.75);
+ if (fabsf(physic.get_velocity_x()) < walk_speed) be_normal();
+ }
+
+ }
+ break;
}
- return CONTINUE;
}
HitResponse
{
switch(state) {
case STATE_NORMAL:
- if(fabsf(hit.normal.x) > .5) {
- 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 STATE_FLAT:
case STATE_KICKED_DELAY:
return FORCE_MOVE;
}
bool
-Snail::collision_squished(Player& player)
+Snail::collision_squished(GameObject& object)
{
switch(state) {
sound_manager->play("sounds/stomp.wav", get_pos());
be_flat();
break;
-
+
case STATE_FLAT:
sound_manager->play("sounds/kick.wav", get_pos());
-
- 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;
+ }
}
be_kicked();
break;
case STATE_KICKED_DELAY:
break;
-
+
}
- player.bounce(*this);
+ Player* player = dynamic_cast<Player*>(&object);
+ if (player) player->bounce(*this);
return true;
}