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