Ispy Class Reference

An Ispy: When it spots Tux, a script will run. More...

#include <ispy.hpp>

Inherits MovingSprite.

List of all members.

Public Member Functions

 Ispy (const Reader &lisp)
HitResponse collision (GameObject &other, const CollisionHit &hit)
 this function is called when the object collided with any other object
virtual void update (float elapsed_time)
 This function is called once per frame and allows the object to update it's state.

Private Types

enum  IspyState { ISPYSTATE_IDLE, ISPYSTATE_ALERT, ISPYSTATE_HIDING, ISPYSTATE_SHOWING }

Private Member Functions

bool line_intersects_line (Vector line1_start, Vector line1_end, Vector line2_start, Vector line2_end)
bool intersects_line (Rectf r, Vector line_start, Vector line_end)
bool free_line_of_sight (Vector p1, Vector p2, const MovingObject *ignore_object)

Private Attributes

IspyState state
 current state
std::string script
 script to execute when Tux is spotted
Direction dir


Detailed Description

An Ispy: When it spots Tux, a script will run.

Definition at line 26 of file ispy.hpp.


Member Enumeration Documentation

enum Ispy::IspyState [private]

Enumerator:
ISPYSTATE_IDLE 
ISPYSTATE_ALERT 
ISPYSTATE_HIDING 
ISPYSTATE_SHOWING 

Definition at line 40 of file ispy.hpp.

00040                  {
00041     ISPYSTATE_IDLE,
00042     ISPYSTATE_ALERT,
00043     ISPYSTATE_HIDING,
00044     ISPYSTATE_SHOWING
00045   };


Constructor & Destructor Documentation

Ispy::Ispy ( const Reader lisp  ) 

Definition at line 29 of file ispy.cpp.

References AUTO, dir, DOWN, lisp::Lisp::get(), LEFT, log_warning, RIGHT, script, and MovingSprite::sprite.

00029                                :
00030   MovingSprite(reader, "images/objects/ispy/ispy.sprite", LAYER_TILES+5, COLGROUP_DISABLED), 
00031   state(ISPYSTATE_IDLE), 
00032   script(),
00033   dir(AUTO)
00034 {
00035   // read script to execute
00036   reader.get("script", script);
00037 
00038   // read direction to face in
00039   std::string dir_str;
00040   bool facing_down = false;
00041   reader.get("direction", dir_str);
00042   if( dir_str == "left" ) dir = LEFT;
00043   if( dir_str == "right" ) dir = RIGHT;
00044   reader.get("facing-down", facing_down);
00045   if (facing_down) dir = DOWN;
00046   if (dir == AUTO) log_warning << "Setting an Ispy's direction to AUTO is no good idea" << std::endl;
00047 
00048   // set initial sprite action
00049   sprite->set_action((dir == DOWN) ? "idle-down" : ((dir == LEFT) ? "idle-left" : "idle-right"));
00050 }


Member Function Documentation

HitResponse Ispy::collision ( GameObject other,
const CollisionHit hit 
) [virtual]

this function is called when the object collided with any other object

Implements MovingObject.

Definition at line 53 of file ispy.cpp.

References ABORT_MOVE.

Referenced by free_line_of_sight().

00054 {
00055   return ABORT_MOVE;
00056 }

void Ispy::update ( float  elapsed_time  )  [virtual]

This function is called once per frame and allows the object to update it's state.

