Renamed namespaces to all lowercase
[supertux.git] / src / object / scripted_object.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "object/scripted_object.hpp"
18
19 #include <stdio.h>
20
21 #include "scripting/squirrel_util.hpp"
22 #include "sprite/sprite.hpp"
23 #include "supertux/object_factory.hpp"
24 #include "util/reader.hpp"
25
26 ScriptedObject::ScriptedObject(const Reader& lisp) :
27   MovingSprite(lisp, LAYER_OBJECTS, COLGROUP_MOVING_STATIC),
28   physic(),
29   name(),
30   solid(true), 
31   physic_enabled(true), 
32   visible(true), 
33   new_vel_set(false),
34   new_vel()
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 void
147 ScriptedObject::set_action(const std::string& animation)
148 {
149   sprite->set_action(animation);
150 }
151
152 std::string
153 ScriptedObject::get_action()
154 {
155   return sprite->get_action();
156 }
157
158 std::string
159 ScriptedObject::get_name()
160 {
161   return name;
162 }
163
164 void
165 ScriptedObject::update(float elapsed_time)
166 {
167   if(!physic_enabled)
168     return;
169
170   if(new_vel_set) {
171     physic.set_velocity(new_vel.x, new_vel.y);
172     new_vel_set = false;
173   }
174   movement = physic.get_movement(elapsed_time);
175 }
176
177 void
178 ScriptedObject::draw(DrawingContext& context)
179 {
180   if(!visible)
181     return;
182
183   sprite->draw(context, get_pos(), layer);
184 }
185
186 void
187 ScriptedObject::collision_solid(const CollisionHit& hit)
188 {
189   if(!physic_enabled)
190     return;
191
192   if(hit.bottom) {
193     if(physic.get_velocity_y() > 0)
194       physic.set_velocity_y(0);
195   } else if(hit.top) {
196     physic.set_velocity_y(.1f);
197   }
198
199   if(hit.left || hit.right) {
200     physic.set_velocity_x(0);
201   }
202 }
203
204 HitResponse
205 ScriptedObject::collision(GameObject& , const CollisionHit& )
206 {
207   return FORCE_MOVE;
208 }
209
210 IMPLEMENT_FACTORY(ScriptedObject, "scriptedobject");
211
212 /* EOF */