fix cr/lfs and remove trailing whitespaces...
[supertux.git] / src / badguy / igel.cpp
1 //  $Id$
2 //
3 //  SuperTux - Badguy "Igel"
4 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
5 //
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.
10 //
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.
15 //
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.
19
20 #include <config.h>
21
22 #include "igel.hpp"
23 #include "object/block.hpp"
24 #include "sector.hpp"
25 #include "object/bullet.hpp"
26
27 namespace {
28   const float WALKSPEED = 80; /**< speed at which we walk around */
29   const float TURN_RECOVER_TIME = 0.5; /**< seconds before we will again turn around when shot at */
30   const float RANGE_OF_VISION = 256; /**< range in px at which we can see bullets */
31 }
32
33 Igel::Igel(const lisp::Lisp& reader)
34   : WalkingBadguy(reader, "images/creatures/igel/igel.sprite", "walking-left", "walking-right")
35 {
36   walk_speed = WALKSPEED;
37   max_drop_height = 0;
38 }
39
40 Igel::Igel(const Vector& pos, Direction d)
41   : WalkingBadguy(pos, d, "images/creatures/igel/igel.sprite", "walking-left", "walking-right")
42 {
43   walk_speed = WALKSPEED;
44   max_drop_height = 0;
45 }
46
47 void
48 Igel::write(lisp::Writer& writer)
49 {
50   writer.start_list("igel");
51   WalkingBadguy::write(writer);
52   writer.end_list("igel");
53 }
54
55 void
56 Igel::be_normal()
57 {
58   activate();
59 }
60
61 void
62 Igel::turn_around()
63 {
64   WalkingBadguy::turn_around();
65   turn_recover_timer.start(TURN_RECOVER_TIME);
66 }
67
68 bool
69 Igel::can_see(const MovingObject& o)
70 {
71   Rect mb = get_bbox();
72   Rect ob = o.get_bbox();
73
74   bool inReach_left = (ob.p2.x >= mb.p1.x-((dir == LEFT) ? RANGE_OF_VISION : 0));
75   bool inReach_right = (ob.p1.x <= mb.p2.x+((dir == RIGHT) ? RANGE_OF_VISION : 0));
76   bool inReach_top = (ob.p2.y >= mb.p1.y);
77   bool inReach_bottom = (ob.p1.y <= mb.p2.y);
78
79   return (inReach_left && inReach_right && inReach_top && inReach_bottom);
80 }
81
82 void
83 Igel::active_update(float elapsed_time)
84 {
85   bool wants_to_flee = false;
86
87   // check if we see a bullet
88   Sector* sector = Sector::current();
89   for (Sector::GameObjects::iterator i = sector->gameobjects.begin(); i != sector->gameobjects.end(); ++i) {
90     Bullet* bullet = dynamic_cast<Bullet*>(*i);
91     if (!bullet) continue;
92     if (can_see(*bullet)) wants_to_flee = true;
93   }
94
95   // if we flee, handle this ourselves
96   if (wants_to_flee && (!turn_recover_timer.started())) {
97     turn_around();
98     BadGuy::active_update(elapsed_time);
99     return;
100   }
101
102   // else adhere to default behaviour
103   WalkingBadguy::active_update(elapsed_time);
104 }
105
106 HitResponse
107 Igel::collision_bullet(Bullet& bullet, const CollisionHit& hit)
108 {
109   //remove bullet
110   bullet.remove_me();
111
112   // die if hit on front side
113   if (((dir == LEFT) && hit.left) || ((dir == RIGHT) && hit.right)) {
114     kill_fall();
115     return ABORT_MOVE;
116   }
117
118   // else ignore bullet
119   return FORCE_MOVE;
120 }
121
122 bool
123 Igel::collision_squished(Player& )
124 {
125   // this will hurt
126   return false;
127 }
128
129 IMPLEMENT_FACTORY(Igel, "igel")