Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / object / platform.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
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.
8 //
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.
13 //
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/>.
16
17 #include "object/platform.hpp"
18
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"
25
26 Platform::Platform(const Reader& reader)
27   : MovingSprite(reader, Vector(0,0), LAYER_OBJECTS, COLGROUP_STATIC), 
28     speed(Vector(0,0)), 
29     automatic(false), player_contact(false), last_player_contact(false)
30 {
31   bool running = true;
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");
36   if(pathLisp == NULL)
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());
42 }
43
44 /*
45   Platform::Platform(const Platform& other) :
46   MovingSprite(other), 
47   ScriptInterface(other), 
48   speed(other.speed), 
49   automatic(other.automatic), 
50   player_contact(false), 
51   last_player_contact(false)
52   {
53   name = other.name;
54   path.reset(new Path(*other.path));
55   walker.reset(new PathWalker(*other.walker));
56   walker->path = &*path;
57   }
58 */
59
60 HitResponse
61 Platform::collision(GameObject& other, const CollisionHit& )
62 {
63   if (dynamic_cast<Player*>(&other)) player_contact = true;
64   return FORCE_MOVE;
65 }
66
67 void
68 Platform::update(float elapsed_time)
69 {
70   // check if Platform should automatically pick a destination
71   if (automatic) {
72
73     if (!player_contact && !walker->is_moving()) {
74       // Player doesn't touch platform and Platform is not moving
75
76       // Travel to node nearest to nearest player
77       // FIXME: does not really use nearest player
78       Player* player = 0;      
79       std::vector<Player*> players = Sector::current()->get_players();
80       for (std::vector<Player*>::iterator playerIter = players.begin(); playerIter != players.end(); ++playerIter) {
81         player = *playerIter;
82       }
83       if (player) {
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);
87         }
88       }
89     } 
90
91     if (player_contact && !last_player_contact && !walker->is_moving()) {
92       // Player touched platform, didn't touch last frame and Platform is not moving
93
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);
98       } 
99     }
100
101     // Clear player_contact flag set by collision() method
102     last_player_contact = player_contact;
103     player_contact = false;
104   }
105
106   movement = walker->advance(elapsed_time) - get_pos();
107   speed = movement / elapsed_time;
108 }
109
110 void
111 Platform::goto_node(int node_no)
112 {
113   walker->goto_node(node_no);
114 }
115
116 void
117 Platform::start_moving()
118 {
119   walker->start_moving();
120 }
121
122 void
123 Platform::stop_moving()
124 {
125   walker->stop_moving();
126 }
127
128 void
129 Platform::expose(HSQUIRRELVM vm, SQInteger table_idx)
130 {
131   if (name.empty()) return;
132   Scripting::Platform* interface = new Scripting::Platform(this);
133   expose_object(vm, table_idx, interface, name, true);
134 }
135
136 void
137 Platform::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
138 {
139   if (name.empty()) return;
140   Scripting::unexpose_object(vm, table_idx, name);
141 }
142
143 IMPLEMENT_FACTORY(Platform, "platform");
144
145 /* EOF */