Massive copyright update. I'm sorry if I'm crediting Matze for something he didn...
[supertux.git] / src / object / scripted_object.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 #include <config.h>
21
22 #include <stdexcept>
23 #include <math.h>
24
25 #include "scripted_object.hpp"
26 #include "video/drawing_context.hpp"
27 #include "sprite/sprite_manager.hpp"
28 #include "scripting/wrapper_util.hpp"
29 #include "resources.hpp"
30 #include "object_factory.hpp"
31 #include "math/vector.hpp"
32
33 ScriptedObject::ScriptedObject(const lisp::Lisp& lisp)
34   : solid(true), physic_enabled(true), visible(true), new_vel_set(false),
35     layer(LAYER_OBJECTS)
36 {
37   lisp.get("name", name);
38   if(name == "")
39     throw std::runtime_error("Scripted object must have a name specified");
40   
41   std::string spritename;
42   lisp.get("sprite", spritename);
43   if(spritename == "")
44     throw std::runtime_error("Scripted object must have a sprite name specified");
45   sprite = sprite_manager->create(spritename);
46
47   lisp.get("x", bbox.p1.x);
48   lisp.get("y", bbox.p1.y);
49   float width = sprite->get_width();
50   float height = sprite->get_height();
51   lisp.get("width", width);
52   lisp.get("height", height);
53   bbox.set_size(width, height);
54
55   lisp.get("solid", solid);
56   lisp.get("physic-enabled", physic_enabled);
57   lisp.get("visible", visible);
58   lisp.get("layer", layer);
59   if(solid)
60     flags |= FLAG_SOLID;
61 }
62
63 ScriptedObject::~ScriptedObject()
64 {
65   delete sprite;
66 }
67
68 void
69 ScriptedObject::expose(HSQUIRRELVM vm, int table_idx)
70 {
71   Scripting::ScriptedObject* interface = static_cast<Scripting::ScriptedObject*> (this);
72   expose_object(vm, table_idx, interface, name, false);
73 }
74
75 void
76 ScriptedObject::unexpose(HSQUIRRELVM vm, int table_idx)
77 {
78   Scripting::unexpose_object(vm, table_idx, name);
79 }
80
81 void
82 ScriptedObject::move(float x, float y)
83 {
84   bbox.move(Vector(x, y));
85 }
86
87 void
88 ScriptedObject::set_pos(float x, float y)
89 {
90   bbox.set_pos(Vector(x, y));
91 }
92
93 float
94 ScriptedObject::get_pos_x()
95 {
96   return get_pos().x;
97 }
98
99 float
100 ScriptedObject::get_pos_y()
101 {
102   return get_pos().y;
103 }
104
105 void
106 ScriptedObject::set_velocity(float x, float y)
107 {
108   new_vel = Vector(x, y);
109   new_vel_set = true;
110 }
111
112 float
113 ScriptedObject::get_velocity_x()
114 {
115   return physic.get_velocity_x();
116 }
117
118 float
119 ScriptedObject::get_velocity_y()
120 {
121   return physic.get_velocity_y();
122 }
123
124 void
125 ScriptedObject::set_visible(bool visible)
126 {
127   this->visible = visible;
128 }
129
130 bool
131 ScriptedObject::is_visible()
132 {
133   return visible;
134 }
135
136 void
137 ScriptedObject::set_action(const std::string& animation)
138 {
139   sprite->set_action(animation);
140 }
141
142 std::string
143 ScriptedObject::get_action()
144 {
145   return sprite->get_action_name();
146 }
147
148 std::string
149 ScriptedObject::get_name()
150 {
151   return name;
152 }
153
154 void
155 ScriptedObject::update(float elapsed_time)
156 {
157   if(!physic_enabled)
158     return;
159
160   if(new_vel_set) {
161     physic.set_velocity(new_vel.x, new_vel.y);
162     new_vel_set = false;
163   }
164   movement = physic.get_movement(elapsed_time);
165 }
166
167 void
168 ScriptedObject::draw(DrawingContext& context)
169 {
170   if(!visible)
171     return;
172
173   sprite->draw(context, get_pos(), layer);
174 }
175
176 HitResponse
177 ScriptedObject::collision(GameObject& other, const CollisionHit& hit)
178 {
179   if(!physic_enabled)
180     return FORCE_MOVE;
181
182   if(!(other.get_flags() & FLAG_SOLID))
183     return FORCE_MOVE;
184
185   if(other.get_flags() & FLAG_SOLID) {
186     if(hit.normal.y < 0) { // landed on floor
187       if(physic.get_velocity_y() < 0)
188         physic.set_velocity_y(0);
189     } else if(hit.normal.y > 0) { // bumped against roof
190       physic.set_velocity_y(.1);
191     }
192
193     if(fabsf(hit.normal.x) > .9) { // hit on side?
194       physic.set_velocity_x(0);
195     }
196         
197     return CONTINUE;
198   }
199
200   return FORCE_MOVE;
201 }
202
203 IMPLEMENT_FACTORY(ScriptedObject, "scriptedobject");