6 #include "scripted_object.hpp"
7 #include "video/drawing_context.hpp"
8 #include "sprite/sprite_manager.hpp"
9 #include "resources.hpp"
10 #include "object_factory.hpp"
11 #include "math/vector.hpp"
13 ScriptedObject::ScriptedObject(const lisp::Lisp& lisp)
14 : solid(true), physic_enabled(true), visible(true), new_vel_set(false),
17 lisp.get("name", name);
19 throw std::runtime_error("Scripted object must have a name specified");
21 std::string spritename;
22 lisp.get("sprite", spritename);
24 throw std::runtime_error("Scripted object must have a sprite name specified");
25 sprite = sprite_manager->create(spritename);
27 lisp.get("x", bbox.p1.x);
28 lisp.get("y", bbox.p1.y);
29 float width = sprite->get_width();
30 float height = sprite->get_height();
31 lisp.get("width", width);
32 lisp.get("height", height);
33 bbox.set_size(width, height);
35 lisp.get("solid", solid);
36 lisp.get("physic-enabled", physic_enabled);
37 lisp.get("visible", visible);
38 lisp.get("layer", layer);
43 ScriptedObject::~ScriptedObject()
49 ScriptedObject::move(float x, float y)
51 bbox.move(Vector(x, y));
55 ScriptedObject::set_pos(float x, float y)
57 bbox.set_pos(Vector(x, y));
61 ScriptedObject::get_pos_x()
67 ScriptedObject::get_pos_y()
73 ScriptedObject::set_velocity(float x, float y)
75 new_vel = Vector(x, y);
80 ScriptedObject::get_velocity_x()
82 return physic.get_velocity_x();
86 ScriptedObject::get_velocity_y()
88 return physic.get_velocity_y();
92 ScriptedObject::set_visible(bool visible)
94 this->visible = visible;
98 ScriptedObject::is_visible()
104 ScriptedObject::set_action(const std::string& animation)
106 sprite->set_action(animation);
110 ScriptedObject::get_action()
112 return sprite->get_action_name();
116 ScriptedObject::get_name()
122 ScriptedObject::update(float elapsed_time)
128 physic.set_velocity(new_vel.x, new_vel.y);
131 movement = physic.get_movement(elapsed_time);
135 ScriptedObject::draw(DrawingContext& context)
140 sprite->draw(context, get_pos(), layer);
144 ScriptedObject::collision(GameObject& other, const CollisionHit& hit)
149 if(!(other.get_flags() & FLAG_SOLID))
152 if(other.get_flags() & FLAG_SOLID) {
153 if(hit.normal.y < 0) { // landed on floor
154 if(physic.get_velocity_y() < 0)
155 physic.set_velocity_y(0);
156 } else if(hit.normal.y > 0) { // bumped against roof
157 physic.set_velocity_y(.1);
160 if(fabsf(hit.normal.x) > .9) { // hit on side?
161 physic.set_velocity_x(0);
170 IMPLEMENT_FACTORY(ScriptedObject, "scriptedobject");