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