3 // SuperTux - Badguy "Igel"
4 // Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include "object/block.hpp"
25 #include "object/bullet.hpp"
27 #include "lisp/writer.hpp"
28 #include "object_factory.hpp"
31 const float WALKSPEED = 80; /**< speed at which we walk around */
32 const float TURN_RECOVER_TIME = 0.5; /**< seconds before we will again turn around when shot at */
33 const float RANGE_OF_VISION = 256; /**< range in px at which we can see bullets */
36 Igel::Igel(const lisp::Lisp& reader)
37 : WalkingBadguy(reader, "images/creatures/igel/igel.sprite", "walking-left", "walking-right")
39 walk_speed = WALKSPEED;
43 Igel::Igel(const Vector& pos, Direction d)
44 : WalkingBadguy(pos, d, "images/creatures/igel/igel.sprite", "walking-left", "walking-right")
46 walk_speed = WALKSPEED;
51 Igel::write(lisp::Writer& writer)
53 writer.start_list("igel");
54 WalkingBadguy::write(writer);
55 writer.end_list("igel");
67 WalkingBadguy::turn_around();
68 turn_recover_timer.start(TURN_RECOVER_TIME);
72 Igel::can_see(const MovingObject& o)
75 Rect ob = o.get_bbox();
77 bool inReach_left = ((ob.p2.x < mb.p1.x) && (ob.p2.x >= mb.p1.x-((dir == LEFT) ? RANGE_OF_VISION : 0)));
78 bool inReach_right = ((ob.p1.x > mb.p2.x) && (ob.p1.x <= mb.p2.x+((dir == RIGHT) ? RANGE_OF_VISION : 0)));
79 bool inReach_top = (ob.p2.y >= mb.p1.y);
80 bool inReach_bottom = (ob.p1.y <= mb.p2.y);
82 return ((inReach_left || inReach_right) && inReach_top && inReach_bottom);
86 Igel::active_update(float elapsed_time)
88 bool wants_to_flee = false;
90 // check if we see a fire bullet
91 Sector* sector = Sector::current();
92 for (Sector::GameObjects::iterator i = sector->gameobjects.begin(); i != sector->gameobjects.end(); ++i) {
93 Bullet* bullet = dynamic_cast<Bullet*>(*i);
94 if (!bullet) continue;
95 if (bullet->get_type() != FIRE_BONUS) continue;
96 if (can_see(*bullet)) wants_to_flee = true;
99 // if we flee, handle this ourselves
100 if (wants_to_flee && (!turn_recover_timer.started())) {
102 BadGuy::active_update(elapsed_time);
106 // else adhere to default behaviour
107 WalkingBadguy::active_update(elapsed_time);
111 Igel::collision_bullet(Bullet& bullet, const CollisionHit& hit)
113 // default reaction if hit on front side
114 if (((dir == LEFT) && hit.left) || ((dir == RIGHT) && hit.right)) {
115 return BadGuy::collision_bullet(bullet, hit);
118 // else make bullet ricochet and ignore the hit
119 bullet.ricochet(*this, hit);
124 Igel::collision_squished(GameObject& )
130 IMPLEMENT_FACTORY(Igel, "igel")