renamed all .h to .hpp
[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 {
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   new_vel = Vector(x, y);
73   new_vel_set = true;
74 }
75
76 float
77 ScriptedObject::get_velocity_x()
78 {
79   return physic.get_velocity_x();
80 }
81
82 float
83 ScriptedObject::get_velocity_y()
84 {
85   return physic.get_velocity_y();
86 }
87
88 void
89 ScriptedObject::set_visible(bool visible)
90 {
91   this->visible = visible;
92 }
93
94 bool
95 ScriptedObject::is_visible()
96 {
97   return visible;
98 }
99
100 void
101 ScriptedObject::set_animation(const std::string& animation)
102 {
103   sprite->set_action(animation);
104 }
105
106 std::string
107 ScriptedObject::get_animation()
108 {
109   return sprite->get_action_name();
110 }
111
112 std::string
113 ScriptedObject::get_name()
114 {
115   return name;
116 }
117
118 void
119 ScriptedObject::update(float elapsed_time)
120 {
121   if(!physic_enabled)
122     return;
123
124   if(new_vel_set) {
125     physic.set_velocity(new_vel.x, new_vel.y);
126     new_vel_set = false;
127   }
128   movement = physic.get_movement(elapsed_time);
129 }
130
131 void
132 ScriptedObject::draw(DrawingContext& context)
133 {
134   if(!visible)
135     return;
136
137   sprite->draw(context, get_pos(), LAYER_OBJECTS);
138 }
139
140 HitResponse
141 ScriptedObject::collision(GameObject& other, const CollisionHit& )
142 {
143   if(!physic_enabled)
144     return FORCE_MOVE;
145
146   if(!(other.get_flags() & FLAG_SOLID))
147     return FORCE_MOVE;
148
149   physic.set_velocity(0, 0);
150   return CONTINUE;
151 }
152
153 IMPLEMENT_FACTORY(ScriptedObject, "scriptedobject");