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