Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3333 was 3333, checked in by bensch, 19 years ago

orxonox/branches/parenting: :TrackManager: reimplemented some functions, and added some for the logic of it.

File size: 9.3 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 reserves Space for childCount children
67   \param childCount The Count of children to make space for.
68*/
69void TrackElement::initChildren(unsigned int childCount)
70{
71  this->childCount = childCount;
72  this->children = new TrackElement*[childCount];
73  for (int i=0; i<childCount; i++)
74    this->children[i] = new TrackElement();
75}
76
77/**
78   \brief Searches through all the TrackElements for trackID.
79   \param trackID The ID to search for.
80   \returns The TrackElement if Found, NULL otherwise.
81   
82   \todo make this more modular, to search for different things
83*/
84TrackElement* TrackElement::findByID(unsigned int trackID)
85{
86  // return if Found.
87  if (this->ID == trackID)
88    return this;
89  // search on.
90  if (this->childCount > 0)
91    for (int i=0; i < this->childCount; i++)
92      {
93        TrackElement* tmpElem;
94        if ((tmpElem = this->children[i]->findByID(trackID)))
95          return tmpElem;
96      }
97  else return NULL;
98}
99
100
101
102
103
104/////////////////////////////////////
105///// TRACKMANAGER //////////////////
106/////////////////////////////////////
107/**
108   \brief standard constructor
109
110   \todo this constructor is not jet implemented - do it
111*/
112TrackManager::TrackManager () 
113{
114  this->setClassName ("TrackManager");
115
116  PRINTF(3)("Initializing the TrackManager\n");
117  this->firstTrackElem = NULL;
118  this->currentTrackElem = firstTrackElem;
119  this->localTime = 0;
120  this->maxTime = 0;
121  this->trackElemCount = 0;
122}
123
124
125/**
126   \brief standard destructor
127
128   \todo this deconstructor is not jet implemented - do it
129*/
130TrackManager::~TrackManager () 
131{
132  PRINTF(3)("Destruct TrackManager\n");
133
134  PRINTF(3)("Deleting all the TrackElements\n");
135  delete this->firstTrackElem;
136  // we do not have a TrackManager anymore
137  singletonRef = NULL;
138}
139
140TrackManager* TrackManager::singletonRef = NULL;
141
142/**
143   \returns The reference on the TrackManager.
144
145   If the TrackManager does not exist, it will be created.
146*/
147TrackManager* TrackManager::getInstance(void) 
148{
149  if (singletonRef)
150    return singletonRef;
151  else
152    return singletonRef = new TrackManager();
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->currentTrackElem->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->currentTrackElem->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.