2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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.
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.
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/>.
17 #include "object/scripted_object.hpp"
21 #include "scripting/squirrel_util.hpp"
22 #include "sprite/sprite.hpp"
23 #include "supertux/object_factory.hpp"
24 #include "util/reader.hpp"
26 ScriptedObject::ScriptedObject(const Reader& lisp) :
27 MovingSprite(lisp, LAYER_OBJECTS, COLGROUP_MOVING_STATIC),
36 lisp.get("name", name);
38 throw std::runtime_error("Scripted object must have a name specified");
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);
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);
52 set_group( COLGROUP_MOVING_STATIC );
54 set_group( COLGROUP_DISABLED );
59 ScriptedObject::expose(HSQUIRRELVM vm, SQInteger table_idx)
61 if (name.empty()) return;
62 expose_object(vm, table_idx, dynamic_cast<scripting::ScriptedObject *>(this), name, false);
66 ScriptedObject::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
68 if (name.empty()) return;
69 scripting::unexpose_object(vm, table_idx, name);
73 ScriptedObject::move(float x, float y)
75 bbox.move(Vector(x, y));
79 ScriptedObject::set_pos(float x, float y)
81 // printf("SetPos: %f %f\n", x, y);
82 MovingObject::set_pos(Vector(x, y));
87 ScriptedObject::get_pos_x()
93 ScriptedObject::get_pos_y()
99 ScriptedObject::set_velocity(float x, float y)
101 new_vel = Vector(x, y);
106 ScriptedObject::get_velocity_x()
108 return physic.get_velocity_x();
112 ScriptedObject::get_velocity_y()
114 return physic.get_velocity_y();
118 ScriptedObject::set_visible(bool visible_)
120 this->visible = visible_;
124 ScriptedObject::is_visible()
130 ScriptedObject::set_solid(bool solid_)
132 this->solid = solid_;
134 set_group( COLGROUP_MOVING_STATIC );
136 set_group( COLGROUP_DISABLED );
141 ScriptedObject::is_solid()
147 ScriptedObject::gravity_enabled() const
149 return physic.gravity_enabled();
153 ScriptedObject::enable_gravity(bool f)
155 physic.enable_gravity(f);
159 ScriptedObject::set_action(const std::string& animation)
161 sprite->set_action(animation);
165 ScriptedObject::get_action()
167 return sprite->get_action();
171 ScriptedObject::get_name()
177 ScriptedObject::update(float elapsed_time)
183 physic.set_velocity(new_vel.x, new_vel.y);
186 movement = physic.get_movement(elapsed_time);
190 ScriptedObject::draw(DrawingContext& context)
195 sprite->draw(context, get_pos(), layer);
199 ScriptedObject::collision_solid(const CollisionHit& hit)
205 if(physic.get_velocity_y() > 0)
206 physic.set_velocity_y(0);
208 physic.set_velocity_y(.1f);
211 if(hit.left || hit.right) {
212 physic.set_velocity_x(0);
217 ScriptedObject::collision(GameObject& , const CollisionHit& )