13e5f5f7594a4a730f6a61039c1866dde3196856
[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 #include "sprite/sprite.hpp"
31
32 ScriptedObject::ScriptedObject(const lisp::Lisp& lisp)
33   : MovingSprite(lisp, LAYER_OBJECTS, COLGROUP_MOVING_STATIC),
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     set_group( COLGROUP_MOVING_STATIC );
53   } else {
54     set_group( COLGROUP_DISABLED );
55   }
56 }
57
58 void
59 ScriptedObject::expose(HSQUIRRELVM vm, SQInteger table_idx)
60 {
61   if (name.empty()) return;
62   expose_object(vm, table_idx, dynamic_cast<Scripting::ScriptedObject *>(this), name, false);
63 }
64
65 void
66 ScriptedObject::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
67 {
68   if (name.empty()) return;
69   Scripting::unexpose_object(vm, table_idx, name);
70 }
71
72 void
73 ScriptedObject::move(float x, float y)
74 {
75   bbox.move(Vector(x, y));
76 }
77
78 void
79 ScriptedObject::set_pos(float x, float y)
80 {
81   printf("SetPos: %f %f\n", x, y);
82   bbox.set_pos(Vector(x, y));
83   physic.reset();
84 }
85
86 float
87 ScriptedObject::get_pos_x()
88 {
89   return get_pos().x;
90 }
91
92 float
93 ScriptedObject::get_pos_y()
94 {
95   return get_pos().y;
96 }
97
98 void
99 ScriptedObject::set_velocity(float x, float y)
100 {
101   new_vel = Vector(x, y);
102   new_vel_set = true;
103 }
104
105 float
106 ScriptedObject::get_velocity_x()
107 {
108   return physic.get_velocity_x();
109 }
110
111 float
112 ScriptedObject::get_velocity_y()
113 {
114   return physic.get_velocity_y();
115 }
116
117 void
118 ScriptedObject::set_visible(bool visible)
119 {
120   this->visible = visible;
121 }
122
123 bool
124 ScriptedObject::is_visible()
125 {
126   return visible;
127 }
128
129 void
130 ScriptedObject::set_solid(bool solid)
131 {
132   this->solid = solid;
133   if( solid ){
134     set_group( COLGROUP_MOVING_STATIC );
135   } else {
136     set_group( COLGROUP_DISABLED );
137   }
138 }
139
140 bool
141 ScriptedObject::is_solid()
142 {
143   return solid;
144 }
145
146
147 void
148 ScriptedObject::set_action(const std::string& animation)
149 {
150   sprite->set_action(animation);
151 }
152
153 std::string
154 ScriptedObject::get_action()
155 {
156   return sprite->get_action();
157 }
158
159 std::string
160 ScriptedObject::get_name()
161 {
162   return name;
163 }
164
165 void
166 ScriptedObject::update(float elapsed_time)
167 {
168   if(!physic_enabled)
169     return;
170
171   if(new_vel_set) {
172     physic.set_velocity(new_vel.x, new_vel.y);
173     new_vel_set = false;
174   }
175   movement = physic.get_movement(elapsed_time);
176 }
177
178 void
179 ScriptedObject::draw(DrawingContext& context)
180 {
181   if(!visible)
182     return;
183
184   sprite->draw(context, get_pos(), layer);
185 }
186
187 void
188 ScriptedObject::collision_solid(const CollisionHit& hit)
189 {
190   if(!physic_enabled)
191     return;
192
193   if(hit.bottom) {
194     if(physic.get_velocity_y() > 0)
195       physic.set_velocity_y(0);
196   } else if(hit.top) {
197     physic.set_velocity_y(.1f);
198   }
199
200   if(hit.left || hit.right) {
201     physic.set_velocity_x(0);
202   }
203 }
204
205 HitResponse
206 ScriptedObject::collision(GameObject& , const CollisionHit& )
207 {
208   return FORCE_MOVE;
209 }
210
211 IMPLEMENT_FACTORY(ScriptedObject, "scriptedobject");