Tentative checkin of tuxdev's "Object improvement patch, part 1"
[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 #include <config.h>
20
21 #include <stdexcept>
22 #include <math.h>
23
24 #include "scripted_object.hpp"
25 #include "video/drawing_context.hpp"
26 #include "scripting/squirrel_util.hpp"
27 #include "resources.hpp"
28 #include "object_factory.hpp"
29 #include "math/vector.hpp"
30
31 ScriptedObject::ScriptedObject(const lisp::Lisp& lisp)
32   : MovingSprite(lisp, LAYER_OBJECTS, COLGROUP_MOVING),
33     solid(true), physic_enabled(true), visible(true), new_vel_set(false)
34 {
35   lisp.get("name", name);
36   if(name == "")
37     throw std::runtime_error("Scripted object must have a name specified");
38
39   // FIXME: do we need this? bbox is already set via .sprite file
40   float width = sprite->get_width();
41   float height = sprite->get_height();
42   lisp.get("width", width);
43   lisp.get("height", height);
44   bbox.set_size(width, height);
45
46   lisp.get("solid", solid);
47   lisp.get("physic-enabled", physic_enabled);
48   lisp.get("visible", visible);
49   lisp.get("z-pos", layer);
50 }
51
52 void
53 ScriptedObject::expose(HSQUIRRELVM vm, SQInteger table_idx)
54 {
55   if (name.empty()) return;
56   expose_object(vm, table_idx, dynamic_cast<Scripting::ScriptedObject *>(this), name, false);
57 }
58
59 void
60 ScriptedObject::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
61 {
62   if (name.empty()) return;
63   Scripting::unexpose_object(vm, table_idx, name);
64 }
65
66 void
67 ScriptedObject::move(float x, float y)
68 {
69   bbox.move(Vector(x, y));
70 }
71
72 void
73 ScriptedObject::set_pos(float x, float y)
74 {
75   printf("SetPos: %f %f\n", x, y);
76   bbox.set_pos(Vector(x, y));
77   physic.reset();
78 }
79
80 float
81 ScriptedObject::get_pos_x()
82 {
83   return get_pos().x;
84 }
85
86 float
87 ScriptedObject::get_pos_y()
88 {
89   return get_pos().y;
90 }
91
92 void
93 ScriptedObject::set_velocity(float x, float y)
94 {
95   new_vel = Vector(x, y);
96   new_vel_set = true;
97 }
98
99 float
100 ScriptedObject::get_velocity_x()
101 {
102   return physic.get_velocity_x();
103 }
104
105 float
106 ScriptedObject::get_velocity_y()
107 {
108   return physic.get_velocity_y();
109 }
110
111 void
112 ScriptedObject::set_visible(bool visible)
113 {
114   this->visible = visible;
115 }
116
117 bool
118 ScriptedObject::is_visible()
119 {
120   return visible;
121 }
122
123 void
124 ScriptedObject::set_solid(bool solid)
125 {
126   this->solid = solid;
127 }
128
129 bool
130 ScriptedObject::is_solid()
131 {
132   return solid;
133 }
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();
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 void
177 ScriptedObject::collision_solid(const CollisionHit& hit)
178 {
179   if(!physic_enabled)
180     return;
181
182   if(hit.bottom) {
183     if(physic.get_velocity_y() > 0)
184       physic.set_velocity_y(0);
185   } else if(hit.top) {
186     physic.set_velocity_y(.1);
187   }
188
189   if(hit.left || hit.right) {
190     physic.set_velocity_x(0);
191   }
192 }
193
194 HitResponse
195 ScriptedObject::collision(GameObject& , const CollisionHit& )
196 {
197   return FORCE_MOVE;
198 }
199
200 IMPLEMENT_FACTORY(ScriptedObject, "scriptedobject");