00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "object/platform.hpp"
00018
00019 #include "object/player.hpp"
00020 #include "scripting/platform.hpp"
00021 #include "scripting/squirrel_util.hpp"
00022 #include "supertux/object_factory.hpp"
00023 #include "supertux/sector.hpp"
00024 #include "util/reader.hpp"
00025
00026 Platform::Platform(const Reader& reader) :
00027 MovingSprite(reader, Vector(0,0), LAYER_OBJECTS, COLGROUP_STATIC),
00028 path(),
00029 walker(),
00030 speed(Vector(0,0)),
00031 automatic(false),
00032 player_contact(false),
00033 last_player_contact(false)
00034 {
00035 bool running = true;
00036 reader.get("name", name);
00037 reader.get("running", running);
00038 if ((name == "") && (!running)) automatic=true;
00039 const lisp::Lisp* pathLisp = reader.get_lisp("path");
00040 if(pathLisp == NULL)
00041 throw std::runtime_error("No path specified for platform");
00042 path.reset(new Path());
00043 path->read(*pathLisp);
00044 walker.reset(new PathWalker(path.get(), running));
00045 bbox.set_pos(path->get_base());
00046 }
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064 HitResponse
00065 Platform::collision(GameObject& other, const CollisionHit& )
00066 {
00067 if (dynamic_cast<Player*>(&other)) player_contact = true;
00068 return FORCE_MOVE;
00069 }
00070
00071 void
00072 Platform::update(float elapsed_time)
00073 {
00074
00075 if (automatic) {
00076
00077 if (!player_contact && !walker->is_moving()) {
00078
00079
00080
00081
00082 Player* player = 0;
00083 std::vector<Player*> players = Sector::current()->get_players();
00084 for (std::vector<Player*>::iterator playerIter = players.begin(); playerIter != players.end(); ++playerIter) {
00085 player = *playerIter;
00086 }
00087 if (player) {
00088 int nearest_node_id = path->get_nearest_node_no(player->get_bbox().p2);
00089 if (nearest_node_id != -1) {
00090 goto_node(nearest_node_id);
00091 }
00092 }
00093 }
00094
00095 if (player_contact && !last_player_contact && !walker->is_moving()) {
00096
00097
00098
00099 int farthest_node_id = path->get_farthest_node_no(get_pos());
00100 if (farthest_node_id != -1) {
00101 goto_node(farthest_node_id);
00102 }
00103 }
00104
00105
00106 last_player_contact = player_contact;
00107 player_contact = false;
00108 }
00109
00110 movement = walker->advance(elapsed_time) - get_pos();
00111 speed = movement / elapsed_time;
00112 }
00113
00114 void
00115 Platform::goto_node(int node_no)
00116 {
00117 walker->goto_node(node_no);
00118 }
00119
00120 void
00121 Platform::start_moving()
00122 {
00123 walker->start_moving();
00124 }
00125
00126 void
00127 Platform::stop_moving()
00128 {
00129 walker->stop_moving();
00130 }
00131
00132 void
00133 Platform::expose(HSQUIRRELVM vm, SQInteger table_idx)
00134 {
00135 if (name.empty()) return;
00136 scripting::Platform* _this = new scripting::Platform(this);
00137 expose_object(vm, table_idx, _this, name, true);
00138 }
00139
00140 void
00141 Platform::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
00142 {
00143 if (name.empty()) return;
00144 scripting::unexpose_object(vm, table_idx, name);
00145 }
00146
00147