-// $Id: igel.cpp 3478 2006-04-30 23:14:15Z sommer $
+// $Id$
//
// SuperTux - Badguy "Igel"
// Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
}
Igel::Igel(const lisp::Lisp& reader)
- : BadGuy(reader, "images/creatures/igel/igel.sprite"), state(STATE_NORMAL)
+ : WalkingBadguy(reader, "images/creatures/igel/igel.sprite", "walking-left", "walking-right")
{
- set_direction = false;
+ walk_speed = WALKSPEED;
+ max_drop_height = 0;
}
Igel::Igel(const Vector& pos, Direction d)
- : BadGuy(pos, "images/creatures/igel/igel.sprite"), state(STATE_NORMAL)
+ : WalkingBadguy(pos, d, "images/creatures/igel/igel.sprite", "walking-left", "walking-right")
{
- set_direction = true;
- initial_direction = d;
+ walk_speed = WALKSPEED;
+ max_drop_height = 0;
}
void
Igel::write(lisp::Writer& writer)
{
writer.start_list("igel");
-
- writer.write_float("x", start_position.x);
- writer.write_float("y", start_position.y);
-
+ WalkingBadguy::write(writer);
writer.end_list("igel");
}
void
-Igel::activate()
-{
- if (set_direction) {dir = initial_direction;}
-
- be_normal();
-}
-
-void
Igel::be_normal()
{
- state = STATE_NORMAL;
- sprite->set_action(dir == LEFT ? "walking-left" : "walking-right");
-
- physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
+ activate();
}
void
Igel::turn_around()
{
- dir = (dir == LEFT ? RIGHT : LEFT);
+ WalkingBadguy::turn_around();
turn_recover_timer.start(TURN_RECOVER_TIME);
- be_normal();
}
bool
void
Igel::active_update(float elapsed_time)
{
- switch (state) {
-
- case STATE_NORMAL:
- if (might_fall()) {
- // turn around when we are at a ledge
- turn_around();
- }
- else if (!turn_recover_timer.started()) {
- // turn around when we see a Bullet
- Sector* sector = Sector::current();
- for (Sector::GameObjects::iterator i = sector->gameobjects.begin(); i != sector->gameobjects.end(); ++i) {
- Bullet* bullet = dynamic_cast<Bullet*>(*i);
- if (bullet) {
- if (can_see(*bullet)) turn_around();
- }
- }
- }
- break;
-
- }
-
- BadGuy::active_update(elapsed_time);
-}
-
-HitResponse
-Igel::collision_solid(GameObject& , const CollisionHit& hit)
-{
- if(fabsf(hit.normal.y) > .5) { // floor or roof
- physic.set_velocity_y(0);
- return CONTINUE;
+ bool wants_to_flee = false;
+
+ // check if we see a bullet
+ Sector* sector = Sector::current();
+ for (Sector::GameObjects::iterator i = sector->gameobjects.begin(); i != sector->gameobjects.end(); ++i) {
+ Bullet* bullet = dynamic_cast<Bullet*>(*i);
+ if (!bullet) continue;
+ if (can_see(*bullet)) wants_to_flee = true;
}
- // hit left or right
- switch(state) {
-
- case STATE_NORMAL:
- turn_around();
- break;
-
+ // if we flee, handle this ourselves
+ if (wants_to_flee && (!turn_recover_timer.started())) {
+ turn_around();
+ BadGuy::active_update(elapsed_time);
+ return;
}
- return CONTINUE;
+ // else adhere to default behaviour
+ WalkingBadguy::active_update(elapsed_time);
}
HitResponse
-Igel::collision_badguy(BadGuy& , const CollisionHit& hit)
+Igel::collision_bullet(Bullet& bullet, const CollisionHit& hit)
{
- if(fabsf(hit.normal.y) > .5) { // floor or roof
- physic.set_velocity_y(0);
- return CONTINUE;
- }
-
- // hit left or right
- switch(state) {
+ //remove bullet
+ bullet.remove_me();
- case STATE_NORMAL:
- turn_around();
- break;
-
- }
-
- return CONTINUE;
-}
-
-HitResponse
-Igel::collision_bullet(Bullet& , const CollisionHit& hit)
-{
// die if hit on front side
- if (((dir == LEFT) && (hit.normal.x > 0)) || ((dir == RIGHT) && (hit.normal.x < 0))) {
+ if (((dir == LEFT) && hit.left) || ((dir == RIGHT) && hit.right)) {
kill_fall();
return ABORT_MOVE;
}
bool
Igel::collision_squished(Player& )
{
- switch(state) {
-
- case STATE_NORMAL:
- // this will hurt
- return false;
- break;
-
- }
-
- kill_fall();
- return true;
+ // this will hurt
+ return false;
}
IMPLEMENT_FACTORY(Igel, "igel")