5 #include "scripted_object.h"
6 #include "video/drawing_context.h"
7 #include "sprite/sprite_manager.h"
9 #include "object_factory.h"
10 #include "math/vector.h"
12 ScriptedObject::ScriptedObject(const lisp::Lisp& lisp)
13 : solid(true), physic_enabled(true), visible(true), new_vel_set(false)
15 lisp.get("name", name);
17 throw std::runtime_error("Scripted object must have a name specified");
19 std::string spritename;
20 lisp.get("sprite", spritename);
22 throw std::runtime_error("Scripted object must have a sprite name specified");
23 sprite = sprite_manager->create(spritename);
25 lisp.get("x", bbox.p1.x);
26 lisp.get("y", bbox.p1.y);
27 float width = sprite->get_width();
28 float height = sprite->get_height();
29 lisp.get("width", width);
30 lisp.get("height", height);
31 bbox.set_size(width, height);
33 lisp.get("solid", solid);
34 lisp.get("physic-enabled", physic_enabled);
35 lisp.get("visible", visible);
40 ScriptedObject::~ScriptedObject()
46 ScriptedObject::move(float x, float y)
48 bbox.move(Vector(x, y));
52 ScriptedObject::set_pos(float x, float y)
54 bbox.set_pos(Vector(x, y));
58 ScriptedObject::get_pos_x()
64 ScriptedObject::get_pos_y()
70 ScriptedObject::set_velocity(float x, float y)
72 new_vel = Vector(x, y);
77 ScriptedObject::get_velocity_x()
79 return physic.get_velocity_x();
83 ScriptedObject::get_velocity_y()
85 return physic.get_velocity_y();
89 ScriptedObject::set_visible(bool visible)
91 this->visible = visible;
95 ScriptedObject::is_visible()
101 ScriptedObject::set_animation(const std::string& animation)
103 sprite->set_action(animation);
107 ScriptedObject::get_animation()
109 return sprite->get_action_name();
113 ScriptedObject::get_name()
119 ScriptedObject::update(float elapsed_time)
125 physic.set_velocity(new_vel.x, new_vel.y);
128 movement = physic.get_movement(elapsed_time);
132 ScriptedObject::draw(DrawingContext& context)
137 sprite->draw(context, get_pos(), LAYER_OBJECTS);
141 ScriptedObject::collision(GameObject& other, const CollisionHit& )
146 if(!(other.get_flags() & FLAG_SOLID))
149 physic.set_velocity(0, 0);
153 IMPLEMENT_FACTORY(ScriptedObject, "scriptedobject");