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