forgot to add files
[supertux.git] / src / object / scripted_object.cpp
1 #include <config.h>
2
3 #include <stdexcept>
4
5 #include "scripted_object.h"
6 #include "video/drawing_context.h"
7 #include "sprite/sprite_manager.h"
8 #include "resources.h"
9 #include "object_factory.h"
10 #include "math/vector.h"
11
12 ScriptedObject::ScriptedObject(const lisp::Lisp& lisp)
13   : solid(true), physic_enabled(true), visible(true)
14 {
15   lisp.get("name", name);
16   if(name == "")
17     throw std::runtime_error("Scripted object must have a name specified");
18   
19   std::string spritename;
20   lisp.get("sprite", spritename);
21   if(spritename == "")
22     throw std::runtime_error("Scripted object must have a sprite name specified");
23   sprite = sprite_manager->create(spritename);
24
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);
32
33   lisp.get("solid", solid);
34   lisp.get("physic-enabled", physic_enabled);
35   lisp.get("visible", visible);
36   if(solid)
37     flags |= FLAG_SOLID;
38 }
39
40 ScriptedObject::~ScriptedObject()
41 {
42   delete sprite;
43 }
44
45 void
46 ScriptedObject::move(float x, float y)
47 {
48   bbox.move(Vector(x, y));
49 }
50
51 void
52 ScriptedObject::set_pos(float x, float y)
53 {
54   bbox.set_pos(Vector(x, y));
55 }
56
57 float
58 ScriptedObject::get_pos_x()
59 {
60   return get_pos().x;
61 }
62
63 float
64 ScriptedObject::get_pos_y()
65 {
66   return get_pos().y;
67 }
68
69 void
70 ScriptedObject::set_velocity(float x, float y)
71 {
72   physic.set_velocity(x, y);
73 }
74
75 float
76 ScriptedObject::get_velocity_x()
77 {
78   return physic.get_velocity_x();
79 }
80
81 float
82 ScriptedObject::get_velocity_y()
83 {
84   return physic.get_velocity_y();
85 }
86
87 void
88 ScriptedObject::set_visible(bool visible)
89 {
90   this->visible = visible;
91 }
92
93 bool
94 ScriptedObject::is_visible()
95 {
96   return visible;
97 }
98
99 void
100 ScriptedObject::set_animation(const std::string& animation)
101 {
102   sprite->set_action(animation);
103 }
104
105 std::string
106 ScriptedObject::get_animation()
107 {
108   return sprite->get_action_name();
109 }
110
111 std::string
112 ScriptedObject::get_name()
113 {
114   return name;
115 }
116
117 void
118 ScriptedObject::action(float elapsed_time)
119 {
120   if(!physic_enabled)
121     return;
122
123   movement = physic.get_movement(elapsed_time);
124 }
125
126 void
127 ScriptedObject::draw(DrawingContext& context)
128 {
129   if(!visible)
130     return;
131
132   sprite->draw(context, get_pos(), LAYER_OBJECTS);
133 }
134
135 HitResponse
136 ScriptedObject::collision(GameObject& other, const CollisionHit& )
137 {
138   if(!physic_enabled)
139     return FORCE_MOVE;
140
141   if(!(other.get_flags() & FLAG_SOLID))
142     return FORCE_MOVE;
143
144   physic.set_velocity(0, 0);
145   return CONTINUE;
146 }
147
148 IMPLEMENT_FACTORY(ScriptedObject, "scriptedobject");