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