02b7163429b8679085f8438794fea1af7cafc3f9
[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   : BadGuy(reader, "images/creatures/igel/igel.sprite"), state(STATE_NORMAL)
35 {
36 }
37
38 Igel::Igel(const Vector& pos, Direction d)
39   : BadGuy(pos, d, "images/creatures/igel/igel.sprite"), state(STATE_NORMAL)
40 {
41 }
42
43 void
44 Igel::write(lisp::Writer& writer)
45 {
46   writer.start_list("igel");
47
48   writer.write_float("x", start_position.x);
49   writer.write_float("y", start_position.y);
50
51   writer.end_list("igel");
52 }
53
54 void
55 Igel::activate()
56 {
57   be_normal();
58 }
59
60 void
61 Igel::be_normal()
62 {
63   state = STATE_NORMAL;
64   sprite->set_action(dir == LEFT ? "walking-left" : "walking-right");
65
66   physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
67 }
68
69 void
70 Igel::turn_around()
71 {
72   dir = (dir == LEFT ? RIGHT : LEFT);
73   turn_recover_timer.start(TURN_RECOVER_TIME);
74   be_normal();
75 }
76
77 bool
78 Igel::can_see(const MovingObject& o)
79 {
80   Rect mb = get_bbox();
81   Rect ob = o.get_bbox();
82
83   bool inReach_left = (ob.p2.x >= mb.p1.x-((dir == LEFT) ? RANGE_OF_VISION : 0));
84   bool inReach_right = (ob.p1.x <= mb.p2.x+((dir == RIGHT) ? RANGE_OF_VISION : 0));
85   bool inReach_top = (ob.p2.y >= mb.p1.y);
86   bool inReach_bottom = (ob.p1.y <= mb.p2.y);
87
88   return (inReach_left && inReach_right && inReach_top && inReach_bottom);
89 }
90
91 void
92 Igel::active_update(float elapsed_time)
93 {
94   switch (state) {
95
96     case STATE_NORMAL:
97       if (might_fall()) {
98         // turn around when we are at a ledge
99         turn_around();
100       } 
101       else if (!turn_recover_timer.started()) {
102         // turn around when we see a Bullet
103         Sector* sector = Sector::current();
104         for (Sector::GameObjects::iterator i = sector->gameobjects.begin(); i != sector->gameobjects.end(); ++i) {
105           Bullet* bullet = dynamic_cast<Bullet*>(*i);
106           if (bullet) {
107             if (can_see(*bullet)) turn_around();
108           }
109         }
110       }
111       break;
112
113   }
114
115   BadGuy::active_update(elapsed_time);
116 }
117
118 void
119 Igel::collision_solid(const CollisionHit& hit)
120 {
121   if(hit.top || hit.bottom) { // floor or roof
122     physic.set_velocity_y(0);
123     return;
124   }
125
126   // hit left or right
127   switch(state) {
128     case STATE_NORMAL:
129       turn_around();
130       break;
131   }
132 }
133
134 HitResponse
135 Igel::collision_badguy(BadGuy& , const CollisionHit& hit)
136 {
137   if(hit.top || hit.bottom) { // floor or roof
138     physic.set_velocity_y(0);
139     return CONTINUE;
140   }
141
142   // hit left or right
143   switch(state) {
144
145     case STATE_NORMAL:
146       turn_around();
147       break;
148
149   }
150
151   return CONTINUE;
152 }
153
154 HitResponse
155 Igel::collision_bullet(Bullet& , const CollisionHit& hit)
156 {
157   // die if hit on front side
158   if (((dir == LEFT) && hit.left) || ((dir == RIGHT) && hit.right)) {
159     kill_fall();
160     return ABORT_MOVE;
161   }
162
163   // else ignore bullet
164   return FORCE_MOVE;
165 }
166
167 bool
168 Igel::collision_squished(Player& )
169 {
170   switch(state) {
171
172     case STATE_NORMAL:
173       // this will hurt
174       return false;
175       break;
176
177   }
178
179   kill_fall();
180   return true;
181 }
182
183 IMPLEMENT_FACTORY(Igel, "igel")