1 | |
---|
2 | |
---|
3 | /* |
---|
4 | orxonox - the future of 3D-vertical-scrollers |
---|
5 | |
---|
6 | Copyright (C) 2004 orx |
---|
7 | |
---|
8 | This program is free software; you can redistribute it and/or modify |
---|
9 | it under the terms of the GNU General Public License as published by |
---|
10 | the Free Software Foundation; either version 2, or (at your option) |
---|
11 | any later version. |
---|
12 | |
---|
13 | ### File Specific: |
---|
14 | main-programmer: Patrick Boenzli |
---|
15 | co-programmer: ... |
---|
16 | */ |
---|
17 | |
---|
18 | |
---|
19 | #include "track_manager.h" |
---|
20 | #include <stdarg.h> |
---|
21 | |
---|
22 | |
---|
23 | using namespace std; |
---|
24 | |
---|
25 | /** |
---|
26 | \brief initializes a TrackElement (sets the default values) |
---|
27 | */ |
---|
28 | TrackElement::TrackElement(void) |
---|
29 | { |
---|
30 | this->isFresh = true; |
---|
31 | this->isSavePoint = false; |
---|
32 | this->isFork = false; |
---|
33 | this->isJoined = false; |
---|
34 | this->cond; //!< todo think!! |
---|
35 | this->ID = -1; |
---|
36 | this->startingTime = 0; //!< \todo eventually set this to the max time of TrackManager. |
---|
37 | this->duration = 1; |
---|
38 | this->curveType = BEZIERCURVE; |
---|
39 | this->nodeCount = 0; |
---|
40 | this->childCount = 0; |
---|
41 | this->name = NULL; |
---|
42 | this->curve = NULL; |
---|
43 | this->children = NULL; |
---|
44 | } |
---|
45 | |
---|
46 | /** |
---|
47 | \brief destroys all alocated memory) |
---|
48 | \todo eventually when deleting a TrackElement you would not like to delete all its preceding TrackElements |
---|
49 | */ |
---|
50 | TrackElement::~TrackElement(void) |
---|
51 | { |
---|
52 | if (this->name) |
---|
53 | delete []name; |
---|
54 | if (this->curve) |
---|
55 | delete this->curve; |
---|
56 | if (this->childCount > 0) |
---|
57 | { |
---|
58 | for (int i=0; i < this->childCount; i++) |
---|
59 | delete this->children[i]; |
---|
60 | } |
---|
61 | if (children) |
---|
62 | delete children; |
---|
63 | } |
---|
64 | |
---|
65 | /** |
---|
66 | \brief Searches through all the TrackElements for trackID. |
---|
67 | \param trackID The ID to search for. |
---|
68 | \returns The TrackElement if Found, NULL otherwise. |
---|
69 | |
---|
70 | \todo make this more modular, to search for different things |
---|
71 | */ |
---|
72 | TrackElement* TrackElement::findByID(unsigned int trackID) |
---|
73 | { |
---|
74 | // return if Found. |
---|
75 | if (this->ID == trackID) |
---|
76 | return this; |
---|
77 | // search on. |
---|
78 | if (this->childCount > 0) |
---|
79 | for (int i=0; i < this->childCount; i++) |
---|
80 | { |
---|
81 | TrackElement* tmpElem; |
---|
82 | if ((tmpElem = this->children[i]->findByID(trackID))) |
---|
83 | return tmpElem; |
---|
84 | } |
---|
85 | else return NULL; |
---|
86 | } |
---|
87 | |
---|
88 | |
---|
89 | |
---|
90 | |
---|
91 | |
---|
92 | ///////////////////////////////////// |
---|
93 | ///// TRACKMANAGER ////////////////// |
---|
94 | ///////////////////////////////////// |
---|
95 | /** |
---|
96 | \brief standard constructor |
---|
97 | |
---|
98 | */ |
---|
99 | TrackManager::TrackManager () |
---|
100 | { |
---|
101 | this->setClassName ("TrackManager"); |
---|
102 | |
---|
103 | PRINTF(3)("Initializing the TrackManager\n"); |
---|
104 | this->firstTrackElem = new TrackElement(); |
---|
105 | this->firstTrackElem->ID = 1; |
---|
106 | this->currentTrackElem = firstTrackElem; |
---|
107 | this->localTime = 0; |
---|
108 | this->maxTime = 0; |
---|
109 | this->trackElemCount = 1; |
---|
110 | } |
---|
111 | |
---|
112 | |
---|
113 | /** |
---|
114 | \brief standard destructor |
---|
115 | |
---|
116 | */ |
---|
117 | TrackManager::~TrackManager () |
---|
118 | { |
---|
119 | PRINTF(3)("Destruct TrackManager\n"); |
---|
120 | |
---|
121 | PRINTF(3)("Deleting all the TrackElements\n"); |
---|
122 | delete this->firstTrackElem; |
---|
123 | |
---|
124 | // we do not have a TrackManager anymore |
---|
125 | singletonRef = NULL; |
---|
126 | } |
---|
127 | |
---|
128 | TrackManager* TrackManager::singletonRef = NULL; |
---|
129 | |
---|
130 | /** |
---|
131 | \returns The reference on the TrackManager. |
---|
132 | |
---|
133 | If the TrackManager does not exist, it will be created. |
---|
134 | */ |
---|
135 | TrackManager* TrackManager::getInstance(void) |
---|
136 | { |
---|
137 | if (singletonRef) |
---|
138 | return singletonRef; |
---|
139 | else |
---|
140 | return singletonRef = new TrackManager(); |
---|
141 | } |
---|
142 | |
---|
143 | /** |
---|
144 | \brief reserves Space for childCount children |
---|
145 | \param childCount The Count of children to make space for. |
---|
146 | */ |
---|
147 | void TrackManager::initChildren(unsigned int childCount) |
---|
148 | { |
---|
149 | this->currentTrackElem->childCount = childCount; |
---|
150 | this->currentTrackElem->children = new TrackElement*[childCount]; |
---|
151 | for (int i=0; i<childCount; i++) |
---|
152 | { |
---|
153 | this->currentTrackElem->children[i] = new TrackElement(); |
---|
154 | this->currentTrackElem->children[i]->ID = ++trackElemCount; |
---|
155 | this->currentTrackElem->children[i]->startingTime = this->currentTrackElem->startingTime+this->currentTrackElem->duration; |
---|
156 | } |
---|
157 | } |
---|
158 | |
---|
159 | /** |
---|
160 | \brief Searches for a given trackID. |
---|
161 | \param trackID the trackID to search for. |
---|
162 | \returns The TrackElement #trackID if found, NULL otherwise. |
---|
163 | */ |
---|
164 | TrackElement* TrackManager::findTrackElementByID(unsigned int trackID) const |
---|
165 | { |
---|
166 | return firstTrackElem->findByID(trackID); |
---|
167 | } |
---|
168 | |
---|
169 | // INITIALIZE // |
---|
170 | |
---|
171 | /** |
---|
172 | \brief Sets the trackID we are working on. |
---|
173 | \param trackID the trackID we are working on |
---|
174 | */ |
---|
175 | void TrackManager::workOn(unsigned int trackID) |
---|
176 | { |
---|
177 | this->currentTrackElem = findTrackElementByID(trackID); |
---|
178 | } |
---|
179 | |
---|
180 | /** |
---|
181 | \brief Sets the Type of the Curve |
---|
182 | \brief curveType The Type to set |
---|
183 | */ |
---|
184 | void TrackManager::setCurveType(CurveType curveType) |
---|
185 | { |
---|
186 | if (!this->currentTrackElem->isFresh) |
---|
187 | { |
---|
188 | PRINTF(2)("It is not possible to change the type of a Curve after you have have appended some points to it\n"); |
---|
189 | return; |
---|
190 | } |
---|
191 | this->currentTrackElem->curveType = curveType; |
---|
192 | switch (curveType) |
---|
193 | { |
---|
194 | case BEZIERCURVE: |
---|
195 | this->currentTrackElem->curve = new BezierCurve(); |
---|
196 | break; |
---|
197 | case UPOINTCURVE: |
---|
198 | this->currentTrackElem->curve = new UPointCurve(); |
---|
199 | break; |
---|
200 | } |
---|
201 | } |
---|
202 | |
---|
203 | /** |
---|
204 | \brief Sets the duration of the current path in seconds. |
---|
205 | \param time The duration in seconds. |
---|
206 | */ |
---|
207 | |
---|
208 | void TrackManager::setDuration(float time) |
---|
209 | { |
---|
210 | this->currentTrackElem->duration = time; |
---|
211 | } |
---|
212 | |
---|
213 | /** |
---|
214 | \brief adds a point to the current TrackElement |
---|
215 | \param newPoint The point to add. |
---|
216 | */ |
---|
217 | bool TrackManager::addPoint(Vector newPoint) |
---|
218 | { |
---|
219 | if (this->currentTrackElem->isFresh) |
---|
220 | { |
---|
221 | this->setCurveType(BEZIERCURVE); |
---|
222 | this->currentTrackElem->isFresh = false; |
---|
223 | } |
---|
224 | this->currentTrackElem->curve->addNode(newPoint); |
---|
225 | } |
---|
226 | |
---|
227 | /** |
---|
228 | \brief adds save/splitpoint. |
---|
229 | \param newPoint The point to add. |
---|
230 | \returns A Pointer to a newly appended Curve |
---|
231 | */ |
---|
232 | int TrackManager::addHotPoint(Vector newPoint) |
---|
233 | { |
---|
234 | if (this->currentTrackElem->isFresh) |
---|
235 | { |
---|
236 | this->setCurveType(BEZIERCURVE); |
---|
237 | this->currentTrackElem->isFresh = false; |
---|
238 | } |
---|
239 | |
---|
240 | // \todo HotPoint Handling. |
---|
241 | this->currentTrackElem->curve->addNode(newPoint); |
---|
242 | } |
---|
243 | |
---|
244 | /** |
---|
245 | \brief Sets the last HotPoint into a savePoint. |
---|
246 | \returns A Pointer to a newly appended Curve |
---|
247 | |
---|
248 | If no HotPoint was defined the last added Point will be rendered into a savePoint. \n |
---|
249 | If the HotPoint was defined as a fork the Point will \b not be set into a savePoint. |
---|
250 | */ |
---|
251 | int TrackManager::setSavePoint(void) |
---|
252 | { |
---|
253 | if (this->currentTrackElem->isFork || this->currentTrackElem->isSavePoint) |
---|
254 | return this->currentTrackElem->children[1]->ID; |
---|
255 | this->currentTrackElem->isSavePoint = true; |
---|
256 | |
---|
257 | this->initChildren(1); |
---|
258 | this->currentTrackElem = this->currentTrackElem->children[0]; |
---|
259 | } |
---|
260 | |
---|
261 | /** |
---|
262 | \brief adds some interessting non-linear movments through the level. |
---|
263 | \param count The Count of childrens the current HotPoint will have. |
---|
264 | |
---|
265 | If no HotPoint was defined the last added Point will be rendered into a fork. \n |
---|
266 | If the HotPoint was defined as a savePoint the Point will \b not be set into a fork. |
---|
267 | */ |
---|
268 | void TrackManager::fork(unsigned int count, ...) |
---|
269 | { |
---|
270 | int* trackIDs = new int [count]; |
---|
271 | va_list ID; |
---|
272 | va_start (ID, count); |
---|
273 | for(int i = 0; i < count; i++) |
---|
274 | { |
---|
275 | trackIDs[i] = va_arg (ID, int); |
---|
276 | } |
---|
277 | va_end(ID); |
---|
278 | this->forkV(count, trackIDs); |
---|
279 | delete []trackIDs; |
---|
280 | } |
---|
281 | |
---|
282 | /** |
---|
283 | \brief adds some interessting non-linear movments through the level. |
---|
284 | \param count The Count of childrens the current HotPoint will have. |
---|
285 | \param trackIDs A Pointer to an Array of ints which will hold the trackID's (the user will have to reserve space for this). |
---|
286 | |
---|
287 | \see void TrackManager::fork(int count, ...) |
---|
288 | |
---|
289 | \todo initialisation is wrong!! also in setSavePoint. |
---|
290 | */ |
---|
291 | void TrackManager::forkV(unsigned int count, int* trackIDs) |
---|
292 | { |
---|
293 | if (this->currentTrackElem->isSavePoint) |
---|
294 | return; |
---|
295 | this->currentTrackElem->isFork = true; |
---|
296 | |
---|
297 | this->initChildren(count); |
---|
298 | } |
---|
299 | |
---|
300 | /** |
---|
301 | \brief decides under what condition a certain Path will be chosen. |
---|
302 | \param groupID the ID on which to choose the preceding move |
---|
303 | \param cond \todo think about this |
---|
304 | */ |
---|
305 | void TrackManager::condition(unsigned int groupID, PathCondition cond) |
---|
306 | { |
---|
307 | |
---|
308 | } |
---|
309 | |
---|
310 | /** |
---|
311 | \brief joins some tracks together again. |
---|
312 | \param count The count of Paths to join. |
---|
313 | |
---|
314 | Join will set the localTime to the longest time a Path has to get to this Point. \n |
---|
315 | Join will join all curves to the first curve. |
---|
316 | */ |
---|
317 | void TrackManager::join(unsigned int count, ...) |
---|
318 | { |
---|
319 | int* trackIDs = new int [count]; |
---|
320 | va_list ID; |
---|
321 | va_start (ID, count); |
---|
322 | for(int i = 0; i < count; i++) |
---|
323 | { |
---|
324 | trackIDs[i] = va_arg (ID, int); |
---|
325 | } |
---|
326 | va_end(ID); |
---|
327 | this->joinV(count, trackIDs); |
---|
328 | delete []trackIDs; |
---|
329 | } |
---|
330 | |
---|
331 | /** |
---|
332 | \brief joins some tracks together again. |
---|
333 | \param count The count of Paths to join. |
---|
334 | \param trackIDs an Array with the trackID's to join |
---|
335 | |
---|
336 | \see void TrackManager::join(int count, ...) |
---|
337 | */ |
---|
338 | void TrackManager::joinV(unsigned int count, int* trackIDs) |
---|
339 | { |
---|
340 | //! \todo this |
---|
341 | } |
---|
342 | |
---|
343 | // RUNTIME // |
---|
344 | |
---|
345 | /** |
---|
346 | \brief calculates the Position for the localTime of the Track. |
---|
347 | \returns the calculated Position |
---|
348 | */ |
---|
349 | Vector TrackManager::calcPos() const |
---|
350 | { |
---|
351 | // PRINTF(0)("TrackElement:%d, localTime: %f\n",this->currentTrackElem->ID, this->localTime); |
---|
352 | return this->currentTrackElem->curve->calcPos((this->localTime-this->currentTrackElem->startingTime)/this->currentTrackElem->duration); |
---|
353 | } |
---|
354 | |
---|
355 | /** |
---|
356 | \brief calculates the Rotation for the localTime of the Track. |
---|
357 | \returns the calculated Rotation |
---|
358 | */ |
---|
359 | Vector TrackManager::calcDir() const |
---|
360 | { |
---|
361 | return this->currentTrackElem->curve->calcDir((this->localTime-this->currentTrackElem->startingTime)/this->currentTrackElem->duration); |
---|
362 | } |
---|
363 | |
---|
364 | /** |
---|
365 | \brief Advances the local-time of the Track around dt |
---|
366 | \param dt The time about which to advance. |
---|
367 | |
---|
368 | This function also checks, if the TrackElement has to be changed. |
---|
369 | */ |
---|
370 | void TrackManager::tick(float dt) |
---|
371 | { |
---|
372 | if (this->localTime <= this->firstTrackElem->duration) |
---|
373 | this->jumpTo(this->localTime); |
---|
374 | this->localTime += dt; |
---|
375 | if (this->localTime > this->currentTrackElem->startingTime + this->currentTrackElem->duration && this->currentTrackElem->childCount > 0) |
---|
376 | this->currentTrackElem = this->currentTrackElem->children[0]; |
---|
377 | } |
---|
378 | |
---|
379 | /** |
---|
380 | \brief Jumps to a certain point on the Track. |
---|
381 | \param time The time on the Track to jump to. |
---|
382 | |
---|
383 | This should be used to Jump backwards on a Track, because moving forward means to change between the Path. (it then tries to choose the default.) |
---|
384 | Max is trackLengthMax. |
---|
385 | */ |
---|
386 | void TrackManager::jumpTo(float time) |
---|
387 | { |
---|
388 | if (time == 0) |
---|
389 | this->currentTrackElem = this->firstTrackElem; |
---|
390 | this->localTime = time; |
---|
391 | } |
---|
392 | |
---|
393 | /** |
---|
394 | \brief a Function that decides which Path we should follow. |
---|
395 | \param graphID The Path to choose. |
---|
396 | |
---|
397 | */ |
---|
398 | void TrackManager::choosePath(int graphID) |
---|
399 | { |
---|
400 | |
---|
401 | } |
---|
402 | |
---|