Removed trailing whitespace from all *.?pp files
[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   path(),
29   walker(),
30   speed(Vector(0,0)),
31   automatic(false),
32   player_contact(false),
33   last_player_contact(false)
34 {
35   bool running = true;
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");
40   if(pathLisp == NULL)
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());
46 }
47
48 /*
49   Platform::Platform(const Platform& other) :
50   MovingSprite(other),
51   ScriptInterface(other),
52   speed(other.speed),
53   automatic(other.automatic),
54   player_contact(false),
55   last_player_contact(false)
56   {
57   name = other.name;
58   path.reset(new Path(*other.path));
59   walker.reset(new PathWalker(*other.walker));
60   walker->path = &*path;
61   }
62 */
63
64 HitResponse
65 Platform::collision(GameObject& other, const CollisionHit& )
66 {
67   if (dynamic_cast<Player*>(&other)) player_contact = true;
68   return FORCE_MOVE;
69 }
70
71 void
72 Platform::update(float elapsed_time)
73 {
74   // check if Platform should automatically pick a destination
75   if (automatic) {
76
77     if (!player_contact && !walker->is_moving()) {
78       // Player doesn't touch platform and Platform is not moving
79
80       // Travel to node nearest to nearest player
81       // FIXME: does not really use nearest player
82       Player* player = 0;
83       std::vector<Player*> players = Sector::current()->get_players();
84       for (std::vector<Player*>::iterator playerIter = players.begin(); playerIter != players.end(); ++playerIter) {
85         player = *playerIter;
86       }
87       if (player) {
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);
91         }
92       }
93     }
94
95     if (player_contact && !last_player_contact && !walker->is_moving()) {
96       // Player touched platform, didn't touch last frame and Platform is not moving
97
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);
102       }
103     }
104
105     // Clear player_contact flag set by collision() method
106     last_player_contact = player_contact;
107     player_contact = false;
108   }
109
110   movement = walker->advance(elapsed_time) - get_pos();
111   speed = movement / elapsed_time;
112 }
113
114 void
115 Platform::goto_node(int node_no)
116 {
117   walker->goto_node(node_no);
118 }
119
120 void
121 Platform::start_moving()
122 {
123   walker->start_moving();
124 }
125
126 void
127 Platform::stop_moving()
128 {
129   walker->stop_moving();
130 }
131
132 void
133 Platform::expose(HSQUIRRELVM vm, SQInteger table_idx)
134 {
135   if (name.empty()) return;
136   scripting::Platform* _this = new scripting::Platform(this);
137   expose_object(vm, table_idx, _this, name, true);
138 }
139
140 void
141 Platform::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
142 {
143   if (name.empty()) return;
144   scripting::unexpose_object(vm, table_idx, name);
145 }
146
147 /* EOF */