#include <path_walker.hpp>
Public Member Functions | |
PathWalker (const Path *path, bool running=true) | |
virtual | ~PathWalker () |
virtual Vector | advance (float elapsed_time) |
advances the path walker on the path and returns its new position | |
Vector | get_pos () |
current position of path walker | |
void | goto_node (int node_no) |
advance until at given node, then stop | |
void | start_moving () |
start advancing automatically | |
void | stop_moving () |
stop advancing automatically | |
bool | is_moving () |
returns true if PathWalker is currently moving | |
Public Attributes | |
const Path * | path |
Private Member Functions | |
void | advance_node () |
void | goback_node () |
PathWalker (const PathWalker &) | |
PathWalker & | operator= (const PathWalker &) |
Private Attributes | |
bool | running |
set to false to immediately stop advancing | |
size_t | current_node_nr |
size_t | next_node_nr |
int | stop_at_node_nr |
stop advancing automatically when this node is reached | |
float | node_time |
the position between the current node and the next node as fraction between 0 and 1 | |
float | node_mult |
float | walking_speed |
Definition at line 27 of file path_walker.hpp.
PathWalker::PathWalker | ( | const Path * | path, | |
bool | running = true | |||
) |
Definition at line 22 of file path_walker.cpp.
References next_node_nr, node_mult, Path::nodes, and path.
00022 : 00023 path(path), 00024 running(running), 00025 current_node_nr(0), 00026 next_node_nr(0), 00027 stop_at_node_nr(running?-1:0), 00028 node_time(0), 00029 node_mult(), 00030 walking_speed(1.0) 00031 { 00032 node_mult = 1 / path->nodes[0].time; 00033 next_node_nr = path->nodes.size() > 1 ? 1 : 0; 00034 }
PathWalker::~PathWalker | ( | ) | [virtual] |
PathWalker::PathWalker | ( | const PathWalker & | ) | [private] |
Vector PathWalker::advance | ( | float | elapsed_time | ) | [virtual] |
advances the path walker on the path and returns its new position
Definition at line 41 of file path_walker.cpp.
References advance_node(), current_node_nr, get_pos(), goback_node(), next_node_nr, node_mult, node_time, Path::nodes, path, running, Path::Node::time, and walking_speed.
00042 { 00043 if (!running) return path->nodes[current_node_nr].position; 00044 00045 assert(elapsed_time >= 0); 00046 00047 elapsed_time *= fabsf(walking_speed); 00048 00049 const Path::Node* current_node = & (path->nodes[current_node_nr]); 00050 while(node_time + elapsed_time * node_mult >= 1) { 00051 elapsed_time -= (1 - node_time) / node_mult; 00052 00053 if(walking_speed > 0) { 00054 advance_node(); 00055 } else if(walking_speed < 0) { 00056 goback_node(); 00057 } 00058 00059 current_node = & (path->nodes[current_node_nr]); 00060 node_time = 0; 00061 if(walking_speed > 0) { 00062 node_mult = 1 / current_node->time; 00063 } else { 00064 node_mult = 1 / path->nodes[next_node_nr].time; 00065 } 00066 } 00067 00068 node_time += elapsed_time * node_mult; 00069 00070 return get_pos(); 00071 }
Vector PathWalker::get_pos | ( | ) |
current position of path walker
Definition at line 74 of file path_walker.cpp.
References current_node_nr, next_node_nr, node_time, Path::nodes, path, and Path::Node::position.
Referenced by advance().
00075 { 00076 const Path::Node* current_node = & (path->nodes[current_node_nr]); 00077 const Path::Node* next_node = & (path->nodes[next_node_nr]); 00078 Vector new_pos = current_node->position + 00079 (next_node->position - current_node->position) * node_time; 00080 00081 return new_pos; 00082 }
void PathWalker::goto_node | ( | int | node_no | ) |
advance until at given node, then stop
Definition at line 85 of file path_walker.cpp.
References running, and stop_at_node_nr.
00086 { 00087 if (node_no == stop_at_node_nr) return; 00088 running = true; 00089 stop_at_node_nr = node_no; 00090 }
void PathWalker::start_moving | ( | ) |
start advancing automatically
Definition at line 93 of file path_walker.cpp.
References running, and stop_at_node_nr.
00094 { 00095 running = true; 00096 stop_at_node_nr = -1; 00097 }
void PathWalker::stop_moving | ( | ) |
stop advancing automatically
Definition at line 100 of file path_walker.cpp.
References next_node_nr, and stop_at_node_nr.
00101 { 00102 stop_at_node_nr = next_node_nr; 00103 }
bool PathWalker::is_moving | ( | ) | [inline] |
returns true if PathWalker is currently moving
Definition at line 51 of file path_walker.hpp.
References running.
00051 { 00052 return running; 00053 }
void PathWalker::advance_node | ( | ) | [private] |
Definition at line 106 of file path_walker.cpp.
References Path::CIRCULAR, current_node_nr, Path::mode, next_node_nr, Path::nodes, Path::ONE_SHOT, path, Path::PING_PONG, running, stop_at_node_nr, and walking_speed.
Referenced by advance().
00107 { 00108 current_node_nr = next_node_nr; 00109 if (static_cast<int>(current_node_nr) == stop_at_node_nr) running = false; 00110 00111 if(next_node_nr + 1 < path->nodes.size()) { 00112 next_node_nr++; 00113 return; 00114 } 00115 00116 switch(path->mode) { 00117 case Path::ONE_SHOT: 00118 next_node_nr = path->nodes.size() - 1; 00119 walking_speed = 0; 00120 return; 00121 00122 case Path::PING_PONG: 00123 walking_speed = -walking_speed; 00124 next_node_nr = path->nodes.size() > 1 ? path->nodes.size() - 2 : 0; 00125 return; 00126 00127 case Path::CIRCULAR: 00128 next_node_nr = 0; 00129 return; 00130 } 00131 00132 // we shouldn't get here 00133 assert(false); 00134 next_node_nr = path->nodes.size() - 1; 00135 walking_speed = 0; 00136 }
void PathWalker::goback_node | ( | ) | [private] |
Definition at line 139 of file path_walker.cpp.
References current_node_nr, Path::mode, next_node_nr, Path::nodes, path, Path::PING_PONG, and walking_speed.
Referenced by advance().
00140 { 00141 current_node_nr = next_node_nr; 00142 00143 if(next_node_nr > 0) { 00144 next_node_nr--; 00145 return; 00146 } 00147 00148 switch(path->mode) { 00149 case Path::PING_PONG: 00150 walking_speed = -walking_speed; 00151 next_node_nr = path->nodes.size() > 1 ? 1 : 0; 00152 return; 00153 default: 00154 break; 00155 } 00156 00157 assert(false); 00158 next_node_nr = 0; 00159 walking_speed = 0; 00160 }
PathWalker& PathWalker::operator= | ( | const PathWalker & | ) | [private] |
const Path* PathWalker::path |
Definition at line 55 of file path_walker.hpp.
Referenced by advance(), advance_node(), get_pos(), goback_node(), and PathWalker().
bool PathWalker::running [private] |
set to false to immediately stop advancing
Definition at line 64 of file path_walker.hpp.
Referenced by advance(), advance_node(), goto_node(), is_moving(), and start_moving().
size_t PathWalker::current_node_nr [private] |
Definition at line 66 of file path_walker.hpp.
Referenced by advance(), advance_node(), get_pos(), and goback_node().
size_t PathWalker::next_node_nr [private] |
Definition at line 67 of file path_walker.hpp.
Referenced by advance(), advance_node(), get_pos(), goback_node(), PathWalker(), and stop_moving().
int PathWalker::stop_at_node_nr [private] |
stop advancing automatically when this node is reached
Definition at line 72 of file path_walker.hpp.
Referenced by advance_node(), goto_node(), start_moving(), and stop_moving().
float PathWalker::node_time [private] |
the position between the current node and the next node as fraction between 0 and 1
Definition at line 78 of file path_walker.hpp.
float PathWalker::node_mult [private] |
float PathWalker::walking_speed [private] |
Definition at line 81 of file path_walker.hpp.
Referenced by advance(), advance_node(), and goback_node().