4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include "platform.hpp"
26 #include "video/drawing_context.hpp"
27 #include "resources.hpp"
30 #include "path_walker.hpp"
31 #include "sprite/sprite.hpp"
32 #include "lisp/lisp.hpp"
33 #include "object_factory.hpp"
34 #include "scripting/platform.hpp"
35 #include "scripting/squirrel_util.hpp"
38 Platform::Platform(const lisp::Lisp& reader)
39 : MovingSprite(reader, Vector(0,0), LAYER_OBJECTS, COLGROUP_STATIC),
41 automatic(false), player_contact(false), last_player_contact(false)
44 reader.get("name", name);
45 reader.get("running", running);
46 if ((name == "") && (!running)) automatic=true;
47 const lisp::Lisp* pathLisp = reader.get_lisp("path");
49 throw std::runtime_error("No path specified for platform");
50 path.reset(new Path());
51 path->read(*pathLisp);
52 walker.reset(new PathWalker(path.get(), running));
53 bbox.set_pos(path->get_base());
56 Platform::Platform(const Platform& other)
57 : MovingSprite(other), ScriptInterface(other),
59 automatic(other.automatic), player_contact(false), last_player_contact(false)
62 path.reset(new Path(*other.path));
63 walker.reset(new PathWalker(*other.walker));
64 walker->path = &*path;
68 Platform::collision(GameObject& other, const CollisionHit& )
70 if (dynamic_cast<Player*>(&other)) player_contact = true;
75 Platform::update(float elapsed_time)
77 // check if Platform should automatically pick a destination
80 if (!player_contact && !walker->is_moving()) {
81 // Player doesn't touch platform and Platform is not moving
83 // Travel to node nearest to nearest player
84 // FIXME: does not really use nearest player
86 std::vector<Player*> players = Sector::current()->get_players();
87 for (std::vector<Player*>::iterator playerIter = players.begin(); playerIter != players.end(); ++playerIter) {
91 int nearest_node_id = path->get_nearest_node_no(player->get_bbox().p2);
92 if (nearest_node_id != -1) {
93 goto_node(nearest_node_id);
98 if (player_contact && !last_player_contact && !walker->is_moving()) {
99 // Player touched platform, didn't touch last frame and Platform is not moving
101 // Travel to node farthest from current position
102 int farthest_node_id = path->get_farthest_node_no(get_pos());
103 if (farthest_node_id != -1) {
104 goto_node(farthest_node_id);
108 // Clear player_contact flag set by collision() method
109 last_player_contact = player_contact;
110 player_contact = false;
113 movement = walker->advance(elapsed_time) - get_pos();
114 speed = movement / elapsed_time;
118 Platform::goto_node(int node_no)
120 walker->goto_node(node_no);
124 Platform::start_moving()
126 walker->start_moving();
130 Platform::stop_moving()
132 walker->stop_moving();
136 Platform::expose(HSQUIRRELVM vm, SQInteger table_idx)
138 if (name.empty()) return;
139 Scripting::Platform* interface = new Scripting::Platform(this);
140 expose_object(vm, table_idx, interface, name, true);
144 Platform::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
146 if (name.empty()) return;
147 Scripting::unexpose_object(vm, table_idx, name);
150 IMPLEMENT_FACTORY(Platform, "platform");