2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "object/path_walker.hpp"
22 PathWalker::PathWalker(const Path* path, bool running) :
27 stop_at_node_nr(running?-1:0),
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 if (!running) return path->nodes[current_node_nr].position;
45 assert(elapsed_time >= 0);
47 elapsed_time *= fabsf(walking_speed);
49 const Path::Node* current_node = & (path->nodes[current_node_nr]);
50 while(node_time + elapsed_time * node_mult >= 1) {
51 elapsed_time -= (1 - node_time) / node_mult;
53 if(walking_speed > 0) {
55 } else if(walking_speed < 0) {
59 current_node = & (path->nodes[current_node_nr]);
61 if(walking_speed > 0) {
62 node_mult = 1 / current_node->time;
64 node_mult = 1 / path->nodes[next_node_nr].time;
68 const Path::Node* next_node = & (path->nodes[next_node_nr]);
69 node_time += elapsed_time * node_mult;
71 Vector new_pos = current_node->position +
72 (next_node->position - current_node->position) * node_time;
78 PathWalker::goto_node(int node_no)
80 if (node_no == stop_at_node_nr) return;
82 stop_at_node_nr = node_no;
86 PathWalker::start_moving()
93 PathWalker::stop_moving()
95 stop_at_node_nr = next_node_nr;
99 PathWalker::advance_node()
101 current_node_nr = next_node_nr;
102 if (static_cast<int>(current_node_nr) == stop_at_node_nr) running = false;
104 if(next_node_nr + 1 < path->nodes.size()) {
111 next_node_nr = path->nodes.size() - 1;
115 case Path::PING_PONG:
116 walking_speed = -walking_speed;
117 next_node_nr = path->nodes.size() > 1 ? path->nodes.size() - 2 : 0;
125 // we shouldn't get here
127 next_node_nr = path->nodes.size() - 1;
132 PathWalker::goback_node()
134 current_node_nr = next_node_nr;
136 if(next_node_nr > 0) {
142 case Path::PING_PONG:
143 walking_speed = -walking_speed;
144 next_node_nr = path->nodes.size() > 1 ? 1 : 0;