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