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