6 #include "scripted_object.hpp"
7 #include "video/drawing_context.hpp"
8 #include "sprite/sprite_manager.hpp"
9 #include "scripting/wrapper_util.hpp"
10 #include "resources.hpp"
11 #include "object_factory.hpp"
12 #include "math/vector.hpp"
14 ScriptedObject::ScriptedObject(const lisp::Lisp& lisp)
15 : solid(true), physic_enabled(true), visible(true), new_vel_set(false),
18 lisp.get("name", name);
20 throw std::runtime_error("Scripted object must have a name specified");
22 std::string spritename;
23 lisp.get("sprite", spritename);
25 throw std::runtime_error("Scripted object must have a sprite name specified");
26 sprite = sprite_manager->create(spritename);
28 lisp.get("x", bbox.p1.x);
29 lisp.get("y", bbox.p1.y);
30 float width = sprite->get_width();
31 float height = sprite->get_height();
32 lisp.get("width", width);
33 lisp.get("height", height);
34 bbox.set_size(width, height);
36 lisp.get("solid", solid);
37 lisp.get("physic-enabled", physic_enabled);
38 lisp.get("visible", visible);
39 lisp.get("layer", layer);
44 ScriptedObject::~ScriptedObject()
50 ScriptedObject::expose(HSQUIRRELVM vm, int table_idx)
52 Scripting::ScriptedObject* interface = static_cast<Scripting::ScriptedObject*> (this);
53 expose_object(vm, table_idx, interface, name, false);
57 ScriptedObject::unexpose(HSQUIRRELVM vm, int table_idx)
59 Scripting::unexpose_object(vm, table_idx, name);
63 ScriptedObject::move(float x, float y)
65 bbox.move(Vector(x, y));
69 ScriptedObject::set_pos(float x, float y)
71 bbox.set_pos(Vector(x, y));
75 ScriptedObject::get_pos_x()
81 ScriptedObject::get_pos_y()
87 ScriptedObject::set_velocity(float x, float y)
89 new_vel = Vector(x, y);
94 ScriptedObject::get_velocity_x()
96 return physic.get_velocity_x();
100 ScriptedObject::get_velocity_y()
102 return physic.get_velocity_y();
106 ScriptedObject::set_visible(bool visible)
108 this->visible = visible;
112 ScriptedObject::is_visible()
118 ScriptedObject::set_action(const std::string& animation)
120 sprite->set_action(animation);
124 ScriptedObject::get_action()
126 return sprite->get_action_name();
130 ScriptedObject::get_name()
136 ScriptedObject::update(float elapsed_time)
142 physic.set_velocity(new_vel.x, new_vel.y);
145 movement = physic.get_movement(elapsed_time);
149 ScriptedObject::draw(DrawingContext& context)
154 sprite->draw(context, get_pos(), layer);
158 ScriptedObject::collision(GameObject& other, const CollisionHit& hit)
163 if(!(other.get_flags() & FLAG_SOLID))
166 if(other.get_flags() & FLAG_SOLID) {
167 if(hit.normal.y < 0) { // landed on floor
168 if(physic.get_velocity_y() < 0)
169 physic.set_velocity_y(0);
170 } else if(hit.normal.y > 0) { // bumped against roof
171 physic.set_velocity_y(.1);
174 if(fabsf(hit.normal.x) > .9) { // hit on side?
175 physic.set_velocity_x(0);
184 IMPLEMENT_FACTORY(ScriptedObject, "scriptedobject");