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),
29 automatic(false), player_contact(false), last_player_contact(false)
32 reader.get("name", name);
33 reader.get("running", running);
34 if ((name == "") && (!running)) automatic=true;
35 const lisp::Lisp* pathLisp = reader.get_lisp("path");
37 throw std::runtime_error("No path specified for platform");
38 path.reset(new Path());
39 path->read(*pathLisp);
40 walker.reset(new PathWalker(path.get(), running));
41 bbox.set_pos(path->get_base());
45 Platform::Platform(const Platform& other) :
47 ScriptInterface(other),
49 automatic(other.automatic),
50 player_contact(false),
51 last_player_contact(false)
54 path.reset(new Path(*other.path));
55 walker.reset(new PathWalker(*other.walker));
56 walker->path = &*path;
61 Platform::collision(GameObject& other, const CollisionHit& )
63 if (dynamic_cast<Player*>(&other)) player_contact = true;
68 Platform::update(float elapsed_time)
70 // check if Platform should automatically pick a destination
73 if (!player_contact && !walker->is_moving()) {
74 // Player doesn't touch platform and Platform is not moving
76 // Travel to node nearest to nearest player
77 // FIXME: does not really use nearest player
79 std::vector<Player*> players = Sector::current()->get_players();
80 for (std::vector<Player*>::iterator playerIter = players.begin(); playerIter != players.end(); ++playerIter) {
84 int nearest_node_id = path->get_nearest_node_no(player->get_bbox().p2);
85 if (nearest_node_id != -1) {
86 goto_node(nearest_node_id);
91 if (player_contact && !last_player_contact && !walker->is_moving()) {
92 // Player touched platform, didn't touch last frame and Platform is not moving
94 // Travel to node farthest from current position
95 int farthest_node_id = path->get_farthest_node_no(get_pos());
96 if (farthest_node_id != -1) {
97 goto_node(farthest_node_id);
101 // Clear player_contact flag set by collision() method
102 last_player_contact = player_contact;
103 player_contact = false;
106 movement = walker->advance(elapsed_time) - get_pos();
107 speed = movement / elapsed_time;
111 Platform::goto_node(int node_no)
113 walker->goto_node(node_no);
117 Platform::start_moving()
119 walker->start_moving();
123 Platform::stop_moving()
125 walker->stop_moving();
129 Platform::expose(HSQUIRRELVM vm, SQInteger table_idx)
131 if (name.empty()) return;
132 Scripting::Platform* interface = new Scripting::Platform(this);
133 expose_object(vm, table_idx, interface, name, true);
137 Platform::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
139 if (name.empty()) return;
140 Scripting::unexpose_object(vm, table_idx, name);
143 IMPLEMENT_FACTORY(Platform, "platform");