4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include "scripted_object.hpp"
25 #include "video/drawing_context.hpp"
26 #include "scripting/squirrel_util.hpp"
27 #include "resources.hpp"
28 #include "object_factory.hpp"
29 #include "math/vector.hpp"
31 ScriptedObject::ScriptedObject(const lisp::Lisp& lisp)
32 : MovingSprite(lisp, LAYER_OBJECTS, COLGROUP_MOVING_STATIC),
33 solid(true), physic_enabled(true), visible(true), new_vel_set(false)
35 lisp.get("name", name);
37 throw std::runtime_error("Scripted object must have a name specified");
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);
46 lisp.get("solid", solid);
47 lisp.get("physic-enabled", physic_enabled);
48 lisp.get("visible", visible);
49 lisp.get("z-pos", layer);
51 set_group( COLGROUP_MOVING_STATIC );
53 set_group( COLGROUP_DISABLED );
58 ScriptedObject::expose(HSQUIRRELVM vm, SQInteger table_idx)
60 if (name.empty()) return;
61 expose_object(vm, table_idx, dynamic_cast<Scripting::ScriptedObject *>(this), name, false);
65 ScriptedObject::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
67 if (name.empty()) return;
68 Scripting::unexpose_object(vm, table_idx, name);
72 ScriptedObject::move(float x, float y)
74 bbox.move(Vector(x, y));
78 ScriptedObject::set_pos(float x, float y)
80 printf("SetPos: %f %f\n", x, y);
81 bbox.set_pos(Vector(x, y));
86 ScriptedObject::get_pos_x()
92 ScriptedObject::get_pos_y()
98 ScriptedObject::set_velocity(float x, float y)
100 new_vel = Vector(x, y);
105 ScriptedObject::get_velocity_x()
107 return physic.get_velocity_x();
111 ScriptedObject::get_velocity_y()
113 return physic.get_velocity_y();
117 ScriptedObject::set_visible(bool visible)
119 this->visible = visible;
123 ScriptedObject::is_visible()
129 ScriptedObject::set_solid(bool solid)
133 set_group( COLGROUP_MOVING_STATIC );
135 set_group( COLGROUP_DISABLED );
140 ScriptedObject::is_solid()
147 ScriptedObject::set_action(const std::string& animation)
149 sprite->set_action(animation);
153 ScriptedObject::get_action()
155 return sprite->get_action();
159 ScriptedObject::get_name()
165 ScriptedObject::update(float elapsed_time)
171 physic.set_velocity(new_vel.x, new_vel.y);
174 movement = physic.get_movement(elapsed_time);
178 ScriptedObject::draw(DrawingContext& context)
183 sprite->draw(context, get_pos(), layer);
187 ScriptedObject::collision_solid(const CollisionHit& hit)
193 if(physic.get_velocity_y() > 0)
194 physic.set_velocity_y(0);
196 physic.set_velocity_y(.1);
199 if(hit.left || hit.right) {
200 physic.set_velocity_x(0);
205 ScriptedObject::collision(GameObject& , const CollisionHit& )
210 IMPLEMENT_FACTORY(ScriptedObject, "scriptedobject");