1 tile holes work again, fixed some bugs in the PathWalker
[supertux.git] / src / object / path_walker.cpp
1 //  $Id: path.hpp 3114 2006-03-23 23:47:04Z sommer $
2 // 
3 //  SuperTux Path
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>
7 //
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.
12 //
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.
17 // 
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
21 //  02111-1307, USA.
22 #include <config.h>
23
24 #include <math.h>
25 #include "path_walker.hpp"
26
27 PathWalker::PathWalker(const Path* path)
28   : path(path), current_node_nr(0), next_node_nr(0), node_time(0),
29     walking_speed(1.0)
30 {
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;
34 }
35
36 PathWalker::~PathWalker()
37 {
38 }
39
40 Vector
41 PathWalker::advance(float elapsed_time)
42 {
43   assert(elapsed_time >= 0);
44
45   elapsed_time *= fabsf(walking_speed);
46   
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;
50
51     if(walking_speed > 0) {
52       advance_node();
53     } else if(walking_speed < 0) {
54       goback_node();
55     }
56
57     current_node = & (path->nodes[current_node_nr]);
58     node_time = 0;
59     if(walking_speed > 0) {
60       node_mult = 1 / current_node->time;
61     } else {
62       node_mult = 1 / path->nodes[next_node_nr].time;
63     }
64   }
65   
66   const Path::Node* next_node = & (path->nodes[next_node_nr]);
67   node_time += elapsed_time * node_mult;
68  
69   Vector new_pos = current_node->position + 
70     (next_node->position - current_node->position) * node_time;
71     
72   Vector result = new_pos - last_pos;
73   last_pos = new_pos;
74   
75   return result;
76 }
77
78 void
79 PathWalker::advance_node()
80 {
81   current_node_nr = next_node_nr;
82
83   if(next_node_nr + 1 < path->nodes.size()) {
84     next_node_nr++;
85     return;
86   }
87
88   switch(path->mode) {
89     case Path::ONE_SHOT:
90       next_node_nr = path->nodes.size() - 1;
91       walking_speed = 0;
92       return;
93
94     case Path::PING_PONG:
95       walking_speed = -walking_speed;
96       next_node_nr = path->nodes.size() > 1 ? path->nodes.size() - 2 : 0;
97       return;
98
99     case Path::CIRCULAR:
100       next_node_nr = 0;
101       return;
102   }
103
104   // we shouldn't get here
105   assert(false);
106   next_node_nr = path->nodes.size() - 1;
107   walking_speed = 0;
108 }
109
110 void
111 PathWalker::goback_node()
112 {
113   current_node_nr = next_node_nr;
114
115   if(next_node_nr > 0) {
116     next_node_nr--;
117     return;
118   }
119
120   switch(path->mode) {
121     case Path::PING_PONG:
122       walking_speed = -walking_speed;
123       next_node_nr = path->nodes.size() > 1 ? 1 : 0;
124       return;
125     default:
126       break;
127   }
128
129   assert(false);
130   next_node_nr = 0;
131   walking_speed = 0;
132 }