[3311] | 1 | /* |
---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
| 10 | |
---|
| 11 | ### File Specific: |
---|
[3433] | 12 | main-programmer: Benjamin Grauer |
---|
[3311] | 13 | co-programmer: ... |
---|
| 14 | */ |
---|
| 15 | |
---|
[3591] | 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_TRACK_MANAGER |
---|
[3311] | 17 | |
---|
| 18 | #include "track_manager.h" |
---|
[3495] | 19 | |
---|
[3608] | 20 | #include "base_object.h" |
---|
[3433] | 21 | #include "p_node.h" |
---|
[3528] | 22 | #include "track_node.h" |
---|
[3608] | 23 | #include "stdincl.h" |
---|
| 24 | #include "list.h" |
---|
[3845] | 25 | #include "text_engine.h" |
---|
[3849] | 26 | #include "t_animation.h" |
---|
[3528] | 27 | |
---|
[3495] | 28 | #include <stdarg.h> |
---|
| 29 | |
---|
[3311] | 30 | using namespace std; |
---|
| 31 | |
---|
[3331] | 32 | /** |
---|
| 33 | \brief initializes a TrackElement (sets the default values) |
---|
| 34 | */ |
---|
| 35 | TrackElement::TrackElement(void) |
---|
| 36 | { |
---|
[3332] | 37 | this->isFresh = true; |
---|
[3354] | 38 | this->isHotPoint = false; |
---|
[3331] | 39 | this->isSavePoint = false; |
---|
| 40 | this->isFork = false; |
---|
| 41 | this->isJoined = false; |
---|
[3356] | 42 | this->mainJoin = false; |
---|
[3331] | 43 | this->ID = -1; |
---|
[3588] | 44 | this->startingTime = 0; |
---|
| 45 | this->duration = TMAN_DEFAULT_DURATION; |
---|
[3433] | 46 | this->endTime = 1; |
---|
| 47 | this->jumpTime = 0; |
---|
[3596] | 48 | this->width = TMAN_DEFAULT_WIDTH; |
---|
[3331] | 49 | this->nodeCount = 0; |
---|
[3835] | 50 | this->curve = NULL; |
---|
[3348] | 51 | this->childCount = 0; |
---|
[3835] | 52 | this->children = NULL; |
---|
[3332] | 53 | this->name = NULL; |
---|
[3527] | 54 | |
---|
| 55 | this->history = NULL; |
---|
| 56 | |
---|
[3835] | 57 | this->subject = NULL; |
---|
[3522] | 58 | this->condFunc = &TrackElement::random; |
---|
[3331] | 59 | } |
---|
[3311] | 60 | |
---|
[3331] | 61 | /** |
---|
[3332] | 62 | \brief destroys all alocated memory) |
---|
[3331] | 63 | \todo eventually when deleting a TrackElement you would not like to delete all its preceding TrackElements |
---|
| 64 | */ |
---|
| 65 | TrackElement::~TrackElement(void) |
---|
| 66 | { |
---|
[3835] | 67 | // deleting the Name |
---|
| 68 | delete []name; |
---|
| 69 | // deleting the Curve |
---|
| 70 | delete this->curve; |
---|
| 71 | |
---|
| 72 | // deleting all the Children of this TrackNode. |
---|
| 73 | if ((!this->isJoined &&this->childCount > 0) |
---|
| 74 | || (this->isJoined && this->mainJoin)) // only if this is the MainJoin. |
---|
[3331] | 75 | { |
---|
[3661] | 76 | tIterator<TrackElement>* iterator = this->children->getIterator(); |
---|
| 77 | TrackElement* enumElem = iterator->nextElement(); |
---|
[3594] | 78 | while (enumElem) |
---|
| 79 | { |
---|
| 80 | delete enumElem; |
---|
[3661] | 81 | enumElem = iterator->nextElement(); |
---|
[3594] | 82 | } |
---|
[3661] | 83 | delete iterator; |
---|
[3356] | 84 | delete this->children; |
---|
[3331] | 85 | } |
---|
| 86 | } |
---|
| 87 | |
---|
[3332] | 88 | /** |
---|
| 89 | \brief Searches through all the TrackElements for trackID. |
---|
| 90 | \param trackID The ID to search for. |
---|
| 91 | \returns The TrackElement if Found, NULL otherwise. |
---|
| 92 | */ |
---|
| 93 | TrackElement* TrackElement::findByID(unsigned int trackID) |
---|
| 94 | { |
---|
| 95 | // return if Found. |
---|
| 96 | if (this->ID == trackID) |
---|
| 97 | return this; |
---|
[3835] | 98 | // search all children |
---|
[3332] | 99 | if (this->childCount > 0) |
---|
[3594] | 100 | { |
---|
[3661] | 101 | tIterator<TrackElement>* iterator = this->children->getIterator(); |
---|
| 102 | TrackElement* enumElem = iterator->nextElement(); |
---|
[3594] | 103 | TrackElement* tmpElem; |
---|
| 104 | while (enumElem) |
---|
| 105 | { |
---|
| 106 | if ((tmpElem = enumElem->findByID(trackID))) |
---|
| 107 | return tmpElem; |
---|
[3661] | 108 | enumElem = iterator->nextElement(); |
---|
[3594] | 109 | } |
---|
[3661] | 110 | delete iterator; |
---|
[3594] | 111 | } |
---|
[3835] | 112 | // if not found |
---|
[3882] | 113 | return NULL; |
---|
[3332] | 114 | } |
---|
[3331] | 115 | |
---|
| 116 | |
---|
[3522] | 117 | /** |
---|
[3835] | 118 | \brief Searches through all the TrackElements for a trackName |
---|
| 119 | \param trackName The name to search for. |
---|
| 120 | \returns The TrackElement if Found, NULL otherwise. |
---|
[3522] | 121 | */ |
---|
[3835] | 122 | TrackElement* TrackElement::findByName(const char* trackName) |
---|
[3522] | 123 | { |
---|
[3835] | 124 | // return if Found. |
---|
| 125 | if (this->name && !strcmp(this->name, trackName)) |
---|
| 126 | return this; |
---|
| 127 | // search all children |
---|
| 128 | if (this->childCount > 0) |
---|
[3522] | 129 | { |
---|
[3661] | 130 | tIterator<TrackElement>* iterator = this->children->getIterator(); |
---|
| 131 | TrackElement* enumElem = iterator->nextElement(); |
---|
[3835] | 132 | TrackElement* tmpElem; |
---|
[3594] | 133 | while (enumElem) |
---|
| 134 | { |
---|
[3835] | 135 | if ((tmpElem = enumElem->findByName(trackName))) |
---|
| 136 | return tmpElem; |
---|
[3661] | 137 | enumElem = iterator->nextElement(); |
---|
[3594] | 138 | } |
---|
[3661] | 139 | delete iterator; |
---|
[3522] | 140 | } |
---|
[3835] | 141 | // if not found |
---|
[3882] | 142 | return NULL; |
---|
[3522] | 143 | } |
---|
[3331] | 144 | |
---|
[3522] | 145 | /** |
---|
[3835] | 146 | \brief checks if there are any BackLoops in the Track (Backloops only |
---|
| 147 | \param trackElem the trackElement to check about |
---|
| 148 | \returns true if NO loop was found, false Otherwise |
---|
| 149 | You actually have to act on false!! |
---|
| 150 | it simply does this by looking if the current trackElem is found again somewhere else in the Track |
---|
| 151 | */ |
---|
| 152 | bool TrackElement::backLoopCheck(const TrackElement* trackElem, unsigned int depth) const |
---|
| 153 | { |
---|
| 154 | if(depth == 0 || this != trackElem) |
---|
| 155 | { |
---|
| 156 | if (this->children) |
---|
| 157 | { |
---|
| 158 | tIterator<TrackElement>* iterator = this->children->getIterator(); |
---|
| 159 | TrackElement* enumElem = iterator->nextElement(); |
---|
| 160 | while (enumElem) |
---|
| 161 | { |
---|
| 162 | if(!enumElem->backLoopCheck(trackElem, depth + 1)) |
---|
| 163 | return false; |
---|
| 164 | enumElem = iterator->nextElement(); |
---|
| 165 | } |
---|
| 166 | delete iterator; |
---|
| 167 | } |
---|
| 168 | } |
---|
| 169 | else |
---|
| 170 | return false; |
---|
| 171 | |
---|
| 172 | // only returns if everything worked out |
---|
| 173 | return true; |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | /** |
---|
[3594] | 177 | \param childNumber which child to return |
---|
[3835] | 178 | \returns the n-the children (starting at 0). |
---|
| 179 | Be aware, that when the trackElement has no Children, NULL will be returned |
---|
[3594] | 180 | */ |
---|
[3835] | 181 | TrackElement* TrackElement::getChild(int childCount) const |
---|
[3594] | 182 | { |
---|
[3835] | 183 | // if the the trackElement has no children return NULL. |
---|
[3594] | 184 | if (this->childCount == 0) |
---|
| 185 | return NULL; |
---|
[3835] | 186 | // we cannot return the childCount+m's Child, so we return the last. |
---|
[3594] | 187 | if (childCount > this->childCount) |
---|
| 188 | childCount = this->childCount; |
---|
| 189 | |
---|
[3832] | 190 | tIterator<TrackElement>* iterator = this->children->getIterator(); |
---|
| 191 | TrackElement* enumElem = iterator->nextElement(); |
---|
[3594] | 192 | for (int i = 0; i < childCount; i++) |
---|
[3832] | 193 | enumElem = iterator->nextElement(); |
---|
| 194 | delete iterator; |
---|
[3594] | 195 | return enumElem; |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | /** |
---|
[3588] | 199 | \param name the Name to set. |
---|
| 200 | */ |
---|
| 201 | void TrackElement::setName(const char* name) |
---|
| 202 | { |
---|
| 203 | // delete the old name |
---|
| 204 | if (this->name) |
---|
| 205 | delete []this->name; |
---|
[3835] | 206 | // if a name was given. |
---|
[3588] | 207 | if (name) |
---|
| 208 | { |
---|
| 209 | this->name = new char[strlen(name)+1]; |
---|
| 210 | strcpy(this->name, name); |
---|
| 211 | } |
---|
| 212 | else |
---|
| 213 | this->name = NULL; |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | /** |
---|
| 217 | \returns The name of this TrackElement |
---|
| 218 | */ |
---|
[3835] | 219 | const char* TrackElement::getName(void) const |
---|
[3588] | 220 | { |
---|
| 221 | return this->name; |
---|
| 222 | } |
---|
| 223 | |
---|
[3593] | 224 | /** |
---|
| 225 | \brief prints out debug information about this TrackElement |
---|
| 226 | */ |
---|
[3835] | 227 | void TrackElement::debug(void) const |
---|
[3593] | 228 | { |
---|
| 229 | PRINT(0)("--== TrackElement:%i ==--", this->ID); |
---|
| 230 | if(this->getName()) |
---|
[3842] | 231 | PRINT(0)("--++Name: %s++--", this->getName()); |
---|
[3593] | 232 | if(this->isFresh) |
---|
| 233 | PRINT(0)(" -- has not jet eddited in any way --\n"); |
---|
| 234 | PRINT(0)("\n TimeTable: startingTime=%f; endTime=%f; duration=%f; jumpTime=%f\n", this->startingTime, this->endTime, this->duration, this->jumpTime); |
---|
| 235 | PRINT(0)(" consists of %d Points\n", this->nodeCount); |
---|
| 236 | if (this->childCount == 0) |
---|
| 237 | PRINT(0)(" has no child\n"); |
---|
| 238 | else if (this->childCount == 1) |
---|
[3594] | 239 | PRINT(0)(" has 1 child: =%d=\n", this->getChild(0)->ID); |
---|
[3593] | 240 | else if (this->childCount > 1) |
---|
| 241 | { |
---|
| 242 | PRINT(0)(" has %d children: ", this->childCount); |
---|
[3832] | 243 | //TrackElement* enumElem = this->children->enumerate(); |
---|
| 244 | tIterator<TrackElement>* iterator = this->children->getIterator(); |
---|
| 245 | TrackElement* enumElem = iterator->nextElement(); |
---|
[3594] | 246 | while (enumElem) |
---|
| 247 | { |
---|
| 248 | PRINT(0)("=%d= ", enumElem->ID); |
---|
[3832] | 249 | enumElem = iterator->nextElement(); |
---|
[3594] | 250 | } |
---|
[3832] | 251 | delete iterator; |
---|
[3593] | 252 | PRINT(0)("\n"); |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | if(this->isHotPoint) |
---|
| 256 | PRINT(0)(" is a special Point:\n"); |
---|
| 257 | if(this->isSavePoint) |
---|
| 258 | PRINT(0)(" is a SavePoint\n"); |
---|
| 259 | if(this->isFork) |
---|
| 260 | { |
---|
| 261 | PRINT(0)(" is A Fork with with %d children.\n", this->childCount); |
---|
| 262 | } |
---|
| 263 | if(this->isJoined) |
---|
| 264 | PRINT(0)(" is Joined at the End\n"); |
---|
| 265 | |
---|
| 266 | if(!this->backLoopCheck(this)) /* this should not happen */ |
---|
| 267 | PRINT(2)(" THERE IS A BACKLOOP TO THIS ELEMENT\n"); |
---|
| 268 | } |
---|
[3588] | 269 | |
---|
| 270 | /** |
---|
[3522] | 271 | \brief CONDITION that chooses the first child for the decision (static) |
---|
| 272 | \param nothing Nothing in this function |
---|
| 273 | \returns the chosen child |
---|
| 274 | */ |
---|
[3835] | 275 | int TrackElement::lowest(const void* nothing) const |
---|
[3522] | 276 | { |
---|
| 277 | return 0; |
---|
| 278 | } |
---|
[3332] | 279 | |
---|
[3522] | 280 | /** |
---|
| 281 | \brief CONDITION that chooses the last child for the decision (static) |
---|
| 282 | \param nothing Nothing in this function |
---|
| 283 | \returns the chosen child |
---|
| 284 | */ |
---|
[3835] | 285 | int TrackElement::highest(const void* nothing) const |
---|
[3522] | 286 | { |
---|
| 287 | return this->childCount-1; |
---|
| 288 | } |
---|
[3332] | 289 | |
---|
[3522] | 290 | /** |
---|
| 291 | \brief CONDITION that chooses a random child for the decision (static) |
---|
| 292 | \param nothing Nothing in this function |
---|
| 293 | \returns the chosen child |
---|
| 294 | */ |
---|
[3835] | 295 | int TrackElement::random(const void* nothing) const |
---|
[3522] | 296 | { |
---|
| 297 | int i = (int)floor ((float)rand()/(float)RAND_MAX * (float)this->childCount); |
---|
| 298 | if (i >= this->childCount) |
---|
| 299 | return this->childCount-1; |
---|
| 300 | else |
---|
| 301 | return i; |
---|
| 302 | } |
---|
| 303 | |
---|
| 304 | /** |
---|
| 305 | \brief CONDITION that chooses child 0, if the node(probably Player) |
---|
| 306 | is left of its parent (z<0)) and 1/right otherwise. |
---|
| 307 | \param node The node to act upon. |
---|
| 308 | \returns the chosen child |
---|
| 309 | */ |
---|
[3835] | 310 | int TrackElement::leftRight(const void* node) const |
---|
[3522] | 311 | { |
---|
| 312 | PNode* tmpNode = (PNode*)node; |
---|
| 313 | |
---|
[3957] | 314 | if (tmpNode->getRelCoor().z < 0) |
---|
[3522] | 315 | return 0; |
---|
| 316 | else |
---|
| 317 | return 1; |
---|
| 318 | } |
---|
| 319 | |
---|
| 320 | |
---|
| 321 | /** |
---|
| 322 | \brief CONDITION that chooses the child, that has the nearest distance to the node (probably player). |
---|
| 323 | \param node The node to act upon. |
---|
| 324 | \returns the chosen child |
---|
| 325 | |
---|
| 326 | This is rather dangerous, because one must carefully set the points on the curve. |
---|
| 327 | The best Way is to set the nodes as wide away of each other as possible, |
---|
| 328 | but take into consideration, that if the nodes are to far from a center node, the center will be chosen. |
---|
| 329 | (play with this!!). |
---|
| 330 | */ |
---|
[3835] | 331 | int TrackElement::nearest(const void* node) const |
---|
[3522] | 332 | { |
---|
| 333 | PNode* tmpNode = (PNode*)node; |
---|
| 334 | |
---|
[3957] | 335 | Vector nodeRelCoord = tmpNode->getRelCoor(); |
---|
[3522] | 336 | float minDist = 100000000; |
---|
[3594] | 337 | int childNumber = 0; |
---|
| 338 | int i = 0; |
---|
| 339 | |
---|
[3832] | 340 | //TrackElement* enumElem = this->children->enumerate(); |
---|
| 341 | tIterator<TrackElement>* iterator = this->children->getIterator(); |
---|
| 342 | TrackElement* enumElem = iterator->nextElement(); |
---|
[3594] | 343 | while (enumElem) |
---|
[3522] | 344 | { |
---|
[3594] | 345 | float dist = (nodeRelCoord - enumElem->curve->getNode(4)).len(); |
---|
[3522] | 346 | if (dist < minDist) |
---|
| 347 | { |
---|
| 348 | minDist = dist; |
---|
[3594] | 349 | childNumber = i; |
---|
[3522] | 350 | } |
---|
[3594] | 351 | i++; |
---|
[3832] | 352 | enumElem = iterator->nextElement(); |
---|
[3522] | 353 | } |
---|
[3832] | 354 | delete iterator; |
---|
[3594] | 355 | |
---|
| 356 | PRINTF(4)("PathDecision with nearest algorithm: %d\n", childNumber); |
---|
| 357 | return childNumber; |
---|
[3522] | 358 | } |
---|
| 359 | |
---|
| 360 | |
---|
[3599] | 361 | //////////////////////// |
---|
| 362 | ///// TRACKMANAGER ///// |
---|
| 363 | //////////////////////// |
---|
[3311] | 364 | /** |
---|
| 365 | \brief standard constructor |
---|
| 366 | |
---|
| 367 | */ |
---|
[3354] | 368 | TrackManager::TrackManager(void) |
---|
[3311] | 369 | { |
---|
[3594] | 370 | this->setClassName("TrackManager"); |
---|
[3528] | 371 | |
---|
[3836] | 372 | TrackManager::singletonRef = this; // do this because otherwise the TrackNode cannot get The instance of the TrackManager |
---|
[3331] | 373 | |
---|
| 374 | PRINTF(3)("Initializing the TrackManager\n"); |
---|
[3836] | 375 | // setting up the First TrackElement |
---|
[3348] | 376 | this->firstTrackElem = new TrackElement(); |
---|
| 377 | this->firstTrackElem->ID = 1; |
---|
[3842] | 378 | this->firstTrackElem->setName("root"); |
---|
| 379 | |
---|
[3331] | 380 | this->currentTrackElem = firstTrackElem; |
---|
[3836] | 381 | |
---|
| 382 | this->curveType = CURVE_BEZIER; |
---|
[3331] | 383 | this->localTime = 0; |
---|
| 384 | this->maxTime = 0; |
---|
[3348] | 385 | this->trackElemCount = 1; |
---|
[3845] | 386 | |
---|
[3836] | 387 | this->trackNode = new TrackNode(); |
---|
| 388 | this->setBindSlave(this->trackNode); |
---|
[3845] | 389 | // initializing the Text |
---|
| 390 | this->trackText = TextEngine::getInstance()->createText("fonts/earth.ttf", 30, TEXT_DYNAMIC, 0, 255, 0); |
---|
| 391 | this->trackText->setAlignment(TEXT_ALIGN_SCREEN_CENTER); |
---|
| 392 | // initializing the Animation for the Text. |
---|
[3847] | 393 | this->textAnimation = new tAnimation<Text>(this->trackText, &Text::setBlending); |
---|
[3872] | 394 | this->textAnimation->addKeyFrame(1.0, 3.0, ANIM_NEG_EXP); |
---|
[3846] | 395 | this->textAnimation->addKeyFrame(0.0, .001); |
---|
[3845] | 396 | this->textAnimation->setInfinity(ANIM_INF_CONSTANT); |
---|
[3311] | 397 | } |
---|
| 398 | |
---|
| 399 | /** |
---|
[3331] | 400 | \brief standard destructor |
---|
[3311] | 401 | */ |
---|
[3354] | 402 | TrackManager::~TrackManager(void) |
---|
[3330] | 403 | { |
---|
[3331] | 404 | PRINTF(3)("Destruct TrackManager\n"); |
---|
[3311] | 405 | |
---|
[3594] | 406 | PRINTF(4)("Deleting all the TrackElements\n"); |
---|
[3331] | 407 | delete this->firstTrackElem; |
---|
[3335] | 408 | |
---|
[3836] | 409 | // the tracknode should be deleted here, but is deleted by pNode: -> null_parent |
---|
| 410 | |
---|
[3331] | 411 | // we do not have a TrackManager anymore |
---|
[3594] | 412 | TrackManager::singletonRef = NULL; |
---|
[3330] | 413 | } |
---|
| 414 | |
---|
[3543] | 415 | //! Singleton Reference to TrackManager |
---|
[3331] | 416 | TrackManager* TrackManager::singletonRef = NULL; |
---|
| 417 | |
---|
[3330] | 418 | /** |
---|
[3331] | 419 | \returns The reference on the TrackManager. |
---|
| 420 | |
---|
| 421 | If the TrackManager does not exist, it will be created. |
---|
| 422 | */ |
---|
| 423 | TrackManager* TrackManager::getInstance(void) |
---|
| 424 | { |
---|
[3594] | 425 | if (!TrackManager::singletonRef) |
---|
| 426 | TrackManager::singletonRef = new TrackManager(); |
---|
| 427 | return TrackManager::singletonRef; |
---|
[3331] | 428 | } |
---|
| 429 | |
---|
[3836] | 430 | |
---|
| 431 | // INITIALIZE // |
---|
[3331] | 432 | /** |
---|
[3335] | 433 | \brief reserves Space for childCount children |
---|
| 434 | \param childCount The Count of children to make space for. |
---|
[3836] | 435 | \param trackElem The TrackElement to appy this to. (if NULL chose this->currentTrackElement) |
---|
[3335] | 436 | */ |
---|
[3836] | 437 | void TrackManager::initChildren(unsigned int childCount, TrackElement* trackElem) |
---|
[3335] | 438 | { |
---|
[3836] | 439 | if (!trackElem) |
---|
| 440 | trackElem = this->currentTrackElem; |
---|
| 441 | |
---|
| 442 | trackElem->childCount = childCount; |
---|
| 443 | trackElem->mainJoin = true; // this tells join, that this one is the Main Join, if it tries to join multiple Tracks |
---|
| 444 | trackElem->children = new tList<TrackElement>(); |
---|
[3594] | 445 | for (int i = 0; i < childCount; i++) |
---|
[3348] | 446 | { |
---|
[3836] | 447 | // create a new Element |
---|
[3594] | 448 | TrackElement* newElem = new TrackElement(); |
---|
[3836] | 449 | // setting up the new ID |
---|
[3594] | 450 | newElem->ID = ++trackElemCount; |
---|
[3836] | 451 | // setting up the Time |
---|
| 452 | newElem->startingTime = trackElem->endTime + trackElem->jumpTime; |
---|
| 453 | // adds the conection Point |
---|
| 454 | this->addPoint(trackElem->curve->getNode(trackElem->curve->getNodeCount()), |
---|
| 455 | newElem); |
---|
| 456 | // add the new child to the childList. |
---|
| 457 | trackElem->children->add(newElem); |
---|
[3348] | 458 | } |
---|
[3836] | 459 | |
---|
[3842] | 460 | // setting the Name of the new TrackElement to the name of the last one + _childI |
---|
[3837] | 461 | |
---|
[3842] | 462 | if (trackElem->getName()) |
---|
| 463 | { |
---|
| 464 | for (int i = 0; i < trackElem->childCount; i++) |
---|
| 465 | { |
---|
| 466 | char* childName = new char[strlen(trackElem->getName())+10]; |
---|
| 467 | sprintf(childName, "%s_child%d", trackElem->getName(), i); |
---|
| 468 | trackElem->getChild(i)->setName(childName); |
---|
| 469 | } |
---|
| 470 | } |
---|
[3837] | 471 | // select the first Child to work on. |
---|
| 472 | this->currentTrackElem = trackElem->getChild(0); |
---|
[3335] | 473 | } |
---|
| 474 | |
---|
[3836] | 475 | |
---|
[3335] | 476 | /** |
---|
[3836] | 477 | \brief Sets the trackID we are working on. |
---|
| 478 | \param trackID the trackID we are working on |
---|
[3330] | 479 | */ |
---|
[3836] | 480 | void TrackManager::workOn(unsigned int trackID) |
---|
[3330] | 481 | { |
---|
[3836] | 482 | TrackElement* tmpElem = this->firstTrackElem->findByID(trackID); |
---|
| 483 | if (tmpElem) |
---|
| 484 | this->currentTrackElem = tmpElem; |
---|
| 485 | else |
---|
| 486 | PRINTF(2)("TrackElement %d not Found, leaving unchanged\n", trackID); |
---|
| 487 | PRINTF(4)("now Working on %d\n", this->currentTrackElem->ID); |
---|
[3330] | 488 | } |
---|
| 489 | |
---|
| 490 | /** |
---|
[3836] | 491 | \brief Sets the TrackElement to work on |
---|
| 492 | \param trackName the Name of the Track to work on |
---|
[3330] | 493 | */ |
---|
[3836] | 494 | void TrackManager::workOn(const char* trackName) |
---|
[3330] | 495 | { |
---|
[3836] | 496 | TrackElement* tmpElem = this->firstTrackElem->findByName(trackName); |
---|
[3355] | 497 | if (tmpElem) |
---|
| 498 | this->currentTrackElem = tmpElem; |
---|
| 499 | else |
---|
[3836] | 500 | PRINTF(2)("TrackElement %s not Found, leaving unchanged\n", trackName); |
---|
[3594] | 501 | PRINTF(4)("now Working on %d\n", this->currentTrackElem->ID); |
---|
[3330] | 502 | } |
---|
| 503 | |
---|
| 504 | /** |
---|
| 505 | \brief Sets the Type of the Curve |
---|
[3522] | 506 | \param curveType The Type to set |
---|
| 507 | \param trackElem the TrackElement that should get a new Curve. |
---|
[3836] | 508 | |
---|
| 509 | \brief this will possibly get obsolete during the process. |
---|
[3330] | 510 | */ |
---|
[3433] | 511 | void TrackManager::setCurveType(CurveType curveType, TrackElement* trackElem) |
---|
[3330] | 512 | { |
---|
[3433] | 513 | if (!trackElem->isFresh) |
---|
[3332] | 514 | { |
---|
| 515 | PRINTF(2)("It is not possible to change the type of a Curve after you have have appended some points to it\n"); |
---|
| 516 | return; |
---|
| 517 | } |
---|
[3588] | 518 | this->curveType = curveType; |
---|
[3332] | 519 | switch (curveType) |
---|
| 520 | { |
---|
[3836] | 521 | case CURVE_BEZIER: |
---|
[3433] | 522 | trackElem->curve = new BezierCurve(); |
---|
[3332] | 523 | break; |
---|
| 524 | } |
---|
[3330] | 525 | } |
---|
| 526 | |
---|
| 527 | /** |
---|
[3333] | 528 | \brief Sets the duration of the current path in seconds. |
---|
[3836] | 529 | \param duration The duration in seconds. |
---|
| 530 | \param trackElem The TrackElement to apply this to. |
---|
[3330] | 531 | */ |
---|
[3836] | 532 | void TrackManager::setDuration(float duration, TrackElement* trackElem) |
---|
[3330] | 533 | { |
---|
[3836] | 534 | if (!trackElem) |
---|
| 535 | trackElem = this->currentTrackElem; |
---|
[3330] | 536 | |
---|
[3836] | 537 | trackElem->duration = duration; |
---|
| 538 | trackElem->endTime = trackElem->startingTime + duration; |
---|
[3352] | 539 | } |
---|
| 540 | |
---|
| 541 | /** |
---|
| 542 | \brief adds a point to trackElem |
---|
| 543 | \param newPoint The point to add. |
---|
| 544 | \param trackElem The TrackElement to add the Point to |
---|
| 545 | */ |
---|
| 546 | bool TrackManager::addPoint(Vector newPoint, TrackElement* trackElem) |
---|
| 547 | { |
---|
[3836] | 548 | if (!trackElem) |
---|
| 549 | trackElem = this->currentTrackElem; |
---|
| 550 | |
---|
[3352] | 551 | if (trackElem->isFresh) |
---|
[3332] | 552 | { |
---|
[3588] | 553 | this->setCurveType(TMAN_DEFAULT_CURVETYPE, trackElem); |
---|
[3352] | 554 | trackElem->isFresh = false; |
---|
[3332] | 555 | } |
---|
[3352] | 556 | trackElem->curve->addNode(newPoint); |
---|
| 557 | trackElem->nodeCount++; |
---|
[3330] | 558 | } |
---|
| 559 | |
---|
| 560 | /** |
---|
| 561 | \brief adds save/splitpoint. |
---|
| 562 | \param newPoint The point to add. |
---|
[3333] | 563 | \returns A Pointer to a newly appended Curve |
---|
[3330] | 564 | */ |
---|
[3836] | 565 | int TrackManager::addHotPoint(Vector newPoint, TrackElement* trackElem) |
---|
[3330] | 566 | { |
---|
[3836] | 567 | if (!trackElem) |
---|
| 568 | trackElem = this->currentTrackElem; |
---|
| 569 | |
---|
[3594] | 570 | PRINTF(4)("setting up a HotPoint\n"); |
---|
[3836] | 571 | if (trackElem->isFresh) |
---|
[3332] | 572 | { |
---|
[3836] | 573 | trackElem->isFresh = false; |
---|
[3332] | 574 | } |
---|
[3330] | 575 | |
---|
[3332] | 576 | // \todo HotPoint Handling. |
---|
[3836] | 577 | trackElem->curve->addNode(newPoint); |
---|
| 578 | trackElem->nodeCount++; |
---|
[3837] | 579 | this->initChildren(1, trackElem); |
---|
[3330] | 580 | } |
---|
| 581 | |
---|
| 582 | /** |
---|
| 583 | \brief Sets the last HotPoint into a savePoint. |
---|
[3837] | 584 | \param trackElem The TrackElement to appy this to. (if NULL chose this->currentTrackElement) |
---|
[3333] | 585 | \returns A Pointer to a newly appended Curve |
---|
[3837] | 586 | |
---|
[3330] | 587 | If no HotPoint was defined the last added Point will be rendered into a savePoint. \n |
---|
| 588 | If the HotPoint was defined as a fork the Point will \b not be set into a savePoint. |
---|
| 589 | */ |
---|
[3837] | 590 | int TrackManager::setSavePoint(TrackElement* trackElem) |
---|
[3330] | 591 | { |
---|
[3837] | 592 | if (!trackElem) |
---|
| 593 | trackElem = this->currentTrackElem; |
---|
| 594 | |
---|
[3594] | 595 | PRINTF(4)("setting up a SavePoint.\n"); |
---|
[3837] | 596 | if (trackElem->isFork || trackElem->isSavePoint) |
---|
[3594] | 597 | { |
---|
[3837] | 598 | PRINTF(2)("%d is already finished \n", trackElem->ID); |
---|
| 599 | return trackElem->getChild(0)->ID; |
---|
[3594] | 600 | } |
---|
[3837] | 601 | trackElem->isSavePoint = true; |
---|
| 602 | trackElem->isHotPoint = true; |
---|
[3332] | 603 | |
---|
[3837] | 604 | this->initChildren(1, trackElem); |
---|
[3330] | 605 | } |
---|
| 606 | |
---|
| 607 | /** |
---|
| 608 | \brief adds some interessting non-linear movments through the level. |
---|
| 609 | \param count The Count of childrens the current HotPoint will have. |
---|
| 610 | |
---|
| 611 | If no HotPoint was defined the last added Point will be rendered into a fork. \n |
---|
| 612 | If the HotPoint was defined as a savePoint the Point will \b not be set into a fork. |
---|
| 613 | */ |
---|
[3332] | 614 | void TrackManager::fork(unsigned int count, ...) |
---|
[3330] | 615 | { |
---|
[3351] | 616 | int* trackIDs = new int[count]; |
---|
| 617 | this->forkV(count, trackIDs); |
---|
[3332] | 618 | va_list ID; |
---|
| 619 | va_start (ID, count); |
---|
| 620 | for(int i = 0; i < count; i++) |
---|
| 621 | { |
---|
[3351] | 622 | *va_arg (ID, int*) = trackIDs[i]; |
---|
[3332] | 623 | } |
---|
[3351] | 624 | va_end(ID); |
---|
[3332] | 625 | delete []trackIDs; |
---|
[3330] | 626 | } |
---|
| 627 | |
---|
| 628 | /** |
---|
| 629 | \brief adds some interessting non-linear movments through the level. |
---|
| 630 | \param count The Count of childrens the current HotPoint will have. |
---|
| 631 | \param trackIDs A Pointer to an Array of ints which will hold the trackID's (the user will have to reserve space for this). |
---|
[3837] | 632 | \param trackElem The TrackElement to appy this to. (if NULL chose this->currentTrackElement) |
---|
[3838] | 633 | \see TrackManager::fork(unsigned int count, ...) |
---|
[3330] | 634 | */ |
---|
[3837] | 635 | void TrackManager::forkV(unsigned int count, int* trackIDs, TrackElement* trackElem) |
---|
[3330] | 636 | { |
---|
[3837] | 637 | if (!trackElem) |
---|
| 638 | trackElem = this->currentTrackElem; |
---|
| 639 | |
---|
[3594] | 640 | PRINTF(4)("Forking with %d children\n", count); |
---|
[3837] | 641 | if (trackElem->isSavePoint) |
---|
[3332] | 642 | return; |
---|
[3837] | 643 | trackElem->isFork = true; |
---|
| 644 | trackElem->isHotPoint = true; |
---|
[3351] | 645 | for(int i = 0; i < count; i++) |
---|
| 646 | trackIDs[i]=this->trackElemCount+1+i; |
---|
[3837] | 647 | this->initChildren(count, trackElem); |
---|
[3330] | 648 | } |
---|
| 649 | |
---|
| 650 | /** |
---|
| 651 | \brief decides under what condition a certain Path will be chosen. |
---|
[3837] | 652 | \param trackID the trackID to apply this to. |
---|
[3522] | 653 | \param cond the CONDITION of the decision |
---|
| 654 | \param subject the Subject that will be decided upon with CONDITION cond. |
---|
| 655 | */ |
---|
[3837] | 656 | void TrackManager::condition(unsigned int trackID, CONDITION cond, void* subject) |
---|
[3522] | 657 | { |
---|
[3837] | 658 | this->condition(cond, subject, this->firstTrackElem->findByID(trackID)); |
---|
[3522] | 659 | } |
---|
[3837] | 660 | |
---|
[3522] | 661 | /** |
---|
| 662 | \brief decides under what condition a certain Path will be chosen. |
---|
| 663 | \param cond the CONDITION of the decision |
---|
| 664 | \param subject the Subject that will be decided upon with CONDITION cond. |
---|
[3837] | 665 | \param trackElem The TrackElement to appy this to. (if NULL chose this->currentTrackElement) |
---|
[3330] | 666 | */ |
---|
[3837] | 667 | void TrackManager::condition(CONDITION cond, void* subject, TrackElement* trackElem) |
---|
[3330] | 668 | { |
---|
[3837] | 669 | if (!trackElem) |
---|
| 670 | trackElem = this->currentTrackElem; |
---|
| 671 | |
---|
| 672 | if (!trackElem->isFork) |
---|
[3522] | 673 | { |
---|
[3837] | 674 | PRINTF(2)("%d is not a Fork, and no condition can be set in this case\n", trackElem->ID); |
---|
[3594] | 675 | return; |
---|
[3522] | 676 | } |
---|
[3594] | 677 | else |
---|
| 678 | { |
---|
| 679 | switch (cond) |
---|
| 680 | { |
---|
| 681 | case LOWEST: |
---|
[3837] | 682 | trackElem->condFunc = &TrackElement::lowest; |
---|
[3594] | 683 | break; |
---|
| 684 | case HIGHEST: |
---|
[3837] | 685 | trackElem->condFunc = &TrackElement::highest; |
---|
[3594] | 686 | break; |
---|
| 687 | case RANDOM: |
---|
[3837] | 688 | trackElem->condFunc = &TrackElement::random; |
---|
[3594] | 689 | break; |
---|
| 690 | case LEFTRIGHT: |
---|
[3837] | 691 | trackElem->condFunc = &TrackElement::leftRight; |
---|
[3594] | 692 | break; |
---|
| 693 | case NEAREST: |
---|
[3837] | 694 | trackElem->condFunc = &TrackElement::nearest; |
---|
[3594] | 695 | break; |
---|
| 696 | case ENEMYKILLED: |
---|
| 697 | break; |
---|
| 698 | } |
---|
[3837] | 699 | trackElem->subject=subject; |
---|
[3594] | 700 | } |
---|
[3330] | 701 | } |
---|
| 702 | |
---|
| 703 | /** |
---|
| 704 | \brief joins some tracks together again. |
---|
| 705 | \param count The count of Paths to join. |
---|
| 706 | |
---|
| 707 | Join will set the localTime to the longest time a Path has to get to this Point. \n |
---|
[3354] | 708 | Join will join all curves to the first curve, meaning that all the tangents will be matched. |
---|
[3330] | 709 | */ |
---|
[3332] | 710 | void TrackManager::join(unsigned int count, ...) |
---|
[3330] | 711 | { |
---|
[3332] | 712 | int* trackIDs = new int [count]; |
---|
| 713 | va_list ID; |
---|
| 714 | va_start (ID, count); |
---|
| 715 | for(int i = 0; i < count; i++) |
---|
| 716 | { |
---|
| 717 | trackIDs[i] = va_arg (ID, int); |
---|
| 718 | } |
---|
| 719 | va_end(ID); |
---|
| 720 | this->joinV(count, trackIDs); |
---|
| 721 | delete []trackIDs; |
---|
[3330] | 722 | } |
---|
| 723 | |
---|
| 724 | /** |
---|
[3841] | 725 | \brief Joins some Tracks together again. |
---|
| 726 | \param count The count of trackElements to join |
---|
| 727 | |
---|
| 728 | \see void TrackManager::join(unsigned int count, ...) |
---|
| 729 | The difference to void TrackManager::join(unsigned int count, ...) is, that this function takes |
---|
| 730 | the Names of the TrackElements as inputs and not their ID |
---|
| 731 | */ |
---|
| 732 | void TrackManager::joinc(unsigned int count, ...) |
---|
| 733 | { |
---|
| 734 | int* trackIDs = new int [count]; |
---|
| 735 | va_list NAME; |
---|
| 736 | va_start (NAME, count); |
---|
| 737 | for(int i = 0; i < count; i++) |
---|
| 738 | { |
---|
| 739 | char* name = va_arg (NAME, char*); |
---|
| 740 | TrackElement* tmpElem = this->firstTrackElem->findByName(name); |
---|
| 741 | if (tmpElem) |
---|
| 742 | trackIDs[i] = tmpElem->ID; |
---|
| 743 | else |
---|
| 744 | PRINTF(1)("Trying to join a Track, of which the name does not exist: %s\n", name); |
---|
| 745 | } |
---|
| 746 | va_end(NAME); |
---|
| 747 | this->joinV(count, trackIDs); |
---|
| 748 | delete []trackIDs; |
---|
| 749 | } |
---|
| 750 | |
---|
| 751 | |
---|
| 752 | /** |
---|
[3330] | 753 | \brief joins some tracks together again. |
---|
| 754 | \param count The count of Paths to join. |
---|
| 755 | \param trackIDs an Array with the trackID's to join |
---|
| 756 | |
---|
[3841] | 757 | \see void TrackManager::join(unsigned int count, ...) |
---|
[3330] | 758 | */ |
---|
[3332] | 759 | void TrackManager::joinV(unsigned int count, int* trackIDs) |
---|
[3330] | 760 | { |
---|
[3840] | 761 | TrackElement* tmpTrackElem; |
---|
[3880] | 762 | TrackElement* tmpJoinElem; |
---|
[3840] | 763 | for (int i = 0; i < count; i++) |
---|
| 764 | if (!this->firstTrackElem->findByID(trackIDs[i])) |
---|
| 765 | { |
---|
| 766 | PRINTF(1)("Error trying to Connect Paths that do not exist yet: %d\n Not Joining Anything", trackIDs[i]); |
---|
| 767 | return; |
---|
| 768 | } |
---|
| 769 | |
---|
| 770 | |
---|
[3594] | 771 | PRINTF(3)("Joining %d tracks and merging to Track %d\n", count, trackIDs[0]); |
---|
[3354] | 772 | |
---|
[3522] | 773 | // checking if there is a back-loop-connection and ERROR if it is. |
---|
[3840] | 774 | tmpTrackElem = this->firstTrackElem->findByID(trackIDs[0]); |
---|
[3522] | 775 | if (!tmpTrackElem->backLoopCheck(tmpTrackElem)) |
---|
[3838] | 776 | { |
---|
| 777 | PRINTF(2)("Backloop connection detected at joining trackElements\n -> TRACK WILL NOT BE JOINED\n"); |
---|
| 778 | return; |
---|
| 779 | } |
---|
[3522] | 780 | |
---|
[3838] | 781 | TrackElement* firstJoint = this->firstTrackElem->findByID(trackIDs[0]); |
---|
[3433] | 782 | float tmpLatestTime = firstJoint->endTime; |
---|
[3354] | 783 | |
---|
| 784 | Vector tmpEndPoint = firstJoint->curve->getNode(firstJoint->curve->getNodeCount()); |
---|
[3433] | 785 | Vector tmpTangentPoint = firstJoint->curve->getNode(firstJoint->curve->getNodeCount()-1); |
---|
| 786 | Vector tmpc2Point = firstJoint->curve->getNode(firstJoint->curve->getNodeCount()-2); |
---|
[3354] | 787 | firstJoint->isJoined = true; |
---|
[3433] | 788 | // firstJoint->mainJoin = true; |
---|
[3354] | 789 | if(!firstJoint->isHotPoint) |
---|
[3838] | 790 | this->setSavePoint(firstJoint); |
---|
[3433] | 791 | // Timing: |
---|
| 792 | for (int i = 0; i < count; i++) |
---|
| 793 | { |
---|
[3880] | 794 | if(tmpJoinElem = this->firstTrackElem->findByID(trackIDs[i])) |
---|
| 795 | { |
---|
| 796 | if (tmpJoinElem->childCount == 0 |
---|
| 797 | && tmpJoinElem->endTime > tmpLatestTime) |
---|
| 798 | tmpLatestTime = tmpJoinElem->endTime; |
---|
| 799 | } |
---|
[3433] | 800 | } |
---|
| 801 | // time the main Join. |
---|
| 802 | firstJoint->jumpTime = tmpLatestTime - firstJoint->endTime; |
---|
| 803 | |
---|
| 804 | // Joining: |
---|
[3354] | 805 | for (int i = 1; i < count; i++) |
---|
[3352] | 806 | { |
---|
[3880] | 807 | if( tmpJoinElem = this->firstTrackElem->findByID(trackIDs[i])) |
---|
[3354] | 808 | { |
---|
[3880] | 809 | if (tmpJoinElem->childCount > 0) |
---|
| 810 | printf("!!This Curve has children, and as such will not be joined!!\n You can try joining other childless TrackElements to this one!"); |
---|
| 811 | else |
---|
| 812 | { |
---|
| 813 | this->addPoint(tmpc2Point, tmpJoinElem); |
---|
| 814 | this->addPoint(tmpTangentPoint, tmpJoinElem); |
---|
| 815 | this->addPoint(tmpEndPoint, tmpJoinElem); |
---|
| 816 | // time all other Joins |
---|
| 817 | tmpJoinElem->jumpTime = tmpLatestTime - tmpJoinElem->endTime; |
---|
| 818 | |
---|
| 819 | //Copying Joint-Info |
---|
| 820 | tmpJoinElem->children = firstJoint->children; |
---|
| 821 | tmpJoinElem->childCount = firstJoint->childCount; |
---|
| 822 | tmpJoinElem->isSavePoint = firstJoint->isSavePoint; |
---|
| 823 | tmpJoinElem->isFork = firstJoint->isFork; |
---|
| 824 | |
---|
| 825 | tmpJoinElem->isJoined = true; |
---|
| 826 | } |
---|
[3354] | 827 | } |
---|
[3352] | 828 | } |
---|
[3838] | 829 | if(firstJoint->children) |
---|
[3594] | 830 | { |
---|
[3832] | 831 | //TrackElement* enumElem = firstJoint->children->enumerate(); |
---|
| 832 | tIterator<TrackElement>* iterator = firstJoint->children->getIterator(); |
---|
| 833 | TrackElement* enumElem = iterator->nextElement(); |
---|
[3594] | 834 | while (enumElem) |
---|
| 835 | { |
---|
[3838] | 836 | PRINTF(5)("Setting startingTime of %d to %f.\n", enumElem->ID, tmpLatestTime); |
---|
[3594] | 837 | enumElem->startingTime = tmpLatestTime; |
---|
| 838 | enumElem->endTime = tmpLatestTime + enumElem->duration; |
---|
| 839 | |
---|
[3832] | 840 | enumElem = iterator->nextElement(); |
---|
[3594] | 841 | } |
---|
[3832] | 842 | delete iterator; |
---|
[3594] | 843 | } |
---|
[3330] | 844 | } |
---|
| 845 | |
---|
[3433] | 846 | /** |
---|
| 847 | \brief finalizes the TrackSystem. after this it will not be editable anymore |
---|
| 848 | |
---|
| 849 | \todo check for any inconsistencies, output errors |
---|
| 850 | */ |
---|
| 851 | void TrackManager::finalize(void) |
---|
| 852 | { |
---|
| 853 | for (int i = 1; i<= trackElemCount ;i++) |
---|
| 854 | { |
---|
[3836] | 855 | TrackElement* tmpElem = this->firstTrackElem->findByID(i); |
---|
[3832] | 856 | if( tmpElem->childCount > 0 && tmpElem->mainJoin) |
---|
[3433] | 857 | { |
---|
[3832] | 858 | tIterator<TrackElement>* iterator = tmpElem->children->getIterator(); |
---|
| 859 | TrackElement* enumElem = iterator->nextElement(); |
---|
| 860 | //TrackElement* enumElem = tmpElem->children->enumerate(); |
---|
[3594] | 861 | while (enumElem) |
---|
[3433] | 862 | { |
---|
| 863 | |
---|
| 864 | // c1-continuity |
---|
[3594] | 865 | enumElem->curve->addNode(enumElem->curve->getNode(0) + |
---|
| 866 | ((enumElem->curve->getNode(0) - |
---|
[3433] | 867 | tmpElem->curve->getNode(tmpElem->curve->getNodeCount()-1)) |
---|
| 868 | ),2); |
---|
[3594] | 869 | enumElem->nodeCount++; |
---|
[3433] | 870 | // c2-continuity |
---|
[3594] | 871 | enumElem->curve->addNode((tmpElem->curve->getNode(tmpElem->curve->getNodeCount())- |
---|
[3433] | 872 | tmpElem->curve->getNode(tmpElem->curve->getNodeCount()-1)) * 4 + |
---|
| 873 | tmpElem->curve->getNode(tmpElem->curve->getNodeCount()-2), 3); |
---|
[3594] | 874 | enumElem->nodeCount++; |
---|
| 875 | PRINTF(5)("accelerations: %d-in: count: %d, %f, %f, %f\n %d-out: count: %d %f, %f, %f\n", |
---|
[3433] | 876 | tmpElem->ID, tmpElem->nodeCount, |
---|
| 877 | tmpElem->curve->calcAcc(0.999).x, tmpElem->curve->calcAcc(0.999).y, tmpElem->curve->calcAcc(0.999).z, |
---|
[3594] | 878 | enumElem->ID, enumElem->nodeCount, |
---|
| 879 | enumElem->curve->calcAcc(0).x, enumElem->curve->calcAcc(0).y, enumElem->curve->calcAcc(0).z); |
---|
| 880 | |
---|
[3832] | 881 | enumElem = iterator->nextElement(); |
---|
[3433] | 882 | } |
---|
[3832] | 883 | delete iterator; |
---|
[3433] | 884 | } |
---|
| 885 | } |
---|
[3838] | 886 | for (int i = 1; i <= trackElemCount;i++) |
---|
[3836] | 887 | if (this->firstTrackElem->findByID(i)->endTime > this->maxTime) |
---|
| 888 | this->maxTime = this->firstTrackElem->findByID(i)->endTime; // very bad implemented :/ |
---|
[3433] | 889 | } |
---|
| 890 | |
---|
| 891 | |
---|
[3330] | 892 | // RUNTIME // |
---|
| 893 | |
---|
| 894 | /** |
---|
| 895 | \brief calculates the Position for the localTime of the Track. |
---|
| 896 | \returns the calculated Position |
---|
| 897 | */ |
---|
[3332] | 898 | Vector TrackManager::calcPos() const |
---|
[3330] | 899 | { |
---|
[3348] | 900 | return this->currentTrackElem->curve->calcPos((this->localTime-this->currentTrackElem->startingTime)/this->currentTrackElem->duration); |
---|
[3330] | 901 | } |
---|
| 902 | |
---|
| 903 | /** |
---|
| 904 | \brief calculates the Rotation for the localTime of the Track. |
---|
| 905 | \returns the calculated Rotation |
---|
| 906 | */ |
---|
[3332] | 907 | Vector TrackManager::calcDir() const |
---|
[3330] | 908 | { |
---|
[3433] | 909 | return this->currentTrackElem->curve->calcDir((this->localTime - this->currentTrackElem->startingTime)/this->currentTrackElem->duration); |
---|
[3330] | 910 | } |
---|
| 911 | |
---|
| 912 | /** |
---|
[3596] | 913 | \returns the current Width of the track |
---|
| 914 | */ |
---|
| 915 | float TrackManager::getWidth(void) const |
---|
| 916 | { |
---|
| 917 | return this->currentTrackElem->width; |
---|
| 918 | } |
---|
| 919 | |
---|
| 920 | /** |
---|
[3330] | 921 | \brief Advances the local-time of the Track around dt |
---|
| 922 | \param dt The time about which to advance. |
---|
[3333] | 923 | |
---|
| 924 | This function also checks, if the TrackElement has to be changed. |
---|
[3330] | 925 | */ |
---|
| 926 | void TrackManager::tick(float dt) |
---|
| 927 | { |
---|
[3433] | 928 | dt /= 1000; |
---|
[3591] | 929 | PRINTF(4)("CurrentTrackID: %d, LocalTime is: %f, timestep is: %f\n", this->currentTrackElem->ID, this->localTime, dt); |
---|
[3348] | 930 | if (this->localTime <= this->firstTrackElem->duration) |
---|
| 931 | this->jumpTo(this->localTime); |
---|
[3601] | 932 | if (this->localTime <= this->maxTime) |
---|
| 933 | this->localTime += dt; |
---|
[3433] | 934 | if (this->localTime > this->currentTrackElem->endTime |
---|
| 935 | && this->currentTrackElem->children) |
---|
| 936 | { |
---|
[3527] | 937 | if (this->currentTrackElem->jumpTime != 0.0) |
---|
[3433] | 938 | this->jumpTo(this->localTime + this->currentTrackElem->jumpTime); |
---|
[3527] | 939 | // jump to the next TrackElement and also set the history of the new Element to the old one. |
---|
| 940 | TrackElement* tmpHistoryElem = this->currentTrackElem; |
---|
[3594] | 941 | this->currentTrackElem = this->currentTrackElem->getChild(this->choosePath(this->currentTrackElem)); |
---|
[3527] | 942 | this->currentTrackElem->history = tmpHistoryElem; |
---|
[3845] | 943 | if (this->currentTrackElem->getName()) |
---|
| 944 | { |
---|
| 945 | this->trackText->setText(this->currentTrackElem->getName()); |
---|
[3846] | 946 | this->textAnimation->replay(); |
---|
[3845] | 947 | } |
---|
[3433] | 948 | } |
---|
| 949 | if (this->bindSlave) |
---|
| 950 | { |
---|
| 951 | Vector tmp = this->calcPos(); |
---|
| 952 | Quaternion quat = Quaternion(this->calcDir(), Vector(this->currentTrackElem->curve->calcAcc((localTime-this->currentTrackElem->startingTime)/this->currentTrackElem->duration).x,1,this->currentTrackElem->curve->calcAcc((localTime-this->currentTrackElem->startingTime)/this->currentTrackElem->duration).z)); |
---|
[3539] | 953 | |
---|
| 954 | Vector v(0.0, 1.0, 0.0); |
---|
[3543] | 955 | Quaternion q(-PI/2, v); |
---|
[3539] | 956 | quat = quat * q; |
---|
| 957 | |
---|
[3809] | 958 | this->bindSlave->setAbsCoor(tmp); |
---|
| 959 | this->bindSlave->setAbsDir(quat); |
---|
[3433] | 960 | } |
---|
[3330] | 961 | } |
---|
| 962 | |
---|
| 963 | /** |
---|
[3331] | 964 | \brief Jumps to a certain point on the Track. |
---|
| 965 | \param time The time on the Track to jump to. |
---|
| 966 | |
---|
| 967 | 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.) |
---|
| 968 | Max is trackLengthMax. |
---|
| 969 | */ |
---|
| 970 | void TrackManager::jumpTo(float time) |
---|
| 971 | { |
---|
[3348] | 972 | if (time == 0) |
---|
[3845] | 973 | { |
---|
| 974 | this->currentTrackElem = this->firstTrackElem; |
---|
| 975 | if (this->currentTrackElem->getName()) |
---|
| 976 | { |
---|
| 977 | this->trackText->setText(this->currentTrackElem->getName()); |
---|
| 978 | this->textAnimation->play(); |
---|
| 979 | } |
---|
| 980 | } |
---|
[3348] | 981 | this->localTime = time; |
---|
[3331] | 982 | } |
---|
| 983 | |
---|
| 984 | /** |
---|
[3330] | 985 | \brief a Function that decides which Path we should follow. |
---|
[3522] | 986 | \param trackElem The Path to choose. |
---|
[3330] | 987 | |
---|
| 988 | */ |
---|
[3522] | 989 | int TrackManager::choosePath(TrackElement* trackElem) |
---|
[3330] | 990 | { |
---|
[3522] | 991 | return (trackElem->*(trackElem->condFunc))(trackElem->subject); |
---|
[3330] | 992 | } |
---|
| 993 | |
---|
[3433] | 994 | /** |
---|
| 995 | \brief Sets the PNode, that should be moved along the Tack |
---|
| 996 | \param bindSlave the PNode to set |
---|
| 997 | */ |
---|
| 998 | void TrackManager::setBindSlave(PNode* bindSlave) |
---|
| 999 | { |
---|
[3556] | 1000 | this->bindSlave = bindSlave; |
---|
[3433] | 1001 | } |
---|
[3350] | 1002 | |
---|
[3556] | 1003 | /** |
---|
| 1004 | \returns the main TrackNode |
---|
| 1005 | */ |
---|
| 1006 | PNode* TrackManager::getTrackNode(void) |
---|
| 1007 | { |
---|
| 1008 | return this->trackNode; |
---|
| 1009 | } |
---|
[3350] | 1010 | |
---|
| 1011 | // DEBUG // |
---|
| 1012 | |
---|
| 1013 | /** |
---|
| 1014 | \brief Imports a model of the Graph into the OpenGL-environment. |
---|
| 1015 | \param dt The Iterator used in seconds for Painting the Graph. |
---|
| 1016 | |
---|
| 1017 | This is for testing facility only. Do this if you want to see the Path inside the Level. |
---|
| 1018 | eventually this will all be packed into a gl-list. |
---|
| 1019 | */ |
---|
| 1020 | void TrackManager::drawGraph(float dt) const |
---|
| 1021 | { |
---|
| 1022 | for (int i = 1; i <= trackElemCount; i++) |
---|
| 1023 | { |
---|
[3352] | 1024 | glBegin(GL_LINE_STRIP); |
---|
[3836] | 1025 | TrackElement* tmpElem = this->firstTrackElem->findByID(i); |
---|
[3350] | 1026 | if (tmpElem->curve) |
---|
| 1027 | for(float f = 0.0; f < 1.0; f+=dt) |
---|
| 1028 | { |
---|
| 1029 | // printf("%f, %f, %f\n",trackManager->calcPos().x, trackManager->calcPos().y, trackManager->calcPos().z); |
---|
| 1030 | Vector tmpVector = tmpElem->curve->calcPos(f); |
---|
| 1031 | glVertex3f(tmpVector.x, tmpVector.y, tmpVector.z); |
---|
| 1032 | } |
---|
[3710] | 1033 | glEnd(); |
---|
[3350] | 1034 | } |
---|
| 1035 | } |
---|
| 1036 | |
---|
[3433] | 1037 | /** |
---|
| 1038 | \brief outputs debug information about the trackManager |
---|
| 1039 | \param level how much debug |
---|
| 1040 | */ |
---|
[3350] | 1041 | void TrackManager::debug(unsigned int level) const |
---|
| 1042 | { |
---|
[3522] | 1043 | PRINT(0)("=========================================\n"); |
---|
| 1044 | PRINT(0)("= CLASS TRACKMANAGER::debug information =\n"); |
---|
| 1045 | PRINT(0)("=========================================\n"); |
---|
| 1046 | // PRINT(0)("Status is: % |
---|
| 1047 | PRINT(0)(" Consists of %d elements\n", this->trackElemCount); |
---|
| 1048 | PRINT(0)(" localTime is: %f\n", this->localTime); |
---|
[3350] | 1049 | if (level >= 2) |
---|
| 1050 | { |
---|
| 1051 | for (int i = 1; i <= trackElemCount; i++) |
---|
| 1052 | { |
---|
[3836] | 1053 | TrackElement* tmpElem = this->firstTrackElem->findByID(i); |
---|
[3593] | 1054 | tmpElem->debug(); |
---|
[3350] | 1055 | } |
---|
| 1056 | } |
---|
[3522] | 1057 | PRINT(0)("-----------------------------------------\n"); |
---|
[3350] | 1058 | } |
---|