src/object/platform.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/platform.hpp"
00018 
00019 #include "object/player.hpp"
00020 #include "scripting/platform.hpp"
00021 #include "scripting/squirrel_util.hpp"
00022 #include "supertux/object_factory.hpp"
00023 #include "supertux/sector.hpp"
00024 #include "util/reader.hpp"
00025 
00026 Platform::Platform(const Reader& reader) :
00027   MovingSprite(reader, Vector(0,0), LAYER_OBJECTS, COLGROUP_STATIC), 
00028   path(),
00029   walker(),
00030   speed(Vector(0,0)), 
00031   automatic(false), 
00032   player_contact(false), 
00033   last_player_contact(false)
00034 {
00035   bool running = true;
00036   reader.get("name", name);
00037   reader.get("running", running);
00038   if ((name == "") && (!running)) automatic=true;
00039   const lisp::Lisp* pathLisp = reader.get_lisp("path");
00040   if(pathLisp == NULL)
00041     throw std::runtime_error("No path specified for platform");
00042   path.reset(new Path());
00043   path->read(*pathLisp);
00044   walker.reset(new PathWalker(path.get(), running));
00045   bbox.set_pos(path->get_base());
00046 }
00047 
00048 /*
00049   Platform::Platform(const Platform& other) :
00050   MovingSprite(other), 
00051   ScriptInterface(other), 
00052   speed(other.speed), 
00053   automatic(other.automatic), 
00054   player_contact(false), 
00055   last_player_contact(false)
00056   {
00057   name = other.name;
00058   path.reset(new Path(*other.path));
00059   walker.reset(new PathWalker(*other.walker));
00060   walker->path = &*path;
00061   }
00062 */
00063 
00064 HitResponse
00065 Platform::collision(GameObject& other, const CollisionHit& )
00066 {
00067   if (dynamic_cast<Player*>(&other)) player_contact = true;
00068   return FORCE_MOVE;
00069 }
00070 
00071 void
00072 Platform::update(float elapsed_time)
00073 {
00074   // check if Platform should automatically pick a destination
00075   if (automatic) {
00076 
00077     if (!player_contact && !walker->is_moving()) {
00078       // Player doesn't touch platform and Platform is not moving
00079 
00080       // Travel to node nearest to nearest player
00081       // FIXME: does not really use nearest player
00082       Player* player = 0;      
00083       std::vector<Player*> players = Sector::current()->get_players();
00084       for (std::vector<Player*>::iterator playerIter = players.begin(); playerIter != players.end(); ++playerIter) {
00085         player = *playerIter;
00086       }
00087       if (player) {
00088         int nearest_node_id = path->get_nearest_node_no(player->get_bbox().p2);
00089         if (nearest_node_id != -1) {
00090           goto_node(nearest_node_id);
00091         }
00092       }
00093     } 
00094 
00095     if (player_contact && !last_player_contact && !walker->is_moving()) {
00096       // Player touched platform, didn't touch last frame and Platform is not moving
00097 
00098       // Travel to node farthest from current position
00099       int farthest_node_id = path->get_farthest_node_no(get_pos());
00100       if (farthest_node_id != -1) {
00101         goto_node(farthest_node_id);
00102       } 
00103     }
00104 
00105     // Clear player_contact flag set by collision() method
00106     last_player_contact = player_contact;
00107     player_contact = false;
00108   }
00109 
00110   movement = walker->advance(elapsed_time) - get_pos();
00111   speed = movement / elapsed_time;
00112 }
00113 
00114 void
00115 Platform::goto_node(int node_no)
00116 {
00117   walker->goto_node(node_no);
00118 }
00119 
00120 void
00121 Platform::start_moving()
00122 {
00123   walker->start_moving();
00124 }
00125 
00126 void
00127 Platform::stop_moving()
00128 {
00129   walker->stop_moving();
00130 }
00131 
00132 void
00133 Platform::expose(HSQUIRRELVM vm, SQInteger table_idx)
00134 {
00135   if (name.empty()) return;
00136   scripting::Platform* _this = new scripting::Platform(this);
00137   expose_object(vm, table_idx, _this, name, true);
00138 }
00139 
00140 void
00141 Platform::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
00142 {
00143   if (name.empty()) return;
00144   scripting::unexpose_object(vm, table_idx, name);
00145 }
00146 
00147 /* EOF */

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