5 #include "scripted_object.hpp"
6 #include "video/drawing_context.hpp"
7 #include "sprite/sprite_manager.hpp"
8 #include "resources.hpp"
9 #include "object_factory.hpp"
10 #include "math/vector.hpp"
12 ScriptedObject::ScriptedObject(const lisp::Lisp& lisp)
13 : solid(true), physic_enabled(true), visible(true), new_vel_set(false),
16 lisp.get("name", name);
18 throw std::runtime_error("Scripted object must have a name specified");
20 std::string spritename;
21 lisp.get("sprite", spritename);
23 throw std::runtime_error("Scripted object must have a sprite name specified");
24 sprite = sprite_manager->create(spritename);
26 lisp.get("x", bbox.p1.x);
27 lisp.get("y", bbox.p1.y);
28 float width = sprite->get_width();
29 float height = sprite->get_height();
30 lisp.get("width", width);
31 lisp.get("height", height);
32 bbox.set_size(width, height);
34 lisp.get("solid", solid);
35 lisp.get("physic-enabled", physic_enabled);
36 lisp.get("visible", visible);
37 lisp.get("layer", layer);
42 ScriptedObject::~ScriptedObject()
48 ScriptedObject::move(float x, float y)
50 bbox.move(Vector(x, y));
54 ScriptedObject::set_pos(float x, float y)
56 bbox.set_pos(Vector(x, y));
60 ScriptedObject::get_pos_x()
66 ScriptedObject::get_pos_y()
72 ScriptedObject::set_velocity(float x, float y)
74 new_vel = Vector(x, y);
79 ScriptedObject::get_velocity_x()
81 return physic.get_velocity_x();
85 ScriptedObject::get_velocity_y()
87 return physic.get_velocity_y();
91 ScriptedObject::set_visible(bool visible)
93 this->visible = visible;
97 ScriptedObject::is_visible()
103 ScriptedObject::set_animation(const std::string& animation)
105 sprite->set_action(animation);
109 ScriptedObject::get_animation()
111 return sprite->get_action_name();
115 ScriptedObject::get_name()
121 ScriptedObject::update(float elapsed_time)
127 physic.set_velocity(new_vel.x, new_vel.y);
130 movement = physic.get_movement(elapsed_time);
134 ScriptedObject::draw(DrawingContext& context)
139 sprite->draw(context, get_pos(), layer);
143 ScriptedObject::collision(GameObject& other, const CollisionHit& )
148 if(!(other.get_flags() & FLAG_SOLID))
151 physic.set_velocity(0, 0);
155 IMPLEMENT_FACTORY(ScriptedObject, "scriptedobject");