src/badguy/igel.cpp

Go to the documentation of this file.
00001 //  SuperTux - Badguy "Igel"
00002 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
00003 //
00004 //  This program is free software: you can redistribute it and/or modify
00005 //  it under the terms of the GNU General Public License as published by
00006 //  the Free Software Foundation, either version 3 of the License, or
00007 //  (at your option) any later version.
00008 //
00009 //  This program is distributed in the hope that it will be useful,
00010 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 //  GNU General Public License for more details.
00013 //
00014 //  You should have received a copy of the GNU General Public License
00015 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
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 } // namespace
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   // check if we see a fire bullet
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   // if we flee, handle this ourselves
00089   if (wants_to_flee && (!turn_recover_timer.started())) {
00090     turn_around();
00091     BadGuy::active_update(elapsed_time);
00092     return;
00093   }
00094 
00095   // else adhere to default behaviour
00096   WalkingBadguy::active_update(elapsed_time);
00097 }
00098 
00099 HitResponse
00100 Igel::collision_bullet(Bullet& bullet, const CollisionHit& hit)
00101 {
00102   // default reaction if hit on front side
00103   if (((dir == LEFT) && hit.left) || ((dir == RIGHT) && hit.right)) {
00104     return BadGuy::collision_bullet(bullet, hit);
00105   }
00106 
00107   // else make bullet ricochet and ignore the hit
00108   bullet.ricochet(*this, hit);
00109   return FORCE_MOVE;
00110 }
00111 
00112 bool
00113 Igel::collision_squished(GameObject& )
00114 {
00115   // this will hurt
00116   return false;
00117 }
00118 
00119 /* EOF */

Generated on Mon Jun 9 03:38:17 2014 for SuperTux by  doxygen 1.5.1