The elapsed_time is the time since the last frame in seconds and should be the base for all timed calculations (don't use SDL_GetTicks directly as this will fail in pause mode)

Reimplemented from MovingSprite.

Definition at line 150 of file ispy.cpp.

References Sector::current(), dir, DOWN, free_line_of_sight(), MovingObject::get_bbox(), Rectf::get_middle(), Sector::get_players(), ISPYSTATE_ALERT, ISPYSTATE_HIDING, ISPYSTATE_IDLE, ISPYSTATE_SHOWING, LEFT, RIGHT, Sector::run_script(), script, MovingSprite::sprite, state, and UP.

00151 {
00152 
00153   if (state == ISPYSTATE_IDLE) {
00154     // check if a player has been spotted
00155     bool playerSpotted = false;
00156     std::vector<Player*> players = Sector::current()->get_players();
00157     for (std::vector<Player*>::iterator playerIter = players.begin(); playerIter != players.end(); ++playerIter) {
00158       Player* player = *playerIter;
00159 
00160       Vector eye = get_bbox().get_middle();
00161       if (dir == LEFT) eye = Vector(get_bbox().p1.x, get_bbox().get_middle().y);
00162       if (dir == RIGHT) eye = Vector(get_bbox().p2.x, get_bbox().get_middle().y);
00163       if (dir == UP) eye = Vector(get_bbox().get_middle().x, get_bbox().p1.y);
00164       if (dir == DOWN) eye = Vector(get_bbox().get_middle().x, get_bbox().p2.y);
00165 
00166       // test for free line of sight to any of all four corners and the middle of a player's bounding box
00167       if (free_line_of_sight(eye, player->get_bbox().p1, player)) playerSpotted = true;
00168       if (free_line_of_sight(eye, Vector(player->get_bbox().p2.x, player->get_bbox().p1.y), player)) playerSpotted = true;
00169       if (free_line_of_sight(eye, player->get_bbox().p2, player)) playerSpotted = true;
00170       if (free_line_of_sight(eye, Vector(player->get_bbox().p1.x, player->get_bbox().p2.y), player)) playerSpotted = true;
00171       if (free_line_of_sight(eye, player->get_bbox().get_middle(), player)) playerSpotted = true;
00172     }
00173 
00174     if (playerSpotted) {
00175       sprite->set_action((dir == DOWN) ? "alert-down" : ((dir == LEFT) ? "alert-left" : "alert-right"), 1);
00176       state = ISPYSTATE_ALERT;
00177 
00178       std::istringstream stream(script);
00179       Sector::current()->run_script(stream, "Ispy");
00180     }
00181   }
00182   if (state == ISPYSTATE_ALERT) {
00183     if (sprite->animation_done()) {
00184       sprite->set_action((dir == DOWN) ? "hiding-down" : ((dir == LEFT) ? "hiding-left" : "hiding-right"), 1);
00185       state = ISPYSTATE_HIDING;
00186     }
00187   }
00188   if (state == ISPYSTATE_HIDING) {
00189     if (sprite->animation_done()) {
00190       sprite->set_action((dir == DOWN) ? "showing-down" : ((dir == LEFT) ? "showing-left" : "showing-right"), 1);
00191       state = ISPYSTATE_SHOWING;
00192     }
00193   }
00194   if (state == ISPYSTATE_SHOWING) {
00195     if (sprite->animation_done()) {
00196       sprite->set_action((dir == DOWN) ? "idle-down" : ((dir == LEFT) ? "idle-left" : "idle-right"));
00197       state = ISPYSTATE_IDLE;
00198     }
00199   }
00200 }

bool Ispy::line_intersects_line ( Vector  line1_start,
Vector  line1_end,
Vector  line2_start,
Vector  line2_end 
) [private]

Definition at line 59 of file ispy.cpp.

References Vector::x, and Vector::y.

Referenced by intersects_line().

00059                                                                                                      {
00060   // Adapted from Striker, (C) 1999 Joris van der Hoeven, GPL
00061 
00062   float a1 = line1_start.x, b1 = line1_start.y, a2 = line1_end.x, b2 = line1_end.y;
00063   float c1 = line2_start.x, d1 = line2_start.y, c2 = line2_end.x, d2 = line2_end.y;
00064 
00065   float num = (b2-b1)*(c2-c1) - (a2-a1)*(d2-d1);
00066   float den1 = (d2-b2)*(c1-c2) + (a2-c2)*(d1-d2);
00067   float den2 = (d2-b2)*(a1-a2) + (a2-c2)*(b1-b2);
00068 
00069   // normalize to positive numerator
00070   if (num < 0) { 
00071     num =- num; 
00072     den1 =- den1; 
00073     den2 =- den2; 
00074   }
00075 
00076   // numerator is zero -> Check for parallel or coinciding lines
00077   if (num == 0) {
00078     if ((b1-b2)*(c1-a2) != (a1-a2)*(d1-b2)) return false;
00079     if (a1 == a2) { 
00080       std::swap(a1, b1); 
00081       std::swap(a2, b2); 
00082       std::swap(c1, d1); 
00083       std::swap(c2, d2); 
00084     }
00085     if (a1 > a2) std::swap(a1, a2);
00086     if (c1 > c2) std::swap(c1, c2);
00087     return ((a1 <= c2) && (a2 >= c1));
00088   }
00089 
00090   // Standard check
00091   return (den1>=0) && (den1<=num) && (den2>=0) && (den2<=num);
00092 
00093 }

bool Ispy::intersects_line ( Rectf  r,
Vector  line_start,
Vector  line_end 
) [private]

Definition at line 96 of file ispy.cpp.

References line_intersects_line(), Rectf::p1, Rectf::p2, Vector::x, and Vector::y.

Referenced by free_line_of_sight().

00097 {
00098   Vector p1 = r.p1;
00099   Vector p2 = Vector(r.p2.x, r.p1.y);
00100   Vector p3 = r.p2;
00101   Vector p4 = Vector(r.p1.x, r.p2.y);
00102   if (line_intersects_line(p1, p2, line_start, line_end)) return true;
00103   if (line_intersects_line(p2, p3, line_start, line_end)) return true;
00104   if (line_intersects_line(p3, p4, line_start, line_end)) return true;
00105   if (line_intersects_line(p4, p1, line_start, line_end)) return true;
00106   return false;
00107 }

bool Ispy::free_line_of_sight ( Vector  p1,
Vector  p2,
const MovingObject ignore_object 
) [private]

Definition at line 110 of file ispy.cpp.

References COLGROUP_MOVING, COLGROUP_MOVING_STATIC, COLGROUP_STATIC, collision(), Sector::current(), Tile::getAttributes(), intersects_line(), Sector::moving_objects, Tile::SOLID, Sector::solid_tilemaps, Vector::x, and Vector::y.

Referenced by update().

00111 {
00112 
00113   // check if no tile is in the way
00114   float lsx = std::min(line_start.x, line_end.x);
00115   float lex = std::max(line_start.x, line_end.x);
00116   float lsy = std::min(line_start.y, line_end.y);
00117   float ley = std::max(line_start.y, line_end.y);
00118   std::list<TileMap*> solid_tilemaps = Sector::current()->solid_tilemaps;
00119   for (float test_x = lsx; test_x <= lex; test_x += 16) {
00120     for (float test_y = lsy; test_y <= ley; test_y += 16) {
00121       for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
00122         TileMap* solids = *i;
00123         const Tile* tile = solids->get_tile_at(Vector(test_x, test_y));
00124         if(!tile) continue;
00125         // FIXME: check collision with slope tiles
00126         if((tile->getAttributes() & Tile::SOLID)) return false;
00127       }
00128     }
00129   }
00130 
00131   // check if no object is in the way
00132   using namespace collision;
00133   Sector::MovingObjects& moving_objects = Sector::current()->moving_objects;
00134   for(Sector::MovingObjects::const_iterator i = moving_objects.begin();
00135       i != moving_objects.end(); ++i) {
00136     const MovingObject* moving_object = *i;
00137     if (moving_object == ignore_object) continue;
00138     if (!moving_object->is_valid()) continue;
00139     if ((moving_object->get_group() == COLGROUP_MOVING)
00140         || (moving_object->get_group() == COLGROUP_MOVING_STATIC)
00141         || (moving_object->get_group() == COLGROUP_STATIC)) {
00142       if(intersects_line(moving_object->get_bbox(), line_start, line_end)) return false;
00143     }
00144   }
00145 
00146   return true;
00147 }


Member Data Documentation

IspyState Ispy::state [private]

current state

Definition at line 46 of file ispy.hpp.

Referenced by update().

std::string Ispy::script [private]

script to execute when Tux is spotted

Definition at line 48 of file ispy.hpp.

Referenced by Ispy(), and update().

Direction Ispy::dir [private]

Definition at line 49 of file ispy.hpp.

Referenced by Ispy(), and update().


The documentation for this class was generated from the following files:
Generated on Mon Jun 9 03:38:31 2014 for SuperTux by  doxygen 1.5.1