Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/parenting/src/track_manager.cc @ 3346

Last change on this file since 3346 was 3335, checked in by bensch, 20 years ago

orxonox/branches/parenting: added TrackManager to world.cc. (just the constructor), and moved some functions.

File size: 9.2 KB
Line 
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
23using namespace std;
24
25/**
26   \brief initializes a TrackElement (sets the default values)
27*/
28TrackElement::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 =0;
38  this->curveType = BEZIERCURVE;
39  this->nodeCount = 0;
40  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*/
50TrackElement::~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*/
72TrackElement* 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*/
99TrackManager::TrackManager () 
100{
101  this->setClassName ("TrackManager");
102
103  PRINTF(3)("Initializing the TrackManager\n");
104  this->firstTrackElem = new TrackElement;
105  this->currentTrackElem = firstTrackElem;
106  this->localTime = 0;
107  this->maxTime = 0;
108  this->trackElemCount = 0;
109}
110
111
112/**
113   \brief standard destructor
114
115*/
116TrackManager::~TrackManager () 
117{
118  PRINTF(3)("Destruct TrackManager\n");
119
120  PRINTF(3)("Deleting all the TrackElements\n");
121  delete this->firstTrackElem;
122
123  // we do not have a TrackManager anymore
124  singletonRef = NULL;
125}
126
127TrackManager* TrackManager::singletonRef = NULL;
128
129/**
130   \returns The reference on the TrackManager.
131
132   If the TrackManager does not exist, it will be created.
133*/
134TrackManager* TrackManager::getInstance(void) 
135{
136  if (singletonRef)
137    return singletonRef;
138  else
139    return singletonRef = new TrackManager();
140}
141
142/**
143   \brief reserves Space for childCount children
144   \param childCount The Count of children to make space for.
145*/
146void TrackManager::initChildren(unsigned int childCount)
147{
148  trackElemCount = 0;
149  this->currentTrackElem->childCount = childCount;
150  this->currentTrackElem->children = new TrackElement*[childCount];
151  for (int i=0; i<childCount; i++)
152    this->currentTrackElem->children[i] = new TrackElement();
153}
154
155/**
156   \brief Searches for a given trackID.
157   \param trackID the trackID to search for.
158   \returns The TrackElement #trackID if found, NULL otherwise.
159*/
160TrackElement* TrackManager::findTrackElementByID(unsigned int trackID) const
161{
162  return firstTrackElem->findByID(trackID);
163}
164
165// INITIALIZE //
166
167/**
168   \brief Sets the trackID we are working on.
169   \param trackID the trackID we are working on
170*/
171void TrackManager::workOn(unsigned int trackID)
172{
173  this->currentTrackElem = findTrackElementByID(trackID);
174}
175
176/**
177   \brief Sets the Type of the Curve
178   \brief curveType The Type to set
179*/
180void TrackManager::setCurveType(CurveType curveType)
181{
182  if (!this->currentTrackElem->isFresh)
183    {
184      PRINTF(2)("It is not possible to change the type of a Curve after you have have appended some points to it\n");
185      return;
186    }
187  this->currentTrackElem->curveType = curveType;
188  switch (curveType)
189    {
190    case BEZIERCURVE:
191      this->currentTrackElem->curve = new BezierCurve();
192      break;
193    case UPOINTCURVE:
194      this->currentTrackElem->curve = new UPointCurve();
195      break;
196    }
197}
198
199/**
200   \brief Sets the duration of the current path in seconds.
201   \param time The duration in seconds.
202*/
203
204void TrackManager::setDuration(float time)
205{
206  this->currentTrackElem->duration = time;
207}
208
209/**
210   \brief adds a point to the current TrackElement
211   \param newPoint The point to add.
212*/
213bool TrackManager::addPoint(Vector newPoint)
214{
215  if (this->currentTrackElem->isFresh)
216    {
217      this->setCurveType(BEZIERCURVE);
218      this->currentTrackElem->isFresh = false;
219    }
220  this->currentTrackElem->curve->addNode(newPoint);
221}
222
223/**
224   \brief adds save/splitpoint.
225   \param newPoint The point to add.
226   \returns A Pointer to a newly appended Curve
227*/
228int TrackManager::addHotPoint(Vector newPoint)
229{
230  if (this->currentTrackElem->isFresh)
231    {
232      this->setCurveType(BEZIERCURVE);
233      this->currentTrackElem->isFresh = false;
234    }
235
236  // \todo HotPoint Handling.
237  this->currentTrackElem->curve->addNode(newPoint);
238}
239
240/**
241   \brief Sets the last HotPoint into a savePoint.
242   \returns A Pointer to a newly appended Curve
243   
244   If no HotPoint was defined the last added Point will be rendered into a savePoint. \n
245   If the HotPoint was defined as a fork the Point will \b not be set into a savePoint.
246*/
247int TrackManager::setSavePoint(void)
248{
249  if (this->currentTrackElem->isFork || this->currentTrackElem->isSavePoint)
250    return this->currentTrackElem->children[1]->ID;
251  this->currentTrackElem->isSavePoint = true;
252
253  this->initChildren(1);
254}
255
256/**
257   \brief adds some interessting non-linear movments through the level.
258   \param count The Count of childrens the current HotPoint will have.
259
260   If no HotPoint was defined the last added Point will be rendered into a fork. \n
261   If the HotPoint was defined as a savePoint the Point will \b not be set into a fork.
262*/
263void TrackManager::fork(unsigned int count, ...)
264{
265  int* trackIDs = new int [count];
266  va_list ID;
267  va_start (ID, count);
268  for(int i = 0; i < count; i++)
269    {
270      trackIDs[i] = va_arg (ID, int);
271    }
272  va_end(ID);
273  this->forkV(count, trackIDs);
274  delete []trackIDs;
275}
276
277/**
278   \brief adds some interessting non-linear movments through the level.
279   \param count The Count of childrens the current HotPoint will have.
280   \param trackIDs A Pointer to an Array of ints which will hold the trackID's (the user will have to reserve space for this).
281
282   \see void TrackManager::fork(int count, ...)
283
284   \todo initialisation is wrong!! also in setSavePoint.
285*/
286void TrackManager::forkV(unsigned int count, int* trackIDs)
287{
288  if (this->currentTrackElem->isSavePoint)
289    return;
290  this->currentTrackElem->isFork = true;
291
292  this->initChildren(count);
293}
294
295/**
296   \brief decides under what condition a certain Path will be chosen.
297   \param groupID the ID on which to choose the preceding move
298   \param cond \todo think about this
299*/
300void TrackManager::condition(unsigned int groupID, PathCondition cond)
301{
302 
303}
304
305/**
306   \brief joins some tracks together again.
307   \param count The count of Paths to join.
308
309   Join will set the localTime to the longest time a Path has to get to this Point. \n
310   Join will join all curves to the first curve.
311*/
312void TrackManager::join(unsigned int count, ...)
313{
314  int* trackIDs = new int [count];
315  va_list ID;
316  va_start (ID, count);
317  for(int i = 0; i < count; i++)
318    {
319      trackIDs[i] = va_arg (ID, int);
320    }
321  va_end(ID);
322  this->joinV(count, trackIDs);
323  delete []trackIDs;
324}
325
326/**
327   \brief joins some tracks together again.
328   \param count The count of Paths to join.
329   \param trackIDs an Array with the trackID's to join
330
331   \see void TrackManager::join(int count, ...)
332*/
333void TrackManager::joinV(unsigned int count, int* trackIDs)
334{
335  //! \todo this
336}
337
338// RUNTIME //
339
340/**
341   \brief calculates the Position for the localTime of the Track.
342   \returns the calculated Position
343*/
344Vector TrackManager::calcPos() const
345{
346  return this->currentTrackElem->curve->calcPos(this->localTime);
347}
348
349/**
350   \brief calculates the Rotation for the localTime of the Track.
351   \returns the calculated Rotation
352*/
353Vector TrackManager::calcDir() const
354{
355  return this->currentTrackElem->curve->calcDir(this->localTime);
356}
357
358/**
359   \brief Advances the local-time of the Track around dt
360   \param dt The time about which to advance.
361
362   This function also checks, if the TrackElement has to be changed.
363*/
364void TrackManager::tick(float dt)
365{
366  this->localTime += dt;
367}
368
369/**
370   \brief Jumps to a certain point on the Track.
371   \param time The time on the Track to jump to.
372
373   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.)
374   Max is trackLengthMax.
375*/
376void TrackManager::jumpTo(float time)
377{
378  localTime = time;
379}
380
381/**
382   \brief a Function that decides which Path we should follow.
383   \param graphID The Path to choose.
384   
385*/
386void TrackManager::choosePath(int graphID)
387{
388
389}
390
Note: See TracBrowser for help on using the repository browser.