Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / badguy / igel.cpp
1 //  SuperTux - Badguy "Igel"
2 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "badguy/igel.hpp"
18 #include "object/bullet.hpp"
19 #include "supertux/sector.hpp"
20
21 #include "supertux/object_factory.hpp"
22
23 namespace {
24
25 const float WALKSPEED = 80; /**< speed at which we walk around */
26 const float TURN_RECOVER_TIME = 0.5; /**< seconds before we will again turn around when shot at */
27 const float RANGE_OF_VISION = 256; /**< range in px at which we can see bullets */
28
29 } // namespace
30
31 Igel::Igel(const Reader& reader) :
32   WalkingBadguy(reader, "images/creatures/igel/igel.sprite", "walking-left", "walking-right"), 
33   turn_recover_timer()
34 {
35   walk_speed = WALKSPEED;
36   max_drop_height = 16;
37 }
38
39 Igel::Igel(const Vector& pos, Direction d) :
40   WalkingBadguy(pos, d, "images/creatures/igel/igel.sprite", "walking-left", "walking-right"),
41   turn_recover_timer()
42 {
43   walk_speed = WALKSPEED;
44   max_drop_height = 16;
45 }
46
47 void
48 Igel::be_normal()
49 {
50   initialize();
51 }
52
53 void
54 Igel::turn_around()
55 {
56   WalkingBadguy::turn_around();
57   turn_recover_timer.start(TURN_RECOVER_TIME);
58 }
59
60 bool
61 Igel::can_see(const MovingObject& o)
62 {
63   Rect mb = get_bbox();
64   Rect ob = o.get_bbox();
65
66   bool inReach_left = ((ob.p2.x < mb.p1.x) && (ob.p2.x >= mb.p1.x-((dir == LEFT) ? RANGE_OF_VISION : 0)));
67   bool inReach_right = ((ob.p1.x > mb.p2.x) && (ob.p1.x <= mb.p2.x+((dir == RIGHT) ? RANGE_OF_VISION : 0)));
68   bool inReach_top = (ob.p2.y >= mb.p1.y);
69   bool inReach_bottom = (ob.p1.y <= mb.p2.y);
70
71   return ((inReach_left || inReach_right) && inReach_top && inReach_bottom);
72 }
73
74 void
75 Igel::active_update(float elapsed_time)
76 {
77   bool wants_to_flee = false;
78
79   // check if we see a fire bullet
80   Sector* sector = Sector::current();
81   for (Sector::GameObjects::iterator i = sector->gameobjects.begin(); i != sector->gameobjects.end(); ++i) {
82     Bullet* bullet = dynamic_cast<Bullet*>(*i);
83     if (!bullet) continue;
84     if (bullet->get_type() != FIRE_BONUS) continue;
85     if (can_see(*bullet)) wants_to_flee = true;
86   }
87
88   // if we flee, handle this ourselves
89   if (wants_to_flee && (!turn_recover_timer.started())) {
90     turn_around();
91     BadGuy::active_update(elapsed_time);
92     return;
93   }
94
95   // else adhere to default behaviour
96   WalkingBadguy::active_update(elapsed_time);
97 }
98
99 HitResponse
100 Igel::collision_bullet(Bullet& bullet, const CollisionHit& hit)
101 {
102   // default reaction if hit on front side
103   if (((dir == LEFT) && hit.left) || ((dir == RIGHT) && hit.right)) {
104     return BadGuy::collision_bullet(bullet, hit);
105   }
106
107   // else make bullet ricochet and ignore the hit
108   bullet.ricochet(*this, hit);
109   return FORCE_MOVE;
110 }
111
112 bool
113 Igel::collision_squished(GameObject& )
114 {
115   // this will hurt
116   return false;
117 }
118
119 IMPLEMENT_FACTORY(Igel, "igel");
120
121 /* EOF */