fix collision against spikes too wide, fix paths, try to fix jumping on badguys that...
[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 }
34
35 PathWalker::~PathWalker()
36 {
37 }
38
39 Vector
40 PathWalker::advance(float elapsed_time)
41 {
42   assert(elapsed_time >= 0);
43
44   elapsed_time *= fabsf(walking_speed);
45   
46   const Path::Node* current_node = & (path->nodes[current_node_nr]);
47   while(node_time + elapsed_time * node_mult >= 1) {
48     elapsed_time -= (1 - node_time) * node_mult;
49
50     if(walking_speed > 0) {
51       advance_node();
52     } else if(walking_speed < 0) {
53       goback_node();
54     }
55
56     current_node = & (path->nodes[current_node_nr]);
57     node_time = 0;
58     if(walking_speed > 0) {
59       node_mult = 1 / current_node->time;
60     } else {
61       node_mult = 1 / path->nodes[next_node_nr].time;
62     }
63   }
64   
65   const Path::Node* next_node = & (path->nodes[next_node_nr]);
66   node_time += elapsed_time * node_mult;
67  
68   Vector new_pos = current_node->position + 
69     (next_node->position - current_node->position) * node_time;
70     
71   Vector result = new_pos - last_pos;
72   last_pos = new_pos;
73   
74   return result;
75 }
76
77 void
78 PathWalker::advance_node()
79 {
80   current_node_nr = next_node_nr;
81
82   if(next_node_nr + 1 < path->nodes.size()) {
83     next_node_nr++;
84     return;
85   }
86
87   switch(path->mode) {
88     case Path::ONE_SHOT:
89       next_node_nr = path->nodes.size() - 1;
90       walking_speed = 0;
91       return;
92
93     case Path::PING_PONG:
94       walking_speed = -walking_speed;
95       next_node_nr = path->nodes.size() > 1 ? path->nodes.size() - 2 : 0;
96       return;
97
98     case Path::CIRCULAR:
99       next_node_nr = 0;
100       return;
101   }
102
103   // we shouldn't get here
104   assert(false);
105   next_node_nr = path->nodes.size() - 1;
106   walking_speed = 0;
107 }
108
109 void
110 PathWalker::goback_node()
111 {
112   current_node_nr = next_node_nr;
113
114   if(next_node_nr > 0) {
115     next_node_nr--;
116     return;
117   }
118
119   switch(path->mode) {
120     case Path::PING_PONG:
121       walking_speed = -walking_speed;
122       return;
123     default:
124       break;
125   }
126
127   assert(false);
128   next_node_nr = 0;
129   walking_speed = 0;
130 }