1 // $Id: path.hpp 3114 2006-03-23 23:47:04Z sommer $
4 // Copyright (C) 2005 Philipp <balinor@pnxs.de>
5 // Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
6 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License
10 // as published by the Free Software Foundation; either version 2
11 // of the License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 #include "path_walker.hpp"
27 PathWalker::PathWalker(const Path* path)
28 : path(path), current_node_nr(0), next_node_nr(0), node_time(0),
31 last_pos = path->nodes[0].position;
32 node_time = path->nodes[0].time;
35 PathWalker::~PathWalker()
40 PathWalker::advance(float elapsed_time)
42 assert(elapsed_time >= 0);
44 elapsed_time *= fabsf(walking_speed);
46 const Path::Node* current_node = & (path->nodes[current_node_nr]);
47 while(node_time - elapsed_time < 0) {
48 elapsed_time -= current_node->time - node_time;
50 if(walking_speed > 0) {
52 } else if(walking_speed < 0) {
56 current_node = & (path->nodes[current_node_nr]);
57 if(walking_speed > 0) {
58 node_time = current_node->time;
60 node_time = path->nodes[next_node_nr].time;
64 const Path::Node* next_node = & (path->nodes[next_node_nr]);
65 node_time -= elapsed_time;
67 Vector new_pos = current_node->position +
68 (next_node->position - current_node->position)
69 * (1 - (node_time / current_node->time));
71 Vector result = new_pos - last_pos;
78 PathWalker::advance_node()
80 current_node_nr = next_node_nr;
82 if(next_node_nr + 1 < path->nodes.size()) {
89 next_node_nr = path->nodes.size() - 1;
94 walking_speed = -walking_speed;
95 next_node_nr = path->nodes.size() > 1 ? path->nodes.size() - 2 : 0;
103 // we shouldn't get here
105 next_node_nr = path->nodes.size() - 1;
110 PathWalker::goback_node()
112 current_node_nr = next_node_nr;
114 if(next_node_nr > 0) {
120 case Path::PING_PONG:
121 walking_speed = -walking_speed;