/* 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 =0; this->curveType = BEZIERCURVE; this->nodeCount = 0; childCount = 0; this->name = NULL; 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->currentTrackElem = firstTrackElem; this->localTime = 0; this->maxTime = 0; this->trackElemCount = 0; } /** \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) { trackElemCount = 0; this->currentTrackElem->childCount = childCount; this->currentTrackElem->children = new TrackElement*[childCount]; for (int i=0; icurrentTrackElem->children[i] = new TrackElement(); } /** \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; } this->currentTrackElem->curve->addNode(newPoint); } /** \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); } /** \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); } /** \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]; va_list ID; va_start (ID, count); for(int i = 0; i < count; i++) { trackIDs[i] = va_arg (ID, int); } va_end(ID); this->forkV(count, trackIDs); 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; 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 { return this->currentTrackElem->curve->calcPos(this->localTime); } /** \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); } /** \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) { this->localTime += dt; } /** \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) { localTime = time; } /** \brief a Function that decides which Path we should follow. \param graphID The Path to choose. */ void TrackManager::choosePath(int graphID) { }