[3573] | 1 | |
---|
| 2 | |
---|
[3738] | 3 | |
---|
[3573] | 4 | /* |
---|
| 5 | orxonox - the future of 3D-vertical-scrollers |
---|
| 6 | |
---|
| 7 | Copyright (C) 2004 orx |
---|
| 8 | |
---|
| 9 | This program is free software; you can redistribute it and/or modify |
---|
| 10 | it under the terms of the GNU General Public License as published by |
---|
| 11 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 12 | any later version. |
---|
| 13 | |
---|
| 14 | ### File Specific: |
---|
| 15 | main-programmer: Patrick Boenzli |
---|
| 16 | co-programmer: ... |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | |
---|
| 20 | #include "simple_animation.h" |
---|
| 21 | #include "stdincl.h" |
---|
[3720] | 22 | #include "vector.h" |
---|
[3729] | 23 | #include "world_entity.h" |
---|
[3573] | 24 | |
---|
| 25 | using namespace std; |
---|
| 26 | |
---|
| 27 | |
---|
| 28 | |
---|
[3727] | 29 | SimpleAnimation* SimpleAnimation::singletonRef = 0; |
---|
[3573] | 30 | /** |
---|
[3727] | 31 | \brief gets the singleton instance |
---|
| 32 | \returns singleton instance |
---|
| 33 | */ |
---|
| 34 | SimpleAnimation* SimpleAnimation::getInstance() |
---|
| 35 | { |
---|
| 36 | if( singletonRef == NULL) |
---|
| 37 | singletonRef = new SimpleAnimation(); |
---|
| 38 | return singletonRef; |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | /** |
---|
[3573] | 42 | \brief standard constructor |
---|
| 43 | */ |
---|
[3727] | 44 | SimpleAnimation::SimpleAnimation () |
---|
[3573] | 45 | { |
---|
| 46 | this->setClassName ("SimpleAnimation"); |
---|
| 47 | this->frames = new tList<KeyFrame>(); |
---|
[3738] | 48 | this->animators = new tList<Animation>(); |
---|
[3573] | 49 | this->localTime = 0; |
---|
[3719] | 50 | this->bRunning = false; |
---|
| 51 | this->currentFrame = NULL; |
---|
| 52 | this->lastFrame = NULL; |
---|
[3720] | 53 | |
---|
| 54 | this->tmpVect = new Vector(); |
---|
[3729] | 55 | this->lastPosition = new Vector(); |
---|
[3733] | 56 | this->deltaT = 0.2; |
---|
[3573] | 57 | } |
---|
| 58 | |
---|
| 59 | |
---|
| 60 | /** |
---|
| 61 | \brief standard deconstructor |
---|
| 62 | |
---|
| 63 | */ |
---|
| 64 | SimpleAnimation::~SimpleAnimation () |
---|
| 65 | { |
---|
[3661] | 66 | tIterator<KeyFrame>* iterator = this->frames->getIterator(); |
---|
| 67 | KeyFrame* frame = iterator->nextElement(); |
---|
[3573] | 68 | while( frame != NULL) |
---|
| 69 | { |
---|
| 70 | delete frame; |
---|
[3661] | 71 | frame = iterator->nextElement(); |
---|
[3573] | 72 | } |
---|
[3661] | 73 | delete iterator; |
---|
[3573] | 74 | delete this->frames; |
---|
[3752] | 75 | singletonRef = NULL; |
---|
[3573] | 76 | } |
---|
| 77 | |
---|
| 78 | |
---|
[3727] | 79 | /** |
---|
| 80 | \brief this determines the start of an Animator Describtion |
---|
[3573] | 81 | |
---|
[3727] | 82 | this can then be followed by different commands like addKeyFrame(..) etc. and |
---|
| 83 | will be closed with AnimatiorEnd() |
---|
| 84 | */ |
---|
[3729] | 85 | void SimpleAnimation::animatorBegin() |
---|
[3727] | 86 | { |
---|
| 87 | this->bDescriptive = true; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | |
---|
[3573] | 91 | /** |
---|
[3727] | 92 | \brief this determines the end of an Animator Describtion |
---|
| 93 | |
---|
| 94 | this can then be followed by different commands like addKeyFrame(..) etc. and |
---|
| 95 | will be closed with AnimatiorEnd() |
---|
| 96 | */ |
---|
[3729] | 97 | void SimpleAnimation::animatorEnd() |
---|
[3727] | 98 | { |
---|
| 99 | this->workingObject = NULL; |
---|
[3743] | 100 | this->workingAnimator = NULL; |
---|
[3727] | 101 | this->bDescriptive = false; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | |
---|
[3739] | 105 | /* |
---|
| 106 | Vector* lastPosition; |
---|
| 107 | Vector* tmpVect; |
---|
| 108 | tList<KeyFrame>* frames; |
---|
| 109 | animationMode animMode; |
---|
| 110 | movementMode movMode; |
---|
| 111 | bool bRunning; |
---|
| 112 | float deltaT; |
---|
| 113 | */ |
---|
| 114 | |
---|
[3727] | 115 | /** |
---|
| 116 | \brief select an object to work on by using this function |
---|
| 117 | \param object wo work on |
---|
| 118 | */ |
---|
| 119 | void SimpleAnimation::selectObject(WorldEntity* entity) |
---|
| 120 | { |
---|
[3739] | 121 | Animation* anim = getAnimationFromWorldEntity(entity); |
---|
| 122 | if( anim == NULL) |
---|
| 123 | { |
---|
| 124 | anim = new Animation; |
---|
| 125 | anim->object = entity; |
---|
| 126 | anim->lastPosition = new Vector(); |
---|
| 127 | anim->tmpVect = new Vector(); |
---|
[3743] | 128 | anim->frames = new tList<KeyFrame>(); |
---|
[3752] | 129 | anim->animMode = LOOP; |
---|
[3755] | 130 | anim->bRunning = false; |
---|
[3739] | 131 | deltaT = 0.0; |
---|
| 132 | this->animators->add(anim); |
---|
| 133 | } |
---|
| 134 | this->workingAnimator = anim; |
---|
[3727] | 135 | } |
---|
| 136 | |
---|
| 137 | |
---|
| 138 | |
---|
| 139 | /** |
---|
[3573] | 140 | \brief adds a keyframe with properties |
---|
| 141 | \param the point of the object |
---|
[3729] | 142 | \param and the direction of it |
---|
[3573] | 143 | \param at this time |
---|
| 144 | */ |
---|
[3729] | 145 | void SimpleAnimation::addKeyFrame(Vector* point, Quaternion* direction, float time) |
---|
[3573] | 146 | { |
---|
[3743] | 147 | if( !this->bDescriptive || this->workingAnimator == NULL) |
---|
[3727] | 148 | { |
---|
| 149 | PRINTF(1)("SimpleAnimation: executing animation code outside a AnimationBegin()/AnimationEnd() - ignoring\n"); |
---|
| 150 | return; |
---|
| 151 | } |
---|
[3726] | 152 | KeyFrame* frame = new KeyFrame; |
---|
| 153 | frame->position = point; |
---|
[3729] | 154 | frame->direction = direction; |
---|
[3726] | 155 | frame->time = time; |
---|
| 156 | frame->mode = DEFAULT_ANIMATION_MODE; |
---|
[3743] | 157 | frame->object = this->workingAnimator->object; |
---|
| 158 | this->workingAnimator->frames->add(frame); |
---|
[3573] | 159 | } |
---|
| 160 | |
---|
| 161 | |
---|
| 162 | /** |
---|
| 163 | \brief adds a keyframe with properties |
---|
| 164 | \param the point of the object |
---|
[3729] | 165 | \param and the direction of it |
---|
[3573] | 166 | \param at this time |
---|
| 167 | \param function of the velocity of the movement |
---|
| 168 | */ |
---|
[3729] | 169 | void SimpleAnimation::addKeyFrame(Vector* point, Quaternion* direction, float time, movementMode mode) |
---|
[3573] | 170 | { |
---|
[3743] | 171 | if( !this->bDescriptive || this->workingAnimator == NULL) |
---|
[3727] | 172 | { |
---|
| 173 | PRINTF(1)("SimpleAnimation: executing animation code outside a AnimationBegin()/AnimationEnd() - ignoring\n"); |
---|
| 174 | return; |
---|
| 175 | } |
---|
[3726] | 176 | KeyFrame* frame = new KeyFrame; |
---|
| 177 | frame->position = point; |
---|
[3729] | 178 | frame->direction = direction; |
---|
[3726] | 179 | frame->time = time; |
---|
| 180 | frame->mode = mode; |
---|
[3743] | 181 | frame->object = this->workingAnimator->object; |
---|
| 182 | this->workingAnimator->frames->add(frame); |
---|
[3573] | 183 | } |
---|
| 184 | |
---|
| 185 | /** |
---|
| 186 | \brief adds a already defined keyframe |
---|
| 187 | \param the keyframe to add |
---|
| 188 | */ |
---|
| 189 | void SimpleAnimation::addKeyFrame(KeyFrame* frame) |
---|
| 190 | { |
---|
[3743] | 191 | if( !this->bDescriptive || this->workingAnimator == NULL) |
---|
[3727] | 192 | { |
---|
| 193 | PRINTF(1)("SimpleAnimation: executing animation code outside a AnimationBegin()/AnimationEnd() - ignoring\n"); |
---|
| 194 | return; |
---|
| 195 | } |
---|
[3743] | 196 | frame->object = this->workingAnimator->object; |
---|
| 197 | this->workingAnimator->frames->add(frame); |
---|
[3573] | 198 | } |
---|
| 199 | |
---|
| 200 | |
---|
[3752] | 201 | void SimpleAnimation::setAnimationMode(animationMode mode) |
---|
| 202 | { |
---|
| 203 | if( !this->bDescriptive || this->workingAnimator == NULL) |
---|
| 204 | { |
---|
| 205 | PRINTF(1)("SimpleAnimation: executing animation code outside a AnimationBegin()/AnimationEnd() - ignoring\n"); |
---|
| 206 | return; |
---|
| 207 | } |
---|
| 208 | this->workingAnimator->animMode = mode; |
---|
| 209 | } |
---|
| 210 | |
---|
[3573] | 211 | /** |
---|
| 212 | \brief clear the list of keyframes, deleting all keyframes included |
---|
| 213 | */ |
---|
| 214 | void SimpleAnimation::reset() |
---|
| 215 | { |
---|
[3743] | 216 | /* |
---|
[3661] | 217 | tIterator<KeyFrame>* iterator = this->frames->getIterator(); |
---|
| 218 | KeyFrame* frame = iterator->nextElement(); |
---|
[3573] | 219 | while( frame != NULL) |
---|
| 220 | { |
---|
| 221 | delete frame; |
---|
[3661] | 222 | frame = iterator->nextElement(); |
---|
[3573] | 223 | } |
---|
[3661] | 224 | delete iterator; |
---|
[3573] | 225 | delete this->frames; |
---|
| 226 | |
---|
| 227 | this->frames = new tList<KeyFrame>(); |
---|
| 228 | this->localTime = 0; |
---|
[3719] | 229 | this->bRunning = false; |
---|
[3573] | 230 | |
---|
[3719] | 231 | this->currentFrame = NULL; |
---|
| 232 | this->lastFrame = NULL; |
---|
[3743] | 233 | */ |
---|
[3573] | 234 | } |
---|
| 235 | |
---|
| 236 | /** |
---|
| 237 | \brief starts the animation, therefore listens to tick signals |
---|
| 238 | */ |
---|
| 239 | void SimpleAnimation::start() |
---|
[3719] | 240 | { |
---|
| 241 | if( this->bRunning) |
---|
| 242 | { |
---|
| 243 | PRINTF(2)("SimpleAnimatin is already running. You are trying to start it again.\n"); |
---|
| 244 | return; |
---|
| 245 | } |
---|
| 246 | |
---|
[3744] | 247 | if( this->workingAnimator == NULL) |
---|
| 248 | { |
---|
| 249 | PRINTF(1)("You have no target selected to start: either do this with start(target) or by prev selecting it\n"); |
---|
| 250 | return; |
---|
| 251 | } |
---|
| 252 | this->workingAnimator->localTime = 0.0; |
---|
| 253 | this->workingAnimator->bRunning = true; |
---|
| 254 | this->workingAnimator->currentFrame = this->workingAnimator->frames->firstElement(); |
---|
| 255 | this->workingAnimator->lastFrame = this->workingAnimator->frames->nextElement(this->workingAnimator->currentFrame); |
---|
[3743] | 256 | |
---|
| 257 | /* |
---|
| 258 | tIterator<Animation>* iterator = this->animators->getIterator(); |
---|
| 259 | Animation* anim = iterator->nextElement(); |
---|
| 260 | while( anim != NULL) |
---|
| 261 | { |
---|
| 262 | printf("SimpleAnimation::start() - initializing an animaion\n"); |
---|
| 263 | anim->currentFrame = anim->frames->firstElement(); |
---|
| 264 | anim->lastFrame = anim->frames->nextElement(anim->currentFrame); |
---|
| 265 | anim = iterator->nextElement(); |
---|
| 266 | } |
---|
| 267 | */ |
---|
[3719] | 268 | } |
---|
[3573] | 269 | |
---|
| 270 | |
---|
| 271 | /** |
---|
| 272 | \brief stops the animation, immune to tick signals |
---|
| 273 | */ |
---|
| 274 | void SimpleAnimation::stop() |
---|
[3719] | 275 | { |
---|
| 276 | this->bRunning = false; |
---|
| 277 | } |
---|
[3573] | 278 | |
---|
| 279 | /** |
---|
| 280 | \brief stops and then starts the animation from begining |
---|
| 281 | */ |
---|
| 282 | void SimpleAnimation::restart() |
---|
| 283 | { |
---|
| 284 | this->localTime = 0; |
---|
[3743] | 285 | //this->lastFrame = this->frames->firstElement(); |
---|
| 286 | //this->currentFrame = this->frames->nextElement(this->currentFrame); |
---|
[3719] | 287 | this->bRunning = true; |
---|
[3573] | 288 | } |
---|
| 289 | |
---|
| 290 | /** |
---|
| 291 | \brief pauses the animation until resumed |
---|
| 292 | */ |
---|
| 293 | void SimpleAnimation::pause() |
---|
| 294 | { |
---|
[3719] | 295 | this->bRunning = false; |
---|
[3573] | 296 | } |
---|
| 297 | |
---|
| 298 | /** |
---|
| 299 | \brief resumes a pause, if not paused, no effect |
---|
| 300 | */ |
---|
| 301 | void SimpleAnimation::resume() |
---|
| 302 | { |
---|
[3719] | 303 | this->bRunning = true; |
---|
[3573] | 304 | } |
---|
| 305 | |
---|
| 306 | |
---|
| 307 | /** |
---|
| 308 | \brief heart beat, next animation step |
---|
| 309 | */ |
---|
| 310 | void SimpleAnimation::tick(float time) |
---|
| 311 | { |
---|
[3738] | 312 | tIterator<Animation>* iterator = this->animators->getIterator(); |
---|
| 313 | Animation* anim = iterator->nextElement(); |
---|
| 314 | while( anim != NULL) |
---|
[3573] | 315 | { |
---|
[3738] | 316 | if( anim->bRunning) |
---|
[3743] | 317 | { |
---|
[3744] | 318 | anim->localTime += time; |
---|
[3738] | 319 | /* first get the current frame via time-stamps */ |
---|
[3744] | 320 | while( anim->localTime > anim->currentFrame->time) |
---|
[3738] | 321 | { |
---|
[3755] | 322 | PRINTF(4)("SimpleAnimation::tick(...) - changing Frame\n"); |
---|
[3738] | 323 | |
---|
[3744] | 324 | anim->localTime -= anim->currentFrame->time; |
---|
[3738] | 325 | //this->currentFrame->object->setRelCoor(*this->currentFrame->position); |
---|
| 326 | *anim->lastPosition = *anim->currentFrame->position; |
---|
| 327 | |
---|
| 328 | anim->lastFrame = anim->currentFrame; |
---|
| 329 | anim->currentFrame = anim->frames->nextElement(anim->currentFrame); |
---|
[3752] | 330 | if( anim->currentFrame == anim->frames->firstElement() && anim->animMode == SINGLE) |
---|
| 331 | { |
---|
| 332 | anim->bRunning = false; |
---|
| 333 | return; |
---|
| 334 | } |
---|
[3738] | 335 | anim->movMode = anim->currentFrame->mode; |
---|
| 336 | if( anim->movMode == NEG_EXP) |
---|
| 337 | { |
---|
| 338 | *anim->tmpVect = *anim->currentFrame->position - *anim->lastFrame->position; |
---|
| 339 | anim->deltaT = 1/anim->currentFrame->time * logf(1.0 + 600.0/anim->tmpVect->len()); |
---|
| 340 | } |
---|
| 341 | } |
---|
| 342 | |
---|
| 343 | /* now animate it */ |
---|
| 344 | switch( anim->movMode) |
---|
| 345 | { |
---|
| 346 | case LINEAR: |
---|
| 347 | *anim->tmpVect = *anim->currentFrame->position - *anim->lastFrame->position; |
---|
[3744] | 348 | *anim->tmpVect = *anim->tmpVect * anim->localTime / anim->currentFrame->time; |
---|
[3738] | 349 | anim->currentFrame->object->setRelCoor(*anim->lastFrame->position + *anim->tmpVect); |
---|
| 350 | *anim->lastPosition = *anim->tmpVect; |
---|
| 351 | break; |
---|
| 352 | case EXP: |
---|
| 353 | |
---|
| 354 | break; |
---|
| 355 | case NEG_EXP: |
---|
| 356 | *anim->tmpVect = *anim->currentFrame->position - *anim->lastFrame->position; |
---|
[3744] | 357 | *anim->tmpVect = *anim->tmpVect * (1 - exp(- anim->localTime * anim->deltaT)); |
---|
[3738] | 358 | anim->currentFrame->object->setRelCoor(*anim->lastFrame->position + *anim->tmpVect); |
---|
| 359 | *anim->lastPosition = *anim->tmpVect; |
---|
| 360 | break; |
---|
| 361 | case SIN: |
---|
| 362 | *anim->tmpVect = *anim->currentFrame->position - *anim->lastFrame->position; |
---|
[3744] | 363 | *anim->tmpVect = *anim->tmpVect * 0.5*(1 - cos(M_PI * anim->localTime / anim->currentFrame->time)); |
---|
[3738] | 364 | anim->currentFrame->object->setRelCoor(*anim->lastFrame->position + *anim->tmpVect); |
---|
| 365 | *anim->lastPosition = *anim->tmpVect; |
---|
| 366 | break; |
---|
| 367 | case COS: |
---|
| 368 | |
---|
| 369 | break; |
---|
| 370 | case QUADRATIC: |
---|
| 371 | *anim->tmpVect = *anim->currentFrame->position - *anim->lastFrame->position; |
---|
[3744] | 372 | *anim->tmpVect = *anim->tmpVect * 1/3 * ldexpf(anim->localTime, 3); |
---|
[3738] | 373 | break; |
---|
| 374 | default: |
---|
| 375 | break; |
---|
| 376 | } |
---|
[3733] | 377 | } |
---|
[3738] | 378 | anim = iterator->nextElement(); |
---|
[3719] | 379 | } |
---|
[3738] | 380 | delete anim; |
---|
[3573] | 381 | } |
---|
[3739] | 382 | |
---|
| 383 | |
---|
| 384 | |
---|
| 385 | Animation* SimpleAnimation::getAnimationFromWorldEntity(WorldEntity* entity) |
---|
| 386 | { |
---|
| 387 | tIterator<Animation>* iterator = this->animators->getIterator(); |
---|
| 388 | Animation* anim = iterator->nextElement(); |
---|
| 389 | while( anim != NULL) |
---|
| 390 | { |
---|
| 391 | if( anim->object == entity) |
---|
| 392 | return anim; |
---|
| 393 | anim = iterator->nextElement(); |
---|
| 394 | } |
---|
| 395 | delete iterator; |
---|
| 396 | return NULL; |
---|
| 397 | } |
---|