4151cc541c7ba67aa9f763711ac65800c5c69092
[supertux.git] / src / object / platform.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
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.
10 //
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.
15 //
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.
19
20 #include <config.h>
21
22 #include "platform.hpp"
23
24 #include <stdexcept>
25 #include "log.hpp"
26 #include "video/drawing_context.hpp"
27 #include "resources.hpp"
28 #include "player.hpp"
29 #include "path.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"
36 #include "sector.hpp"
37
38 Platform::Platform(const lisp::Lisp& reader)
39         : MovingSprite(reader, Vector(0,0), LAYER_OBJECTS, COLGROUP_STATIC), 
40         speed(Vector(0,0)), 
41         automatic(false), player_contact(false), last_player_contact(false)
42 {
43   bool running = true;
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");
48   if(pathLisp == NULL)
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());
54 }
55
56 Platform::Platform(const Platform& other)
57         : MovingSprite(other), ScriptInterface(other), 
58         speed(other.speed), 
59         automatic(other.automatic), player_contact(false), last_player_contact(false)
60 {
61   name = other.name;
62   path.reset(new Path(*other.path));
63   walker.reset(new PathWalker(*other.walker));
64   walker->path = &*path;
65 }
66
67 HitResponse
68 Platform::collision(GameObject& other, const CollisionHit& )
69 {
70   if (dynamic_cast<Player*>(&other)) player_contact = true;
71   return FORCE_MOVE;
72 }
73
74 void
75 Platform::update(float elapsed_time)
76 {
77   // check if Platform should automatically pick a destination
78   if (automatic) {
79
80     if (!player_contact && !walker->is_moving()) {
81       // Player doesn't touch platform and Platform is not moving
82
83       // Travel to node nearest to nearest player
84       // FIXME: does not really use nearest player
85       Player* player = 0;      
86       std::vector<Player*> players = Sector::current()->get_players();
87       for (std::vector<Player*>::iterator playerIter = players.begin(); playerIter != players.end(); ++playerIter) {
88         player = *playerIter;
89       }
90       if (player) {
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);
94         }
95       }
96     } 
97
98     if (player_contact && !last_player_contact && !walker->is_moving()) {
99       // Player touched platform, didn't touch last frame and Platform is not moving
100
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);
105       } 
106     }
107
108     // Clear player_contact flag set by collision() method
109     last_player_contact = player_contact;
110     player_contact = false;
111   }
112
113   movement = walker->advance(elapsed_time) - get_pos();
114   speed = movement / elapsed_time;
115 }
116
117 void
118 Platform::goto_node(int node_no)
119 {
120   walker->goto_node(node_no);
121 }
122
123 void
124 Platform::start_moving()
125 {
126   walker->start_moving();
127 }
128
129 void
130 Platform::stop_moving()
131 {
132   walker->stop_moving();
133 }
134
135 void
136 Platform::expose(HSQUIRRELVM vm, SQInteger table_idx)
137 {
138   if (name.empty()) return;
139   Scripting::Platform* interface = new Scripting::Platform(this);
140   expose_object(vm, table_idx, interface, name, true);
141 }
142
143 void
144 Platform::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
145 {
146   if (name.empty()) return;
147   Scripting::unexpose_object(vm, table_idx, name);
148 }
149
150 IMPLEMENT_FACTORY(Platform, "platform");