1 // $Id: igel.cpp 3478 2006-04-30 23:14:15Z sommer $
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"
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 */
33 Igel::Igel(const lisp::Lisp& reader)
34 : BadGuy(reader, "images/creatures/igel/igel.sprite"), state(STATE_NORMAL)
36 set_direction = false;
39 Igel::Igel(const Vector& pos, Direction d)
40 : BadGuy(pos, "images/creatures/igel/igel.sprite"), state(STATE_NORMAL)
43 initial_direction = d;
47 Igel::write(lisp::Writer& writer)
49 writer.start_list("igel");
51 writer.write_float("x", start_position.x);
52 writer.write_float("y", start_position.y);
54 writer.end_list("igel");
60 if (set_direction) {dir = initial_direction;}
69 sprite->set_action(dir == LEFT ? "walking-left" : "walking-right");
71 physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
77 dir = (dir == LEFT ? RIGHT : LEFT);
78 turn_recover_timer.start(TURN_RECOVER_TIME);
83 Igel::can_see(const MovingObject& o)
86 Rect ob = o.get_bbox();
88 bool inReach_left = (ob.p2.x >= mb.p1.x-((dir == LEFT) ? RANGE_OF_VISION : 0));
89 bool inReach_right = (ob.p1.x <= mb.p2.x+((dir == RIGHT) ? RANGE_OF_VISION : 0));
90 bool inReach_top = (ob.p2.y >= mb.p1.y);
91 bool inReach_bottom = (ob.p1.y <= mb.p2.y);
93 return (inReach_left && inReach_right && inReach_top && inReach_bottom);
97 Igel::active_update(float elapsed_time)
103 // turn around when we are at a ledge
106 else if (!turn_recover_timer.started()) {
107 // turn around when we see a Bullet
108 Sector* sector = Sector::current();
109 for (Sector::GameObjects::iterator i = sector->gameobjects.begin(); i != sector->gameobjects.end(); ++i) {
110 Bullet* bullet = dynamic_cast<Bullet*>(*i);
112 if (can_see(*bullet)) turn_around();
120 BadGuy::active_update(elapsed_time);
124 Igel::collision_solid(GameObject& , const CollisionHit& hit)
126 if(fabsf(hit.normal.y) > .5) { // floor or roof
127 physic.set_velocity_y(0);
144 Igel::collision_badguy(BadGuy& , const CollisionHit& hit)
146 if(fabsf(hit.normal.y) > .5) { // floor or roof
147 physic.set_velocity_y(0);
164 Igel::collision_bullet(Bullet& , const CollisionHit& hit)
166 // die if hit on front side
167 if (((dir == LEFT) && (hit.normal.x > 0)) || ((dir == RIGHT) && (hit.normal.x < 0))) {
172 // else ignore bullet
177 Igel::collision_squished(Player& )
192 IMPLEMENT_FACTORY(Igel, "igel")