00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "object/ispy.hpp"
00018
00019 #include "object/player.hpp"
00020 #include "object/tilemap.hpp"
00021 #include "sprite/sprite.hpp"
00022 #include "supertux/object_factory.hpp"
00023 #include "supertux/sector.hpp"
00024 #include "supertux/tile.hpp"
00025 #include "util/reader.hpp"
00026
00027 #include <sstream>
00028
00029 Ispy::Ispy(const Reader& reader) :
00030 MovingSprite(reader, "images/objects/ispy/ispy.sprite", LAYER_TILES+5, COLGROUP_DISABLED),
00031 state(ISPYSTATE_IDLE),
00032 script(),
00033 dir(AUTO)
00034 {
00035
00036 reader.get("script", script);
00037
00038
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
00049 sprite->set_action((dir == DOWN) ? "idle-down" : ((dir == LEFT) ? "idle-left" : "idle-right"));
00050 }
00051
00052 HitResponse
00053 Ispy::collision(GameObject& , const CollisionHit& )
00054 {
00055 return ABORT_MOVE;
00056 }
00057
00058 bool
00059 Ispy::line_intersects_line(Vector line1_start, Vector line1_end, Vector line2_start, Vector line2_end) {
00060
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
00070 if (num < 0) {
00071 num =- num;
00072 den1 =- den1;
00073 den2 =- den2;
00074 }
00075
00076
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
00091 return (den1>=0) && (den1<=num) && (den2>=0) && (den2<=num);
00092
00093 }
00094
00095 bool
00096 Ispy::intersects_line(Rectf r, Vector line_start, Vector line_end)
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 }
00108
00109 bool
00110 Ispy::free_line_of_sight(Vector line_start, Vector line_end, const MovingObject* ignore_object)
00111 {
00112
00113
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
00126 if((tile->getAttributes() & Tile::SOLID)) return false;
00127 }
00128 }
00129 }
00130
00131
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 }
00148
00149 void
00150 Ispy::update(float )
00151 {
00152
00153 if (state == ISPYSTATE_IDLE) {
00154
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
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 }
00201
00202