X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fobject%2Fpath_walker.cpp;h=0c8ae2599ee8fff40bca5ecbb7773bcfd66e4501;hb=a98f7cee9f6a2593c0e1f3442800f159bad410df;hp=1eca370230c03ee680fca2a300f19b34352238e1;hpb=1d63ad0dfe0eedf89191627be00b2b3b1215c311;p=supertux.git diff --git a/src/object/path_walker.cpp b/src/object/path_walker.cpp index 1eca37023..0c8ae2599 100644 --- a/src/object/path_walker.cpp +++ b/src/object/path_walker.cpp @@ -1,8 +1,6 @@ -// $Id: path.hpp 3114 2006-03-23 23:47:04Z sommer $ -// -// SuperTux Path -// Copyright (C) 2005 Philipp -// Copyright (C) 2006 Christoph Sommer +// $Id$ +// +// SuperTux // Copyright (C) 2006 Matthias Braun // // This program is free software; you can redistribute it and/or @@ -14,22 +12,23 @@ // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. -// +// // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA -// 02111-1307, USA. +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + #include #include +#include #include "path_walker.hpp" -PathWalker::PathWalker(const Path* path) - : path(path), current_node_nr(0), next_node_nr(0), node_time(0), +PathWalker::PathWalker(const Path* path, bool running) + : path(path), running(running), current_node_nr(0), next_node_nr(0), stop_at_node_nr(running?-1:0), node_time(0), walking_speed(1.0) { - last_pos = path->nodes[0].position; - node_time = path->nodes[0].time; + node_mult = 1 / path->nodes[0].time; + next_node_nr = path->nodes.size() > 1 ? 1 : 0; } PathWalker::~PathWalker() @@ -39,13 +38,15 @@ PathWalker::~PathWalker() Vector PathWalker::advance(float elapsed_time) { + if (!running) return path->nodes[current_node_nr].position; + assert(elapsed_time >= 0); elapsed_time *= fabsf(walking_speed); - + const Path::Node* current_node = & (path->nodes[current_node_nr]); - while(node_time - elapsed_time < 0) { - elapsed_time -= current_node->time - node_time; + while(node_time + elapsed_time * node_mult >= 1) { + elapsed_time -= (1 - node_time) / node_mult; if(walking_speed > 0) { advance_node(); @@ -54,30 +55,50 @@ PathWalker::advance(float elapsed_time) } current_node = & (path->nodes[current_node_nr]); + node_time = 0; if(walking_speed > 0) { - node_time = current_node->time; + node_mult = 1 / current_node->time; } else { - node_time = path->nodes[next_node_nr].time; + node_mult = 1 / path->nodes[next_node_nr].time; } } - + const Path::Node* next_node = & (path->nodes[next_node_nr]); - node_time -= elapsed_time; - - Vector new_pos = current_node->position + - (next_node->position - current_node->position) - * (1 - (node_time / current_node->time)); - - Vector result = new_pos - last_pos; - last_pos = new_pos; - - return result; + node_time += elapsed_time * node_mult; + + Vector new_pos = current_node->position + + (next_node->position - current_node->position) * node_time; + + return new_pos; } void +PathWalker::goto_node(int node_no) +{ + if (node_no == stop_at_node_nr) return; + running = true; + stop_at_node_nr = node_no; +} + +void +PathWalker::start_moving() +{ + running = true; + stop_at_node_nr = -1; +} + +void +PathWalker::stop_moving() +{ + stop_at_node_nr = next_node_nr; +} + + +void PathWalker::advance_node() { current_node_nr = next_node_nr; + if (static_cast(current_node_nr) == stop_at_node_nr) running = false; if(next_node_nr + 1 < path->nodes.size()) { next_node_nr++; @@ -119,6 +140,7 @@ PathWalker::goback_node() switch(path->mode) { case Path::PING_PONG: walking_speed = -walking_speed; + next_node_nr = path->nodes.size() > 1 ? 1 : 0; return; default: break;