00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "badguy/igel.hpp"
00018 #include "object/bullet.hpp"
00019 #include "supertux/sector.hpp"
00020
00021 #include "supertux/object_factory.hpp"
00022
00023 namespace {
00024
00025 const float IGEL_SPEED = 80;
00026 const float TURN_RECOVER_TIME = 0.5;
00027 const float RANGE_OF_VISION = 256;
00029 }
00030
00031 Igel::Igel(const Reader& reader) :
00032 WalkingBadguy(reader, "images/creatures/igel/igel.sprite", "walking-left", "walking-right"),
00033 turn_recover_timer()
00034 {
00035 walk_speed = IGEL_SPEED;
00036 max_drop_height = 16;
00037 }
00038
00039 Igel::Igel(const Vector& pos, Direction d) :
00040 WalkingBadguy(pos, d, "images/creatures/igel/igel.sprite", "walking-left", "walking-right"),
00041 turn_recover_timer()
00042 {
00043 walk_speed = IGEL_SPEED;
00044 max_drop_height = 16;
00045 }
00046
00047 void
00048 Igel::be_normal()
00049 {
00050 initialize();
00051 }
00052
00053 void
00054 Igel::turn_around()
00055 {
00056 WalkingBadguy::turn_around();
00057 turn_recover_timer.start(TURN_RECOVER_TIME);
00058 }
00059
00060 bool
00061 Igel::can_see(const MovingObject& o)
00062 {
00063 Rectf mb = get_bbox();
00064 Rectf ob = o.get_bbox();
00065
00066 bool inReach_left = ((ob.p2.x < mb.p1.x) && (ob.p2.x >= mb.p1.x-((dir == LEFT) ? RANGE_OF_VISION : 0)));
00067 bool inReach_right = ((ob.p1.x > mb.p2.x) && (ob.p1.x <= mb.p2.x+((dir == RIGHT) ? RANGE_OF_VISION : 0)));
00068 bool inReach_top = (ob.p2.y >= mb.p1.y);
00069 bool inReach_bottom = (ob.p1.y <= mb.p2.y);
00070
00071 return ((inReach_left || inReach_right) && inReach_top && inReach_bottom);
00072 }
00073
00074 void
00075 Igel::active_update(float elapsed_time)
00076 {
00077 bool wants_to_flee = false;
00078
00079
00080 Sector* sector = Sector::current();
00081 for (Sector::GameObjects::iterator i = sector->gameobjects.begin(); i != sector->gameobjects.end(); ++i) {
00082 Bullet* bullet = dynamic_cast<Bullet*>(*i);
00083 if (!bullet) continue;
00084 if (bullet->get_type() != FIRE_BONUS) continue;
00085 if (can_see(*bullet)) wants_to_flee = true;
00086 }
00087
00088
00089 if (wants_to_flee && (!turn_recover_timer.started())) {
00090 turn_around();
00091 BadGuy::active_update(elapsed_time);
00092 return;
00093 }
00094
00095
00096 WalkingBadguy::active_update(elapsed_time);
00097 }
00098
00099 HitResponse
00100 Igel::collision_bullet(Bullet& bullet, const CollisionHit& hit)
00101 {
00102
00103 if (((dir == LEFT) && hit.left) || ((dir == RIGHT) && hit.right)) {
00104 return BadGuy::collision_bullet(bullet, hit);
00105 }
00106
00107
00108 bullet.ricochet(*this, hit);
00109 return FORCE_MOVE;
00110 }
00111
00112 bool
00113 Igel::collision_squished(GameObject& )
00114 {
00115
00116 return false;
00117 }
00118
00119