Reverted bigger parts of tuxdev patch:
[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
37 Platform::Platform(const lisp::Lisp& reader)
38         : MovingSprite(reader, Vector(0,0), LAYER_OBJECTS, COLGROUP_STATIC), speed(Vector(0,0))
39 {
40   bool running = true;
41   reader.get("running", running);
42   const lisp::Lisp* pathLisp = reader.get_lisp("path");
43   if(pathLisp == NULL)
44     throw std::runtime_error("No path specified for platform");
45   path.reset(new Path());
46   path->read(*pathLisp);
47   walker.reset(new PathWalker(path.get(), running));
48   bbox.set_pos(path->get_base());
49 }
50
51 Platform::Platform(const Platform& other)
52         : MovingSprite(other), ScriptInterface(other), speed(other.speed)
53 {
54   name = other.name;
55   path.reset(new Path(*other.path));
56   walker.reset(new PathWalker(*other.walker));
57   walker->path = &*path;
58 }
59
60 HitResponse
61 Platform::collision(GameObject& , const CollisionHit& )
62 {
63   return FORCE_MOVE;
64 }
65
66 void
67 Platform::update(float elapsed_time)
68 {
69   movement = walker->advance(elapsed_time) - get_pos();
70   speed = movement / elapsed_time;
71 }
72
73 void
74 Platform::goto_node(int node_no)
75 {
76   walker->goto_node(node_no);
77 }
78
79 void
80 Platform::start_moving()
81 {
82   walker->start_moving();
83 }
84
85 void
86 Platform::stop_moving()
87 {
88   walker->stop_moving();
89 }
90
91 void
92 Platform::expose(HSQUIRRELVM vm, SQInteger table_idx)
93 {
94   if (name.empty()) return;
95   Scripting::Platform* interface = new Scripting::Platform(this);
96   expose_object(vm, table_idx, interface, name, true);
97 }
98
99 void
100 Platform::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
101 {
102   if (name.empty()) return;
103   Scripting::unexpose_object(vm, table_idx, name);
104 }
105
106 IMPLEMENT_FACTORY(Platform, "platform");