Removed trailing whitespace from all *.?pp files
[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   layer = reader_get_layer (lisp, /* default = */ LAYER_OBJECTS);
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   MovingObject::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 bool
147 ScriptedObject::gravity_enabled() const
148 {
149         return physic.gravity_enabled();
150 }
151
152 void
153 ScriptedObject::enable_gravity(bool f)
154 {
155         physic.enable_gravity(f);
156 }
157
158 void
159 ScriptedObject::set_action(const std::string& animation)
160 {
161   sprite->set_action(animation);
162 }
163
164 std::string
165 ScriptedObject::get_action()
166 {
167   return sprite->get_action();
168 }
169
170 std::string
171 ScriptedObject::get_name()
172 {
173   return name;
174 }
175
176 void
177 ScriptedObject::update(float elapsed_time)
178 {
179   if(!physic_enabled)
180     return;
181
182   if(new_vel_set) {
183     physic.set_velocity(new_vel.x, new_vel.y);
184     new_vel_set = false;
185   }
186   movement = physic.get_movement(elapsed_time);
187 }
188
189 void
190 ScriptedObject::draw(DrawingContext& context)
191 {
192   if(!visible)
193     return;
194
195   sprite->draw(context, get_pos(), layer);
196 }
197
198 void
199 ScriptedObject::collision_solid(const CollisionHit& hit)
200 {
201   if(!physic_enabled)
202     return;
203
204   if(hit.bottom) {
205     if(physic.get_velocity_y() > 0)
206       physic.set_velocity_y(0);
207   } else if(hit.top) {
208     physic.set_velocity_y(.1f);
209   }
210
211   if(hit.left || hit.right) {
212     physic.set_velocity_x(0);
213   }
214 }
215
216 HitResponse
217 ScriptedObject::collision(GameObject& , const CollisionHit& )
218 {
219   return FORCE_MOVE;
220 }
221
222 /* EOF */