[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. |
---|
[3348] | 37 | this->duration = 1; |
---|
[3331] | 38 | this->curveType = BEZIERCURVE; |
---|
| 39 | this->nodeCount = 0; |
---|
[3348] | 40 | this->childCount = 0; |
---|
[3332] | 41 | this->name = NULL; |
---|
[3349] | 42 | this->startPoint = Vector(0,0,0); |
---|
| 43 | this->startTangentPoint = Vector(0,0,0); |
---|
[3331] | 44 | this->curve = NULL; |
---|
| 45 | this->children = NULL; |
---|
| 46 | } |
---|
[3311] | 47 | |
---|
[3331] | 48 | /** |
---|
[3332] | 49 | \brief destroys all alocated memory) |
---|
[3331] | 50 | \todo eventually when deleting a TrackElement you would not like to delete all its preceding TrackElements |
---|
| 51 | */ |
---|
| 52 | TrackElement::~TrackElement(void) |
---|
| 53 | { |
---|
[3332] | 54 | if (this->name) |
---|
| 55 | delete []name; |
---|
[3331] | 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 | } |
---|
[3333] | 63 | if (children) |
---|
| 64 | delete children; |
---|
[3331] | 65 | } |
---|
| 66 | |
---|
[3332] | 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 | */ |
---|
| 74 | TrackElement* 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 | } |
---|
[3331] | 89 | |
---|
| 90 | |
---|
| 91 | |
---|
[3332] | 92 | |
---|
| 93 | |
---|
| 94 | ///////////////////////////////////// |
---|
| 95 | ///// TRACKMANAGER ////////////////// |
---|
| 96 | ///////////////////////////////////// |
---|
[3311] | 97 | /** |
---|
| 98 | \brief standard constructor |
---|
| 99 | |
---|
| 100 | */ |
---|
| 101 | TrackManager::TrackManager () |
---|
| 102 | { |
---|
[3331] | 103 | this->setClassName ("TrackManager"); |
---|
| 104 | |
---|
| 105 | PRINTF(3)("Initializing the TrackManager\n"); |
---|
[3348] | 106 | this->firstTrackElem = new TrackElement(); |
---|
| 107 | this->firstTrackElem->ID = 1; |
---|
[3331] | 108 | this->currentTrackElem = firstTrackElem; |
---|
| 109 | this->localTime = 0; |
---|
| 110 | this->maxTime = 0; |
---|
[3348] | 111 | this->trackElemCount = 1; |
---|
[3311] | 112 | } |
---|
| 113 | |
---|
| 114 | |
---|
| 115 | /** |
---|
[3331] | 116 | \brief standard destructor |
---|
[3311] | 117 | |
---|
| 118 | */ |
---|
[3330] | 119 | TrackManager::~TrackManager () |
---|
| 120 | { |
---|
[3331] | 121 | PRINTF(3)("Destruct TrackManager\n"); |
---|
[3311] | 122 | |
---|
[3331] | 123 | PRINTF(3)("Deleting all the TrackElements\n"); |
---|
| 124 | delete this->firstTrackElem; |
---|
[3335] | 125 | |
---|
[3331] | 126 | // we do not have a TrackManager anymore |
---|
| 127 | singletonRef = NULL; |
---|
[3330] | 128 | } |
---|
| 129 | |
---|
[3331] | 130 | TrackManager* TrackManager::singletonRef = NULL; |
---|
| 131 | |
---|
[3330] | 132 | /** |
---|
[3331] | 133 | \returns The reference on the TrackManager. |
---|
| 134 | |
---|
| 135 | If the TrackManager does not exist, it will be created. |
---|
| 136 | */ |
---|
| 137 | TrackManager* TrackManager::getInstance(void) |
---|
| 138 | { |
---|
| 139 | if (singletonRef) |
---|
| 140 | return singletonRef; |
---|
| 141 | else |
---|
| 142 | return singletonRef = new TrackManager(); |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | /** |
---|
[3335] | 146 | \brief reserves Space for childCount children |
---|
| 147 | \param childCount The Count of children to make space for. |
---|
| 148 | */ |
---|
| 149 | void 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++) |
---|
[3348] | 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; |
---|
[3349] | 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); |
---|
[3348] | 160 | } |
---|
[3335] | 161 | } |
---|
| 162 | |
---|
| 163 | /** |
---|
[3330] | 164 | \brief Searches for a given trackID. |
---|
| 165 | \param trackID the trackID to search for. |
---|
[3332] | 166 | \returns The TrackElement #trackID if found, NULL otherwise. |
---|
[3330] | 167 | */ |
---|
[3332] | 168 | TrackElement* TrackManager::findTrackElementByID(unsigned int trackID) const |
---|
[3330] | 169 | { |
---|
[3332] | 170 | return firstTrackElem->findByID(trackID); |
---|
[3330] | 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 | */ |
---|
[3332] | 179 | void TrackManager::workOn(unsigned int trackID) |
---|
[3330] | 180 | { |
---|
[3332] | 181 | this->currentTrackElem = findTrackElementByID(trackID); |
---|
[3330] | 182 | } |
---|
| 183 | |
---|
| 184 | /** |
---|
| 185 | \brief Sets the Type of the Curve |
---|
| 186 | \brief curveType The Type to set |
---|
| 187 | */ |
---|
[3332] | 188 | void TrackManager::setCurveType(CurveType curveType) |
---|
[3330] | 189 | { |
---|
[3332] | 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 | } |
---|
[3330] | 205 | } |
---|
| 206 | |
---|
| 207 | /** |
---|
[3333] | 208 | \brief Sets the duration of the current path in seconds. |
---|
| 209 | \param time The duration in seconds. |
---|
[3330] | 210 | */ |
---|
| 211 | |
---|
[3333] | 212 | void TrackManager::setDuration(float time) |
---|
[3330] | 213 | { |
---|
[3333] | 214 | this->currentTrackElem->duration = time; |
---|
[3330] | 215 | } |
---|
| 216 | |
---|
| 217 | /** |
---|
| 218 | \brief adds a point to the current TrackElement |
---|
| 219 | \param newPoint The point to add. |
---|
| 220 | */ |
---|
[3333] | 221 | bool TrackManager::addPoint(Vector newPoint) |
---|
[3330] | 222 | { |
---|
[3332] | 223 | if (this->currentTrackElem->isFresh) |
---|
| 224 | { |
---|
| 225 | this->setCurveType(BEZIERCURVE); |
---|
| 226 | this->currentTrackElem->isFresh = false; |
---|
[3349] | 227 | if(this->currentTrackElem != this->firstTrackElem) |
---|
[3350] | 228 | { |
---|
| 229 | this->addPoint(this->currentTrackElem->startPoint); |
---|
| 230 | this->addPoint(this->currentTrackElem->startTangentPoint); |
---|
| 231 | } |
---|
[3332] | 232 | } |
---|
| 233 | this->currentTrackElem->curve->addNode(newPoint); |
---|
[3330] | 234 | } |
---|
| 235 | |
---|
| 236 | /** |
---|
| 237 | \brief adds save/splitpoint. |
---|
| 238 | \param newPoint The point to add. |
---|
[3333] | 239 | \returns A Pointer to a newly appended Curve |
---|
[3330] | 240 | */ |
---|
[3333] | 241 | int TrackManager::addHotPoint(Vector newPoint) |
---|
[3330] | 242 | { |
---|
[3332] | 243 | if (this->currentTrackElem->isFresh) |
---|
| 244 | { |
---|
| 245 | this->setCurveType(BEZIERCURVE); |
---|
| 246 | this->currentTrackElem->isFresh = false; |
---|
| 247 | } |
---|
[3330] | 248 | |
---|
[3332] | 249 | // \todo HotPoint Handling. |
---|
| 250 | this->currentTrackElem->curve->addNode(newPoint); |
---|
[3330] | 251 | } |
---|
| 252 | |
---|
| 253 | /** |
---|
| 254 | \brief Sets the last HotPoint into a savePoint. |
---|
[3333] | 255 | \returns A Pointer to a newly appended Curve |
---|
[3330] | 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 | */ |
---|
[3333] | 260 | int TrackManager::setSavePoint(void) |
---|
[3330] | 261 | { |
---|
[3332] | 262 | if (this->currentTrackElem->isFork || this->currentTrackElem->isSavePoint) |
---|
[3333] | 263 | return this->currentTrackElem->children[1]->ID; |
---|
[3332] | 264 | this->currentTrackElem->isSavePoint = true; |
---|
| 265 | |
---|
[3335] | 266 | this->initChildren(1); |
---|
[3348] | 267 | this->currentTrackElem = this->currentTrackElem->children[0]; |
---|
[3330] | 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 | */ |
---|
[3332] | 277 | void TrackManager::fork(unsigned int count, ...) |
---|
[3330] | 278 | { |
---|
[3332] | 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; |
---|
[3330] | 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, ...) |
---|
[3332] | 297 | |
---|
| 298 | \todo initialisation is wrong!! also in setSavePoint. |
---|
[3330] | 299 | */ |
---|
[3332] | 300 | void TrackManager::forkV(unsigned int count, int* trackIDs) |
---|
[3330] | 301 | { |
---|
[3332] | 302 | if (this->currentTrackElem->isSavePoint) |
---|
| 303 | return; |
---|
| 304 | this->currentTrackElem->isFork = true; |
---|
[3330] | 305 | |
---|
[3335] | 306 | this->initChildren(count); |
---|
[3330] | 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 | */ |
---|
[3332] | 314 | void TrackManager::condition(unsigned int groupID, PathCondition cond) |
---|
[3330] | 315 | { |
---|
[3332] | 316 | |
---|
[3330] | 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 | */ |
---|
[3332] | 326 | void TrackManager::join(unsigned int count, ...) |
---|
[3330] | 327 | { |
---|
[3332] | 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; |
---|
[3330] | 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 | */ |
---|
[3332] | 347 | void TrackManager::joinV(unsigned int count, int* trackIDs) |
---|
[3330] | 348 | { |
---|
[3332] | 349 | //! \todo this |
---|
[3330] | 350 | } |
---|
| 351 | |
---|
| 352 | // RUNTIME // |
---|
| 353 | |
---|
| 354 | /** |
---|
| 355 | \brief calculates the Position for the localTime of the Track. |
---|
| 356 | \returns the calculated Position |
---|
| 357 | */ |
---|
[3332] | 358 | Vector TrackManager::calcPos() const |
---|
[3330] | 359 | { |
---|
[3348] | 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); |
---|
[3330] | 362 | } |
---|
| 363 | |
---|
| 364 | /** |
---|
| 365 | \brief calculates the Rotation for the localTime of the Track. |
---|
| 366 | \returns the calculated Rotation |
---|
| 367 | */ |
---|
[3332] | 368 | Vector TrackManager::calcDir() const |
---|
[3330] | 369 | { |
---|
[3348] | 370 | return this->currentTrackElem->curve->calcDir((this->localTime-this->currentTrackElem->startingTime)/this->currentTrackElem->duration); |
---|
[3330] | 371 | } |
---|
| 372 | |
---|
| 373 | /** |
---|
| 374 | \brief Advances the local-time of the Track around dt |
---|
| 375 | \param dt The time about which to advance. |
---|
[3333] | 376 | |
---|
| 377 | This function also checks, if the TrackElement has to be changed. |
---|
[3330] | 378 | */ |
---|
| 379 | void TrackManager::tick(float dt) |
---|
| 380 | { |
---|
[3348] | 381 | if (this->localTime <= this->firstTrackElem->duration) |
---|
| 382 | this->jumpTo(this->localTime); |
---|
[3332] | 383 | this->localTime += dt; |
---|
[3348] | 384 | if (this->localTime > this->currentTrackElem->startingTime + this->currentTrackElem->duration && this->currentTrackElem->childCount > 0) |
---|
| 385 | this->currentTrackElem = this->currentTrackElem->children[0]; |
---|
[3330] | 386 | } |
---|
| 387 | |
---|
| 388 | /** |
---|
[3331] | 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 | */ |
---|
| 395 | void TrackManager::jumpTo(float time) |
---|
| 396 | { |
---|
[3348] | 397 | if (time == 0) |
---|
| 398 | this->currentTrackElem = this->firstTrackElem; |
---|
| 399 | this->localTime = time; |
---|
[3331] | 400 | } |
---|
| 401 | |
---|
| 402 | /** |
---|
[3330] | 403 | \brief a Function that decides which Path we should follow. |
---|
| 404 | \param graphID The Path to choose. |
---|
| 405 | |
---|
| 406 | */ |
---|
| 407 | void TrackManager::choosePath(int graphID) |
---|
| 408 | { |
---|
| 409 | |
---|
| 410 | } |
---|
| 411 | |
---|
[3350] | 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 | */ |
---|
| 423 | void 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 | |
---|
| 443 | void 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 | } |
---|