src/object/scripted_object.cpp

Go to the documentation of this file.
00001 //  SuperTux
00002 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
00003 //
00004 //  This program is free software: you can redistribute it and/or modify
00005 //  it under the terms of the GNU General Public License as published by
00006 //  the Free Software Foundation, either version 3 of the License, or
00007 //  (at your option) any later version.
00008 //
00009 //  This program is distributed in the hope that it will be useful,
00010 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 //  GNU General Public License for more details.
00013 //
00014 //  You should have received a copy of the GNU General Public License
00015 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 
00017 #include "object/scripted_object.hpp"
00018 
00019 #include <stdio.h>
00020 
00021 #include "scripting/squirrel_util.hpp"
00022 #include "sprite/sprite.hpp"
00023 #include "supertux/object_factory.hpp"
00024 #include "util/reader.hpp"
00025 
00026 ScriptedObject::ScriptedObject(const Reader& lisp) :
00027   MovingSprite(lisp, LAYER_OBJECTS, COLGROUP_MOVING_STATIC),
00028   physic(),
00029   name(),
00030   solid(true), 
00031   physic_enabled(true), 
00032   visible(true), 
00033   new_vel_set(false),
00034   new_vel()
00035 {
00036   lisp.get("name", name);
00037   if(name == "")
00038     throw std::runtime_error("Scripted object must have a name specified");
00039 
00040   // FIXME: do we need this? bbox is already set via .sprite file
00041   float width = sprite->get_width();
00042   float height = sprite->get_height();
00043   lisp.get("width", width);
00044   lisp.get("height", height);
00045   bbox.set_size(width, height);
00046 
00047   lisp.get("solid", solid);
00048   lisp.get("physic-enabled", physic_enabled);
00049   lisp.get("visible", visible);
00050   layer = reader_get_layer (lisp, /* default = */ LAYER_OBJECTS);
00051   if( solid ){
00052     set_group( COLGROUP_MOVING_STATIC );
00053   } else {
00054     set_group( COLGROUP_DISABLED );
00055   }
00056 }
00057 
00058 void
00059 ScriptedObject::expose(HSQUIRRELVM vm, SQInteger table_idx)
00060 {
00061   if (name.empty()) return;
00062   expose_object(vm, table_idx, dynamic_cast<scripting::ScriptedObject *>(this), name, false);
00063 }
00064 
00065 void
00066 ScriptedObject::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
00067 {
00068   if (name.empty()) return;
00069   scripting::unexpose_object(vm, table_idx, name);
00070 }
00071 
00072 void
00073 ScriptedObject::move(float x, float y)
00074 {
00075   bbox.move(Vector(x, y));
00076 }
00077 
00078 void
00079 ScriptedObject::set_pos(float x, float y)
00080 {
00081   printf("SetPos: %f %f\n", x, y);
00082   bbox.set_pos(Vector(x, y));
00083   physic.reset();
00084 }
00085 
00086 float
00087 ScriptedObject::get_pos_x()
00088 {
00089   return get_pos().x;
00090 }
00091 
00092 float
00093 ScriptedObject::get_pos_y()
00094 {
00095   return get_pos().y;
00096 }
00097 
00098 void
00099 ScriptedObject::set_velocity(float x, float y)
00100 {
00101   new_vel = Vector(x, y);
00102   new_vel_set = true;
00103 }
00104 
00105 float
00106 ScriptedObject::get_velocity_x()
00107 {
00108   return physic.get_velocity_x();
00109 }
00110 
00111 float
00112 ScriptedObject::get_velocity_y()
00113 {
00114   return physic.get_velocity_y();
00115 }
00116 
00117 void
00118 ScriptedObject::set_visible(bool visible)
00119 {
00120   this->visible = visible;
00121 }
00122 
00123 bool
00124 ScriptedObject::is_visible()
00125 {
00126   return visible;
00127 }
00128 
00129 void
00130 ScriptedObject::set_solid(bool solid)
00131 {
00132   this->solid = solid;
00133   if( solid ){
00134     set_group( COLGROUP_MOVING_STATIC );
00135   } else {
00136     set_group( COLGROUP_DISABLED );
00137   }
00138 }
00139 
00140 bool
00141 ScriptedObject::is_solid()
00142 {
00143   return solid;
00144 }
00145 
00146 void
00147 ScriptedObject::set_action(const std::string& animation)
00148 {
00149   sprite->set_action(animation);
00150 }
00151 
00152 std::string
00153 ScriptedObject::get_action()
00154 {
00155   return sprite->get_action();
00156 }
00157 
00158 std::string
00159 ScriptedObject::get_name()
00160 {
00161   return name;
00162 }
00163 
00164 void
00165 ScriptedObject::update(float elapsed_time)
00166 {
00167   if(!physic_enabled)
00168     return;
00169 
00170   if(new_vel_set) {
00171     physic.set_velocity(new_vel.x, new_vel.y);
00172     new_vel_set = false;
00173   }
00174   movement = physic.get_movement(elapsed_time);
00175 }
00176 
00177 void
00178 ScriptedObject::draw(DrawingContext& context)
00179 {
00180   if(!visible)
00181     return;
00182 
00183   sprite->draw(context, get_pos(), layer);
00184 }
00185 
00186 void
00187 ScriptedObject::collision_solid(const CollisionHit& hit)
00188 {
00189   if(!physic_enabled)
00190     return;
00191 
00192   if(hit.bottom) {
00193     if(physic.get_velocity_y() > 0)
00194       physic.set_velocity_y(0);
00195   } else if(hit.top) {
00196     physic.set_velocity_y(.1f);
00197   }
00198 
00199   if(hit.left || hit.right) {
00200     physic.set_velocity_x(0);
00201   }
00202 }
00203 
00204 HitResponse
00205 ScriptedObject::collision(GameObject& , const CollisionHit& )
00206 {
00207   return FORCE_MOVE;
00208 }
00209 
00210 /* EOF */

Generated on Mon Jun 9 03:38:20 2014 for SuperTux by  doxygen 1.5.1