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_mult = 1 / path->nodes[0].time;
33 next_node_nr = path->nodes.size() > 1 ? 1 : 0;
36 PathWalker::~PathWalker()
41 PathWalker::advance(float elapsed_time)
43 assert(elapsed_time >= 0);
45 elapsed_time *= fabsf(walking_speed);
47 const Path::Node* current_node = & (path->nodes[current_node_nr]);
48 while(node_time + elapsed_time * node_mult >= 1) {
49 elapsed_time -= (1 - node_time) / node_mult;
51 if(walking_speed > 0) {
53 } else if(walking_speed < 0) {
57 current_node = & (path->nodes[current_node_nr]);
59 if(walking_speed > 0) {
60 node_mult = 1 / current_node->time;
62 node_mult = 1 / path->nodes[next_node_nr].time;
66 const Path::Node* next_node = & (path->nodes[next_node_nr]);
67 node_time += elapsed_time * node_mult;
69 Vector new_pos = current_node->position +
70 (next_node->position - current_node->position) * node_time;
72 Vector result = new_pos - last_pos;
79 PathWalker::advance_node()
81 current_node_nr = next_node_nr;
83 if(next_node_nr + 1 < path->nodes.size()) {
90 next_node_nr = path->nodes.size() - 1;
95 walking_speed = -walking_speed;
96 next_node_nr = path->nodes.size() > 1 ? path->nodes.size() - 2 : 0;
104 // we shouldn't get here
106 next_node_nr = path->nodes.size() - 1;
111 PathWalker::goback_node()
113 current_node_nr = next_node_nr;
115 if(next_node_nr > 0) {
121 case Path::PING_PONG:
122 walking_speed = -walking_speed;
123 next_node_nr = path->nodes.size() > 1 ? 1 : 0;