Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/branches/parenting: TrackManager: implemented Debug-output:

  1. function draw() to draw the Track in Full
  2. function debug, that Prints information about the state of the TrackManager
File size: 12.5 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 = 1;
38  this->curveType = BEZIERCURVE;
39  this->nodeCount = 0;
40  this->childCount = 0;
41  this->name = NULL;
42  this->startPoint = Vector(0,0,0);
43  this->startTangentPoint = Vector(0,0,0);
44  this->curve = NULL;
45  this->children = NULL;
46}
47
48/**
49    \brief destroys all alocated memory)
50    \todo eventually when deleting a TrackElement you would not like to delete all its preceding TrackElements
51*/
52TrackElement::~TrackElement(void)
53{
54  if (this->name)
55    delete []name;
56  if (this->curve)
57    delete this->curve;
58  if (this->childCount > 0)
59    {
60      for (int i=0; i < this->childCount; i++)
61        delete this->children[i];
62    }
63  if (children)
64    delete children;
65}
66
67/**
68   \brief Searches through all the TrackElements for trackID.
69   \param trackID The ID to search for.
70   \returns The TrackElement if Found, NULL otherwise.
71   
72   \todo make this more modular, to search for different things
73*/
74TrackElement* TrackElement::findByID(unsigned int trackID)
75{
76  // return if Found.
77  if (this->ID == trackID)
78    return this;
79  // search on.
80  if (this->childCount > 0)
81    for (int i=0; i < this->childCount; i++)
82      {
83        TrackElement* tmpElem;
84        if ((tmpElem = this->children[i]->findByID(trackID)))
85          return tmpElem;
86      }
87  else return NULL;
88}
89
90
91
92
93
94/////////////////////////////////////
95///// TRACKMANAGER //////////////////
96/////////////////////////////////////
97/**
98   \brief standard constructor
99
100*/
101TrackManager::TrackManager () 
102{
103  this->setClassName ("TrackManager");
104
105  PRINTF(3)("Initializing the TrackManager\n");
106  this->firstTrackElem = new TrackElement();
107  this->firstTrackElem->ID = 1;
108  this->currentTrackElem = firstTrackElem;
109  this->localTime = 0;
110  this->maxTime = 0;
111  this->trackElemCount = 1;
112}
113
114
115/**
116   \brief standard destructor
117
118*/
119TrackManager::~TrackManager () 
120{
121  PRINTF(3)("Destruct TrackManager\n");
122
123  PRINTF(3)("Deleting all the TrackElements\n");
124  delete this->firstTrackElem;
125
126  // we do not have a TrackManager anymore
127  singletonRef = NULL;
128}
129
130TrackManager* TrackManager::singletonRef = NULL;
131
132/**
133   \returns The reference on the TrackManager.
134
135   If the TrackManager does not exist, it will be created.
136*/
137TrackManager* TrackManager::getInstance(void) 
138{
139  if (singletonRef)
140    return singletonRef;
141  else
142    return singletonRef = new TrackManager();
143}
144
145/**
146   \brief reserves Space for childCount children
147   \param childCount The Count of children to make space for.
148*/
149void TrackManager::initChildren(unsigned int childCount)
150{
151  this->currentTrackElem->childCount = childCount;
152  this->currentTrackElem->children = new TrackElement*[childCount];
153  for (int i=0; i<childCount; i++)
154    {
155      this->currentTrackElem->children[i] = new TrackElement();
156      this->currentTrackElem->children[i]->ID = ++trackElemCount;
157      this->currentTrackElem->children[i]->startingTime = this->currentTrackElem->startingTime+this->currentTrackElem->duration;
158      this->currentTrackElem->children[i]->startPoint = this->currentTrackElem->curve->getNode(this->currentTrackElem->curve->getNodeCount());
159      this->currentTrackElem->children[i]->startTangentPoint = (this->currentTrackElem->children[i]->startPoint *2) - this->currentTrackElem->curve->getNode(this->currentTrackElem->curve->getNodeCount()-1);
160    }
161}
162
163/**
164   \brief Searches for a given trackID.
165   \param trackID the trackID to search for.
166   \returns The TrackElement #trackID if found, NULL otherwise.
167*/
168TrackElement* TrackManager::findTrackElementByID(unsigned int trackID) const
169{
170  return firstTrackElem->findByID(trackID);
171}
172
173// INITIALIZE //
174
175/**
176   \brief Sets the trackID we are working on.
177   \param trackID the trackID we are working on
178*/
179void TrackManager::workOn(unsigned int trackID)
180{
181  this->currentTrackElem = findTrackElementByID(trackID);
182}
183
184/**
185   \brief Sets the Type of the Curve
186   \brief curveType The Type to set
187*/
188void TrackManager::setCurveType(CurveType curveType)
189{
190  if (!this->currentTrackElem->isFresh)
191    {
192      PRINTF(2)("It is not possible to change the type of a Curve after you have have appended some points to it\n");
193      return;
194    }
195  this->currentTrackElem->curveType = curveType;
196  switch (curveType)
197    {
198    case BEZIERCURVE:
199      this->currentTrackElem->curve = new BezierCurve();
200      break;
201    case UPOINTCURVE:
202      this->currentTrackElem->curve = new UPointCurve();
203      break;
204    }
205}
206
207/**
208   \brief Sets the duration of the current path in seconds.
209   \param time The duration in seconds.
210*/
211
212void TrackManager::setDuration(float time)
213{
214  this->currentTrackElem->duration = time;
215}
216
217/**
218   \brief adds a point to the current TrackElement
219   \param newPoint The point to add.
220*/
221bool TrackManager::addPoint(Vector newPoint)
222{
223  if (this->currentTrackElem->isFresh)
224    {
225      this->setCurveType(BEZIERCURVE);
226      this->currentTrackElem->isFresh = false;
227      if(this->currentTrackElem != this->firstTrackElem)
228        {
229          this->addPoint(this->currentTrackElem->startPoint);
230          this->addPoint(this->currentTrackElem->startTangentPoint);
231        }
232    }
233  this->currentTrackElem->curve->addNode(newPoint);
234}
235
236/**
237   \brief adds save/splitpoint.
238   \param newPoint The point to add.
239   \returns A Pointer to a newly appended Curve
240*/
241int TrackManager::addHotPoint(Vector newPoint)
242{
243  if (this->currentTrackElem->isFresh)
244    {
245      this->setCurveType(BEZIERCURVE);
246      this->currentTrackElem->isFresh = false;
247    }
248
249  // \todo HotPoint Handling.
250  this->currentTrackElem->curve->addNode(newPoint);
251}
252
253/**
254   \brief Sets the last HotPoint into a savePoint.
255   \returns A Pointer to a newly appended Curve
256   
257   If no HotPoint was defined the last added Point will be rendered into a savePoint. \n
258   If the HotPoint was defined as a fork the Point will \b not be set into a savePoint.
259*/
260int TrackManager::setSavePoint(void)
261{
262  if (this->currentTrackElem->isFork || this->currentTrackElem->isSavePoint)
263    return this->currentTrackElem->children[1]->ID;
264  this->currentTrackElem->isSavePoint = true;
265
266  this->initChildren(1);
267  this->currentTrackElem = this->currentTrackElem->children[0];
268}
269
270/**
271   \brief adds some interessting non-linear movments through the level.
272   \param count The Count of childrens the current HotPoint will have.
273
274   If no HotPoint was defined the last added Point will be rendered into a fork. \n
275   If the HotPoint was defined as a savePoint the Point will \b not be set into a fork.
276*/
277void TrackManager::fork(unsigned int count, ...)
278{
279  int* trackIDs = new int [count];
280  va_list ID;
281  va_start (ID, count);
282  for(int i = 0; i < count; i++)
283    {
284      trackIDs[i] = va_arg (ID, int);
285    }
286  va_end(ID);
287  this->forkV(count, trackIDs);
288  delete []trackIDs;
289}
290
291/**
292   \brief adds some interessting non-linear movments through the level.
293   \param count The Count of childrens the current HotPoint will have.
294   \param trackIDs A Pointer to an Array of ints which will hold the trackID's (the user will have to reserve space for this).
295
296   \see void TrackManager::fork(int count, ...)
297
298   \todo initialisation is wrong!! also in setSavePoint.
299*/
300void TrackManager::forkV(unsigned int count, int* trackIDs)
301{
302  if (this->currentTrackElem->isSavePoint)
303    return;
304  this->currentTrackElem->isFork = true;
305
306  this->initChildren(count);
307}
308
309/**
310   \brief decides under what condition a certain Path will be chosen.
311   \param groupID the ID on which to choose the preceding move
312   \param cond \todo think about this
313*/
314void TrackManager::condition(unsigned int groupID, PathCondition cond)
315{
316 
317}
318
319/**
320   \brief joins some tracks together again.
321   \param count The count of Paths to join.
322
323   Join will set the localTime to the longest time a Path has to get to this Point. \n
324   Join will join all curves to the first curve.
325*/
326void TrackManager::join(unsigned int count, ...)
327{
328  int* trackIDs = new int [count];
329  va_list ID;
330  va_start (ID, count);
331  for(int i = 0; i < count; i++)
332    {
333      trackIDs[i] = va_arg (ID, int);
334    }
335  va_end(ID);
336  this->joinV(count, trackIDs);
337  delete []trackIDs;
338}
339
340/**
341   \brief joins some tracks together again.
342   \param count The count of Paths to join.
343   \param trackIDs an Array with the trackID's to join
344
345   \see void TrackManager::join(int count, ...)
346*/
347void TrackManager::joinV(unsigned int count, int* trackIDs)
348{
349  //! \todo this
350}
351
352// RUNTIME //
353
354/**
355   \brief calculates the Position for the localTime of the Track.
356   \returns the calculated Position
357*/
358Vector TrackManager::calcPos() const
359{
360  //  PRINTF(0)("TrackElement:%d, localTime: %f\n",this->currentTrackElem->ID, this->localTime);
361  return this->currentTrackElem->curve->calcPos((this->localTime-this->currentTrackElem->startingTime)/this->currentTrackElem->duration);
362}
363
364/**
365   \brief calculates the Rotation for the localTime of the Track.
366   \returns the calculated Rotation
367*/
368Vector TrackManager::calcDir() const
369{
370  return this->currentTrackElem->curve->calcDir((this->localTime-this->currentTrackElem->startingTime)/this->currentTrackElem->duration);
371}
372
373/**
374   \brief Advances the local-time of the Track around dt
375   \param dt The time about which to advance.
376
377   This function also checks, if the TrackElement has to be changed.
378*/
379void TrackManager::tick(float dt)
380{
381  if (this->localTime <= this->firstTrackElem->duration)
382    this->jumpTo(this->localTime);
383  this->localTime += dt;
384  if (this->localTime > this->currentTrackElem->startingTime + this->currentTrackElem->duration && this->currentTrackElem->childCount > 0)
385    this->currentTrackElem = this->currentTrackElem->children[0];
386}
387
388/**
389   \brief Jumps to a certain point on the Track.
390   \param time The time on the Track to jump to.
391
392   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.)
393   Max is trackLengthMax.
394*/
395void TrackManager::jumpTo(float time)
396{
397  if (time == 0)
398    this->currentTrackElem = this->firstTrackElem;
399  this->localTime = time;
400}
401
402/**
403   \brief a Function that decides which Path we should follow.
404   \param graphID The Path to choose.
405   
406*/
407void TrackManager::choosePath(int graphID)
408{
409
410}
411
412
413
414// DEBUG //
415
416/**
417   \brief Imports a model of the Graph into the OpenGL-environment.
418   \param dt The Iterator used in seconds for Painting the Graph.
419
420   This is for testing facility only. Do this if you want to see the Path inside the Level.
421   eventually this will all be packed into a gl-list.
422*/
423void TrackManager::drawGraph(float dt) const
424{
425  glBegin(GL_LINES);
426  for (int i = 1; i <= trackElemCount; i++)
427    {
428      TrackElement* tmpElem = this->findTrackElementByID(i);
429      printf("Element: %i\n", tmpElem->ID);
430      if (tmpElem->curve)
431        for(float f = 0.0; f < 1.0; f+=dt)
432          {
433            //      printf("%f, %f, %f\n",trackManager->calcPos().x, trackManager->calcPos().y, trackManager->calcPos().z);
434            Vector tmpVector = tmpElem->curve->calcPos(f);
435            glVertex3f(tmpVector.x, tmpVector.y, tmpVector.z);
436          }
437    }
438  glEnd();
439 
440 
441}
442
443void TrackManager::debug(unsigned int level) const
444{
445  printf("::CLASS TRACKMANAGER::debug information::\n");
446  //  printf("Status is: %
447  printf(" Consists of %d elements\n", this->trackElemCount);
448  printf(" localTime is: %f\n", this->localTime);
449  if (level >= 2)
450    {
451      for (int i = 1; i <= trackElemCount; i++)
452        {
453          TrackElement* tmpElem = this->findTrackElementByID(i);
454          printf("  ::TrackElement:%i::", tmpElem->ID);
455          if(tmpElem->name)
456            printf("name:%s::", tmpElem->name);
457          printf("\n   TimeTable: starting at = %f; ends at = %f; duration = %f\n", tmpElem->startingTime, tmpElem->startingTime+tmpElem->duration, tmpElem->duration);
458          printf("   consists of %d Points\n", tmpElem->nodeCount);
459
460          if(tmpElem->isFresh)
461            printf("   has not jet eddited in any way\n");
462          if(tmpElem->isSavePoint)
463            printf("   is a SavePoint\n");
464          if(tmpElem->isFork)
465            printf("   is A Fork with with %d children\n", tmpElem->childCount);
466          if(tmpElem->isJoined)
467            printf("   is Joined at the End\n");
468
469                   
470        }
471    }
472}
Note: See TracBrowser for help on using the repository browser.