2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "object/platform.hpp"
19 #include "object/player.hpp"
20 #include "scripting/platform.hpp"
21 #include "scripting/squirrel_util.hpp"
22 #include "supertux/object_factory.hpp"
23 #include "supertux/sector.hpp"
24 #include "util/reader.hpp"
26 Platform::Platform(const Reader& reader) :
27 MovingSprite(reader, Vector(0,0), LAYER_OBJECTS, COLGROUP_STATIC),
32 player_contact(false),
33 last_player_contact(false)
36 reader.get("name", name);
37 reader.get("running", running);
38 if ((name == "") && (!running)) automatic=true;
39 const lisp::Lisp* pathLisp = reader.get_lisp("path");
41 throw std::runtime_error("No path specified for platform");
42 path.reset(new Path());
43 path->read(*pathLisp);
44 walker.reset(new PathWalker(path.get(), running));
45 bbox.set_pos(path->get_base());
49 Platform::Platform(const Platform& other) :
51 ScriptInterface(other),
53 automatic(other.automatic),
54 player_contact(false),
55 last_player_contact(false)
58 path.reset(new Path(*other.path));
59 walker.reset(new PathWalker(*other.walker));
60 walker->path = &*path;
65 Platform::collision(GameObject& other, const CollisionHit& )
67 if (dynamic_cast<Player*>(&other)) player_contact = true;
72 Platform::update(float elapsed_time)
74 // check if Platform should automatically pick a destination
77 if (!player_contact && !walker->is_moving()) {
78 // Player doesn't touch platform and Platform is not moving
80 // Travel to node nearest to nearest player
81 // FIXME: does not really use nearest player
83 std::vector<Player*> players = Sector::current()->get_players();
84 for (std::vector<Player*>::iterator playerIter = players.begin(); playerIter != players.end(); ++playerIter) {
88 int nearest_node_id = path->get_nearest_node_no(player->get_bbox().p2);
89 if (nearest_node_id != -1) {
90 goto_node(nearest_node_id);
95 if (player_contact && !last_player_contact && !walker->is_moving()) {
96 // Player touched platform, didn't touch last frame and Platform is not moving
98 // Travel to node farthest from current position
99 int farthest_node_id = path->get_farthest_node_no(get_pos());
100 if (farthest_node_id != -1) {
101 goto_node(farthest_node_id);
105 // Clear player_contact flag set by collision() method
106 last_player_contact = player_contact;
107 player_contact = false;
110 movement = walker->advance(elapsed_time) - get_pos();
111 speed = movement / elapsed_time;
115 Platform::goto_node(int node_no)
117 walker->goto_node(node_no);
121 Platform::start_moving()
123 walker->start_moving();
127 Platform::stop_moving()
129 walker->stop_moving();
133 Platform::expose(HSQUIRRELVM vm, SQInteger table_idx)
135 if (name.empty()) return;
136 Scripting::Platform* interface = new Scripting::Platform(this);
137 expose_object(vm, table_idx, interface, name, true);
141 Platform::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
143 if (name.empty()) return;
144 Scripting::unexpose_object(vm, table_idx, name);
147 IMPLEMENT_FACTORY(Platform, "platform");