4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include "path_walker.hpp"
26 PathWalker::PathWalker(const Path* path, bool running)
27 : path(path), running(running), current_node_nr(0), next_node_nr(0), stop_at_node_nr(running?-1:0), node_time(0),
30 node_mult = 1 / path->nodes[0].time;
31 next_node_nr = path->nodes.size() > 1 ? 1 : 0;
34 PathWalker::~PathWalker()
39 PathWalker::advance(float elapsed_time)
41 if (!running) return path->nodes[current_node_nr].position;
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;
76 PathWalker::goto_node(int node_no)
78 if (node_no == stop_at_node_nr) return;
80 stop_at_node_nr = node_no;
84 PathWalker::start_moving()
91 PathWalker::stop_moving()
93 stop_at_node_nr = next_node_nr;
98 PathWalker::advance_node()
100 current_node_nr = next_node_nr;
101 if (static_cast<int>(current_node_nr) == stop_at_node_nr) running = false;
103 if(next_node_nr + 1 < path->nodes.size()) {
110 next_node_nr = path->nodes.size() - 1;
114 case Path::PING_PONG:
115 walking_speed = -walking_speed;
116 next_node_nr = path->nodes.size() > 1 ? path->nodes.size() - 2 : 0;
124 // we shouldn't get here
126 next_node_nr = path->nodes.size() - 1;
131 PathWalker::goback_node()
133 current_node_nr = next_node_nr;
135 if(next_node_nr > 0) {
141 case Path::PING_PONG:
142 walking_speed = -walking_speed;
143 next_node_nr = path->nodes.size() > 1 ? 1 : 0;