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