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