- Remove ManagedSoundSource
[supertux.git] / src / object / scripted_object.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 <stdexcept>
23 #include <math.h>
24
25 #include "scripted_object.hpp"
26 #include "video/drawing_context.hpp"
27 #include "scripting/squirrel_util.hpp"
28 #include "resources.hpp"
29 #include "object_factory.hpp"
30 #include "math/vector.hpp"
31
32 ScriptedObject::ScriptedObject(const lisp::Lisp& lisp)
33   : MovingSprite(lisp, LAYER_OBJECTS, COLGROUP_MOVING),
34     solid(true), physic_enabled(true), visible(true), new_vel_set(false)
35 {
36   lisp.get("name", name);
37   if(name == "")
38     throw std::runtime_error("Scripted object must have a name specified");
39   
40   // FIXME: do we need this? bbox is already set via .sprite file
41   float width = sprite->get_width();
42   float height = sprite->get_height();
43   lisp.get("width", width);
44   lisp.get("height", height);
45   bbox.set_size(width, height);
46
47   lisp.get("solid", solid);
48   lisp.get("physic-enabled", physic_enabled);
49   lisp.get("visible", visible);
50   lisp.get("z-pos", layer);
51   if(solid)
52     flags |= FLAG_SOLID;
53 }
54
55 void
56 ScriptedObject::expose(HSQUIRRELVM vm, SQInteger table_idx)
57 {
58   Scripting::ScriptedObject* interface = static_cast<Scripting::ScriptedObject*> (this);
59   expose_object(vm, table_idx, interface, name, false);
60 }
61
62 void
63 ScriptedObject::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
64 {
65   Scripting::unexpose_object(vm, table_idx, name);
66 }
67
68 void
69 ScriptedObject::move(float x, float y)
70 {
71   bbox.move(Vector(x, y));
72 }
73
74 void
75 ScriptedObject::set_pos(float x, float y)
76 {
77   printf("SetPos: %f %f\n", x, y);
78   bbox.set_pos(Vector(x, y));
79   physic.reset();
80 }
81
82 float
83 ScriptedObject::get_pos_x()
84 {
85   return get_pos().x;
86 }
87
88 float
89 ScriptedObject::get_pos_y()
90 {
91   return get_pos().y;
92 }
93
94 void
95 ScriptedObject::set_velocity(float x, float y)
96 {
97   new_vel = Vector(x, y);
98   new_vel_set = true;
99 }
100
101 float
102 ScriptedObject::get_velocity_x()
103 {
104   return physic.get_velocity_x();
105 }
106
107 float
108 ScriptedObject::get_velocity_y()
109 {
110   return physic.get_velocity_y();
111 }
112
113 void
114 ScriptedObject::set_visible(bool visible)
115 {
116   this->visible = visible;
117 }
118
119 bool
120 ScriptedObject::is_visible()
121 {
122   return visible;
123 }
124
125 void
126 ScriptedObject::set_action(const std::string& animation)
127 {
128   sprite->set_action(animation);
129 }
130
131 std::string
132 ScriptedObject::get_action()
133 {
134   return sprite->get_action();
135 }
136
137 std::string
138 ScriptedObject::get_name()
139 {
140   return name;
141 }
142
143 void
144 ScriptedObject::update(float elapsed_time)
145 {
146   if(!physic_enabled)
147     return;
148
149   if(new_vel_set) {
150     physic.set_velocity(new_vel.x, new_vel.y);
151     new_vel_set = false;
152   }
153   movement = physic.get_movement(elapsed_time);
154 }
155
156 void
157 ScriptedObject::draw(DrawingContext& context)
158 {
159   if(!visible)
160     return;
161
162   sprite->draw(context, get_pos(), layer);
163 }
164
165 HitResponse
166 ScriptedObject::collision(GameObject& other, const CollisionHit& hit)
167 {
168   if(!physic_enabled)
169     return FORCE_MOVE;
170
171   if(!(other.get_flags() & FLAG_SOLID))
172     return FORCE_MOVE;
173
174   if(other.get_flags() & FLAG_SOLID) {
175     if(hit.normal.y < 0) { // landed on floor
176       if(physic.get_velocity_y() < 0)
177         physic.set_velocity_y(0);
178     } else if(hit.normal.y > 0) { // bumped against roof
179       physic.set_velocity_y(.1);
180     }
181
182     if(fabsf(hit.normal.x) > .9) { // hit on side?
183       physic.set_velocity_x(0);
184     }
185         
186     return CONTINUE;
187   }
188
189   return FORCE_MOVE;
190 }
191
192 IMPLEMENT_FACTORY(ScriptedObject, "scriptedobject");