/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. ### File Specific: main-programmer: Patrick Boenzli co-programmer: ... */ #include "track_manager.h" #include using namespace std; /** \brief initializes a TrackElement (sets the default values) */ TrackElement::TrackElement(void) { this->isFresh = true; this->isSavePoint = false; this->isFork = false; this->isJoined = false; this->cond; //!< todo think!! this->ID = -1; this->startingTime = 0; //!< \todo eventually set this to the max time of TrackManager. this->duration = 1; this->curveType = BEZIERCURVE; this->nodeCount = 0; this->childCount = 0; this->name = NULL; this->startPoint = Vector(0,0,0); this->startTangentPoint = Vector(0,0,0); this->curve = NULL; this->children = NULL; } /** \brief destroys all alocated memory) \todo eventually when deleting a TrackElement you would not like to delete all its preceding TrackElements */ TrackElement::~TrackElement(void) { if (this->name) delete []name; if (this->curve) delete this->curve; if (this->childCount > 0) { for (int i=0; i < this->childCount; i++) delete this->children[i]; } if (children) delete children; } /** \brief Searches through all the TrackElements for trackID. \param trackID The ID to search for. \returns The TrackElement if Found, NULL otherwise. \todo make this more modular, to search for different things */ TrackElement* TrackElement::findByID(unsigned int trackID) { // return if Found. if (this->ID == trackID) return this; // search on. if (this->childCount > 0) for (int i=0; i < this->childCount; i++) { TrackElement* tmpElem; if ((tmpElem = this->children[i]->findByID(trackID))) return tmpElem; } else return NULL; } ///////////////////////////////////// ///// TRACKMANAGER ////////////////// ///////////////////////////////////// /** \brief standard constructor */ TrackManager::TrackManager () { this->setClassName ("TrackManager"); PRINTF(3)("Initializing the TrackManager\n"); this->firstTrackElem = new TrackElement(); this->firstTrackElem->ID = 1; this->currentTrackElem = firstTrackElem; this->localTime = 0; this->maxTime = 0; this->trackElemCount = 1; } /** \brief standard destructor */ TrackManager::~TrackManager () { PRINTF(3)("Destruct TrackManager\n"); PRINTF(3)("Deleting all the TrackElements\n"); delete this->firstTrackElem; // we do not have a TrackManager anymore singletonRef = NULL; } TrackManager* TrackManager::singletonRef = NULL; /** \returns The reference on the TrackManager. If the TrackManager does not exist, it will be created. */ TrackManager* TrackManager::getInstance(void) { if (singletonRef) return singletonRef; else return singletonRef = new TrackManager(); } /** \brief reserves Space for childCount children \param childCount The Count of children to make space for. */ void TrackManager::initChildren(unsigned int childCount) { this->currentTrackElem->childCount = childCount; this->currentTrackElem->children = new TrackElement*[childCount]; for (int i=0; icurrentTrackElem->children[i] = new TrackElement(); this->currentTrackElem->children[i]->ID = ++trackElemCount; this->currentTrackElem->children[i]->startingTime = this->currentTrackElem->startingTime+this->currentTrackElem->duration; this->currentTrackElem->children[i]->startPoint = this->currentTrackElem->curve->getNode(this->currentTrackElem->curve->getNodeCount()); this->currentTrackElem->children[i]->startTangentPoint = (this->currentTrackElem->children[i]->startPoint *2) - this->currentTrackElem->curve->getNode(this->currentTrackElem->curve->getNodeCount()-1); } } /** \brief Searches for a given trackID. \param trackID the trackID to search for. \returns The TrackElement #trackID if found, NULL otherwise. */ TrackElement* TrackManager::findTrackElementByID(unsigned int trackID) const { return firstTrackElem->findByID(trackID); } // INITIALIZE // /** \brief Sets the trackID we are working on. \param trackID the trackID we are working on */ void TrackManager::workOn(unsigned int trackID) { this->currentTrackElem = findTrackElementByID(trackID); } /** \brief Sets the Type of the Curve \brief curveType The Type to set */ void TrackManager::setCurveType(CurveType curveType) { if (!this->currentTrackElem->isFresh) { PRINTF(2)("It is not possible to change the type of a Curve after you have have appended some points to it\n"); return; } this->currentTrackElem->curveType = curveType; switch (curveType) { case BEZIERCURVE: this->currentTrackElem->curve = new BezierCurve(); break; case UPOINTCURVE: this->currentTrackElem->curve = new UPointCurve(); break; } } /** \brief Sets the duration of the current path in seconds. \param time The duration in seconds. */ void TrackManager::setDuration(float time) { this->currentTrackElem->duration = time; } /** \brief adds a point to the current TrackElement \param newPoint The point to add. */ bool TrackManager::addPoint(Vector newPoint) { if (this->currentTrackElem->isFresh) { this->setCurveType(BEZIERCURVE); this->currentTrackElem->isFresh = false; if(this->currentTrackElem != this->firstTrackElem) { this->addPoint(this->currentTrackElem->startPoint); this->addPoint(this->currentTrackElem->startTangentPoint); } } this->currentTrackElem->curve->addNode(newPoint); this->currentTrackElem->nodeCount++; } /** \brief adds save/splitpoint. \param newPoint The point to add. \returns A Pointer to a newly appended Curve */ int TrackManager::addHotPoint(Vector newPoint) { if (this->currentTrackElem->isFresh) { this->setCurveType(BEZIERCURVE); this->currentTrackElem->isFresh = false; } // \todo HotPoint Handling. this->currentTrackElem->curve->addNode(newPoint); this->currentTrackElem->nodeCount++; } /** \brief Sets the last HotPoint into a savePoint. \returns A Pointer to a newly appended Curve If no HotPoint was defined the last added Point will be rendered into a savePoint. \n If the HotPoint was defined as a fork the Point will \b not be set into a savePoint. */ int TrackManager::setSavePoint(void) { if (this->currentTrackElem->isFork || this->currentTrackElem->isSavePoint) return this->currentTrackElem->children[1]->ID; this->currentTrackElem->isSavePoint = true; this->initChildren(1); this->currentTrackElem = this->currentTrackElem->children[0]; } /** \brief adds some interessting non-linear movments through the level. \param count The Count of childrens the current HotPoint will have. If no HotPoint was defined the last added Point will be rendered into a fork. \n If the HotPoint was defined as a savePoint the Point will \b not be set into a fork. */ void TrackManager::fork(unsigned int count, ...) { int* trackIDs = new int[count]; this->forkV(count, trackIDs); va_list ID; va_start (ID, count); for(int i = 0; i < count; i++) { *va_arg (ID, int*) = trackIDs[i]; } va_end(ID); delete []trackIDs; } /** \brief adds some interessting non-linear movments through the level. \param count The Count of childrens the current HotPoint will have. \param trackIDs A Pointer to an Array of ints which will hold the trackID's (the user will have to reserve space for this). \see void TrackManager::fork(int count, ...) \todo initialisation is wrong!! also in setSavePoint. */ void TrackManager::forkV(unsigned int count, int* trackIDs) { if (this->currentTrackElem->isSavePoint) return; this->currentTrackElem->isFork = true; for(int i = 0; i < count; i++) trackIDs[i]=this->trackElemCount+1+i; this->initChildren(count); } /** \brief decides under what condition a certain Path will be chosen. \param groupID the ID on which to choose the preceding move \param cond \todo think about this */ void TrackManager::condition(unsigned int groupID, PathCondition cond) { } /** \brief joins some tracks together again. \param count The count of Paths to join. Join will set the localTime to the longest time a Path has to get to this Point. \n Join will join all curves to the first curve. */ void TrackManager::join(unsigned int count, ...) { int* trackIDs = new int [count]; va_list ID; va_start (ID, count); for(int i = 0; i < count; i++) { trackIDs[i] = va_arg (ID, int); } va_end(ID); this->joinV(count, trackIDs); delete []trackIDs; } /** \brief joins some tracks together again. \param count The count of Paths to join. \param trackIDs an Array with the trackID's to join \see void TrackManager::join(int count, ...) */ void TrackManager::joinV(unsigned int count, int* trackIDs) { //! \todo this } // RUNTIME // /** \brief calculates the Position for the localTime of the Track. \returns the calculated Position */ Vector TrackManager::calcPos() const { // PRINTF(0)("TrackElement:%d, localTime: %f\n",this->currentTrackElem->ID, this->localTime); return this->currentTrackElem->curve->calcPos((this->localTime-this->currentTrackElem->startingTime)/this->currentTrackElem->duration); } /** \brief calculates the Rotation for the localTime of the Track. \returns the calculated Rotation */ Vector TrackManager::calcDir() const { return this->currentTrackElem->curve->calcDir((this->localTime-this->currentTrackElem->startingTime)/this->currentTrackElem->duration); } /** \brief Advances the local-time of the Track around dt \param dt The time about which to advance. This function also checks, if the TrackElement has to be changed. */ void TrackManager::tick(float dt) { if (this->localTime <= this->firstTrackElem->duration) this->jumpTo(this->localTime); this->localTime += dt; if (this->localTime > this->currentTrackElem->startingTime + this->currentTrackElem->duration && this->currentTrackElem->childCount > 0) this->currentTrackElem = this->currentTrackElem->children[0]; } /** \brief Jumps to a certain point on the Track. \param time The time on the Track to jump to. 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.) Max is trackLengthMax. */ void TrackManager::jumpTo(float time) { if (time == 0) this->currentTrackElem = this->firstTrackElem; this->localTime = time; } /** \brief a Function that decides which Path we should follow. \param graphID The Path to choose. */ void TrackManager::choosePath(int graphID) { } // DEBUG // /** \brief Imports a model of the Graph into the OpenGL-environment. \param dt The Iterator used in seconds for Painting the Graph. This is for testing facility only. Do this if you want to see the Path inside the Level. eventually this will all be packed into a gl-list. */ void TrackManager::drawGraph(float dt) const { glBegin(GL_LINES); for (int i = 1; i <= trackElemCount; i++) { TrackElement* tmpElem = this->findTrackElementByID(i); printf("Element: %i\n", tmpElem->ID); if (tmpElem->curve) for(float f = 0.0; f < 1.0; f+=dt) { // printf("%f, %f, %f\n",trackManager->calcPos().x, trackManager->calcPos().y, trackManager->calcPos().z); Vector tmpVector = tmpElem->curve->calcPos(f); glVertex3f(tmpVector.x, tmpVector.y, tmpVector.z); } } glEnd(); } void TrackManager::debug(unsigned int level) const { printf("::CLASS TRACKMANAGER::debug information::\n"); // printf("Status is: % printf(" Consists of %d elements\n", this->trackElemCount); printf(" localTime is: %f\n", this->localTime); if (level >= 2) { for (int i = 1; i <= trackElemCount; i++) { TrackElement* tmpElem = this->findTrackElementByID(i); printf(" ::TrackElement:%i::", tmpElem->ID); if(tmpElem->name) printf("name:%s::", tmpElem->name); printf("\n TimeTable: starting at = %f; ends at = %f; duration = %f\n", tmpElem->startingTime, tmpElem->startingTime+tmpElem->duration, tmpElem->duration); printf(" consists of %d Points\n", tmpElem->nodeCount); if(tmpElem->isFresh) printf(" has not jet eddited in any way\n"); if(tmpElem->isSavePoint) printf(" is a SavePoint\n"); if(tmpElem->isFork) { printf(" is A Fork with with %d children: ", tmpElem->childCount); for(int i = 0; i < tmpElem->childCount; i++) printf("=%d= ", tmpElem->children[i]->ID); printf("\n"); } if(tmpElem->isJoined) printf(" is Joined at the End\n"); } } }