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