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