[3311] | 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" |
---|
[3332] | 20 | #include <stdarg.h> |
---|
[3311] | 21 | |
---|
| 22 | |
---|
| 23 | using namespace std; |
---|
| 24 | |
---|
[3331] | 25 | /** |
---|
| 26 | \brief initializes a TrackElement (sets the default values) |
---|
| 27 | */ |
---|
| 28 | TrackElement::TrackElement(void) |
---|
| 29 | { |
---|
[3332] | 30 | this->isFresh = true; |
---|
[3331] | 31 | this->isSavePoint = false; |
---|
| 32 | this->isFork = false; |
---|
| 33 | this->isJoined = false; |
---|
| 34 | this->cond; //!< todo think!! |
---|
| 35 | this->ID = -1; |
---|
[3333] | 36 | this->startingTime = 0; //!< \todo eventually set this to the max time of TrackManager. |
---|
| 37 | this->duration =0; |
---|
[3331] | 38 | this->curveType = BEZIERCURVE; |
---|
| 39 | this->nodeCount = 0; |
---|
| 40 | childCount = 0; |
---|
[3332] | 41 | this->name = NULL; |
---|
[3331] | 42 | this->curve = NULL; |
---|
| 43 | this->children = NULL; |
---|
| 44 | } |
---|
[3311] | 45 | |
---|
[3331] | 46 | /** |
---|
[3332] | 47 | \brief destroys all alocated memory) |
---|
[3331] | 48 | \todo eventually when deleting a TrackElement you would not like to delete all its preceding TrackElements |
---|
| 49 | */ |
---|
| 50 | TrackElement::~TrackElement(void) |
---|
| 51 | { |
---|
[3332] | 52 | if (this->name) |
---|
| 53 | delete []name; |
---|
[3331] | 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 | } |
---|
[3333] | 61 | if (children) |
---|
| 62 | delete children; |
---|
[3331] | 63 | } |
---|
| 64 | |
---|
[3332] | 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 | */ |
---|
| 72 | TrackElement* 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 | } |
---|
[3331] | 87 | |
---|
| 88 | |
---|
| 89 | |
---|
[3332] | 90 | |
---|
| 91 | |
---|
| 92 | ///////////////////////////////////// |
---|
| 93 | ///// TRACKMANAGER ////////////////// |
---|
| 94 | ///////////////////////////////////// |
---|
[3311] | 95 | /** |
---|
| 96 | \brief standard constructor |
---|
| 97 | |
---|
| 98 | */ |
---|
| 99 | TrackManager::TrackManager () |
---|
| 100 | { |
---|
[3331] | 101 | this->setClassName ("TrackManager"); |
---|
| 102 | |
---|
| 103 | PRINTF(3)("Initializing the TrackManager\n"); |
---|
[3335] | 104 | this->firstTrackElem = new TrackElement; |
---|
[3331] | 105 | this->currentTrackElem = firstTrackElem; |
---|
| 106 | this->localTime = 0; |
---|
| 107 | this->maxTime = 0; |
---|
| 108 | this->trackElemCount = 0; |
---|
[3311] | 109 | } |
---|
| 110 | |
---|
| 111 | |
---|
| 112 | /** |
---|
[3331] | 113 | \brief standard destructor |
---|
[3311] | 114 | |
---|
| 115 | */ |
---|
[3330] | 116 | TrackManager::~TrackManager () |
---|
| 117 | { |
---|
[3331] | 118 | PRINTF(3)("Destruct TrackManager\n"); |
---|
[3311] | 119 | |
---|
[3331] | 120 | PRINTF(3)("Deleting all the TrackElements\n"); |
---|
| 121 | delete this->firstTrackElem; |
---|
[3335] | 122 | |
---|
[3331] | 123 | // we do not have a TrackManager anymore |
---|
| 124 | singletonRef = NULL; |
---|
[3330] | 125 | } |
---|
| 126 | |
---|
[3331] | 127 | TrackManager* TrackManager::singletonRef = NULL; |
---|
| 128 | |
---|
[3330] | 129 | /** |
---|
[3331] | 130 | \returns The reference on the TrackManager. |
---|
| 131 | |
---|
| 132 | If the TrackManager does not exist, it will be created. |
---|
| 133 | */ |
---|
| 134 | TrackManager* TrackManager::getInstance(void) |
---|
| 135 | { |
---|
| 136 | if (singletonRef) |
---|
| 137 | return singletonRef; |
---|
| 138 | else |
---|
| 139 | return singletonRef = new TrackManager(); |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | /** |
---|
[3335] | 143 | \brief reserves Space for childCount children |
---|
| 144 | \param childCount The Count of children to make space for. |
---|
| 145 | */ |
---|
| 146 | void 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 | /** |
---|
[3330] | 156 | \brief Searches for a given trackID. |
---|
| 157 | \param trackID the trackID to search for. |
---|
[3332] | 158 | \returns The TrackElement #trackID if found, NULL otherwise. |
---|
[3330] | 159 | */ |
---|
[3332] | 160 | TrackElement* TrackManager::findTrackElementByID(unsigned int trackID) const |
---|
[3330] | 161 | { |
---|
[3332] | 162 | return firstTrackElem->findByID(trackID); |
---|
[3330] | 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 | */ |
---|
[3332] | 171 | void TrackManager::workOn(unsigned int trackID) |
---|
[3330] | 172 | { |
---|
[3332] | 173 | this->currentTrackElem = findTrackElementByID(trackID); |
---|
[3330] | 174 | } |
---|
| 175 | |
---|
| 176 | /** |
---|
| 177 | \brief Sets the Type of the Curve |
---|
| 178 | \brief curveType The Type to set |
---|
| 179 | */ |
---|
[3332] | 180 | void TrackManager::setCurveType(CurveType curveType) |
---|
[3330] | 181 | { |
---|
[3332] | 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 | } |
---|
[3330] | 197 | } |
---|
| 198 | |
---|
| 199 | /** |
---|
[3333] | 200 | \brief Sets the duration of the current path in seconds. |
---|
| 201 | \param time The duration in seconds. |
---|
[3330] | 202 | */ |
---|
| 203 | |
---|
[3333] | 204 | void TrackManager::setDuration(float time) |
---|
[3330] | 205 | { |
---|
[3333] | 206 | this->currentTrackElem->duration = time; |
---|
[3330] | 207 | } |
---|
| 208 | |
---|
| 209 | /** |
---|
| 210 | \brief adds a point to the current TrackElement |
---|
| 211 | \param newPoint The point to add. |
---|
| 212 | */ |
---|
[3333] | 213 | bool TrackManager::addPoint(Vector newPoint) |
---|
[3330] | 214 | { |
---|
[3332] | 215 | if (this->currentTrackElem->isFresh) |
---|
| 216 | { |
---|
| 217 | this->setCurveType(BEZIERCURVE); |
---|
| 218 | this->currentTrackElem->isFresh = false; |
---|
| 219 | } |
---|
| 220 | this->currentTrackElem->curve->addNode(newPoint); |
---|
[3330] | 221 | } |
---|
| 222 | |
---|
| 223 | /** |
---|
| 224 | \brief adds save/splitpoint. |
---|
| 225 | \param newPoint The point to add. |
---|
[3333] | 226 | \returns A Pointer to a newly appended Curve |
---|
[3330] | 227 | */ |
---|
[3333] | 228 | int TrackManager::addHotPoint(Vector newPoint) |
---|
[3330] | 229 | { |
---|
[3332] | 230 | if (this->currentTrackElem->isFresh) |
---|
| 231 | { |
---|
| 232 | this->setCurveType(BEZIERCURVE); |
---|
| 233 | this->currentTrackElem->isFresh = false; |
---|
| 234 | } |
---|
[3330] | 235 | |
---|
[3332] | 236 | // \todo HotPoint Handling. |
---|
| 237 | this->currentTrackElem->curve->addNode(newPoint); |
---|
[3330] | 238 | } |
---|
| 239 | |
---|
| 240 | /** |
---|
| 241 | \brief Sets the last HotPoint into a savePoint. |
---|
[3333] | 242 | \returns A Pointer to a newly appended Curve |
---|
[3330] | 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 | */ |
---|
[3333] | 247 | int TrackManager::setSavePoint(void) |
---|
[3330] | 248 | { |
---|
[3332] | 249 | if (this->currentTrackElem->isFork || this->currentTrackElem->isSavePoint) |
---|
[3333] | 250 | return this->currentTrackElem->children[1]->ID; |
---|
[3332] | 251 | this->currentTrackElem->isSavePoint = true; |
---|
| 252 | |
---|
[3335] | 253 | this->initChildren(1); |
---|
[3330] | 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 | */ |
---|
[3332] | 263 | void TrackManager::fork(unsigned int count, ...) |
---|
[3330] | 264 | { |
---|
[3332] | 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; |
---|
[3330] | 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, ...) |
---|
[3332] | 283 | |
---|
| 284 | \todo initialisation is wrong!! also in setSavePoint. |
---|
[3330] | 285 | */ |
---|
[3332] | 286 | void TrackManager::forkV(unsigned int count, int* trackIDs) |
---|
[3330] | 287 | { |
---|
[3332] | 288 | if (this->currentTrackElem->isSavePoint) |
---|
| 289 | return; |
---|
| 290 | this->currentTrackElem->isFork = true; |
---|
[3330] | 291 | |
---|
[3335] | 292 | this->initChildren(count); |
---|
[3330] | 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 | */ |
---|
[3332] | 300 | void TrackManager::condition(unsigned int groupID, PathCondition cond) |
---|
[3330] | 301 | { |
---|
[3332] | 302 | |
---|
[3330] | 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 | */ |
---|
[3332] | 312 | void TrackManager::join(unsigned int count, ...) |
---|
[3330] | 313 | { |
---|
[3332] | 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; |
---|
[3330] | 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 | */ |
---|
[3332] | 333 | void TrackManager::joinV(unsigned int count, int* trackIDs) |
---|
[3330] | 334 | { |
---|
[3332] | 335 | //! \todo this |
---|
[3330] | 336 | } |
---|
| 337 | |
---|
| 338 | // RUNTIME // |
---|
| 339 | |
---|
| 340 | /** |
---|
| 341 | \brief calculates the Position for the localTime of the Track. |
---|
| 342 | \returns the calculated Position |
---|
| 343 | */ |
---|
[3332] | 344 | Vector TrackManager::calcPos() const |
---|
[3330] | 345 | { |
---|
[3333] | 346 | return this->currentTrackElem->curve->calcPos(this->localTime); |
---|
[3330] | 347 | } |
---|
| 348 | |
---|
| 349 | /** |
---|
| 350 | \brief calculates the Rotation for the localTime of the Track. |
---|
| 351 | \returns the calculated Rotation |
---|
| 352 | */ |
---|
[3332] | 353 | Vector TrackManager::calcDir() const |
---|
[3330] | 354 | { |
---|
[3333] | 355 | return this->currentTrackElem->curve->calcDir(this->localTime); |
---|
[3330] | 356 | } |
---|
| 357 | |
---|
| 358 | /** |
---|
| 359 | \brief Advances the local-time of the Track around dt |
---|
| 360 | \param dt The time about which to advance. |
---|
[3333] | 361 | |
---|
| 362 | This function also checks, if the TrackElement has to be changed. |
---|
[3330] | 363 | */ |
---|
| 364 | void TrackManager::tick(float dt) |
---|
| 365 | { |
---|
[3332] | 366 | this->localTime += dt; |
---|
[3330] | 367 | } |
---|
| 368 | |
---|
| 369 | /** |
---|
[3331] | 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 | */ |
---|
| 376 | void TrackManager::jumpTo(float time) |
---|
| 377 | { |
---|
[3332] | 378 | localTime = time; |
---|
[3331] | 379 | } |
---|
| 380 | |
---|
| 381 | /** |
---|
[3330] | 382 | \brief a Function that decides which Path we should follow. |
---|
| 383 | \param graphID The Path to choose. |
---|
| 384 | |
---|
| 385 | */ |
---|
| 386 | void TrackManager::choosePath(int graphID) |
---|
| 387 | { |
---|
| 388 | |
---|
| 389 | } |
---|
| 390 | |
---|