AnimationClip.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. #include "Base.h"
  2. #include "AnimationClip.h"
  3. #include "Animation.h"
  4. #include "AnimationTarget.h"
  5. #include "Game.h"
  6. #include "Quaternion.h"
  7. namespace gameplay
  8. {
  9. AnimationClip::AnimationClip(const char* id, Animation* animation, unsigned long startTime, unsigned long endTime)
  10. : _id(id), _animation(animation), _startTime(startTime), _endTime(endTime), _duration(_endTime - _startTime),
  11. _stateBits(0x00), _repeatCount(1.0f), _activeDuration(_duration * _repeatCount), _speed(1.0f), _timeStarted(0),
  12. _elapsedTime(0), _crossFadeToClip(NULL), _crossFadeOutElapsed(0), _crossFadeOutDuration(0), _blendWeight(1.0f),
  13. _beginListeners(NULL), _endListeners(NULL), _listeners(NULL), _listenerItr(NULL)
  14. {
  15. assert(0 <= startTime && startTime <= animation->_duration && 0 <= endTime && endTime <= animation->_duration);
  16. unsigned int channelCount = _animation->_channels.size();
  17. for (unsigned int i = 0; i < channelCount; i++)
  18. {
  19. _values.push_back(new AnimationValue(_animation->_channels[i]->_curve->getComponentCount()));
  20. }
  21. }
  22. AnimationClip::~AnimationClip()
  23. {
  24. std::vector<AnimationValue*>::iterator valueIter = _values.begin();
  25. while (valueIter != _values.end())
  26. {
  27. SAFE_DELETE(*valueIter);
  28. valueIter++;
  29. }
  30. _values.clear();
  31. SAFE_RELEASE(_crossFadeToClip);
  32. SAFE_DELETE(_beginListeners);
  33. SAFE_DELETE(_endListeners);
  34. SAFE_DELETE(_listeners);
  35. SAFE_DELETE(_listenerItr);
  36. }
  37. const char* AnimationClip::getID() const
  38. {
  39. return _id.c_str();
  40. }
  41. Animation* AnimationClip::getAnimation() const
  42. {
  43. return _animation;
  44. }
  45. unsigned long AnimationClip::getStartTime() const
  46. {
  47. return _startTime;
  48. }
  49. unsigned long AnimationClip::getEndTime() const
  50. {
  51. return _endTime;
  52. }
  53. unsigned long AnimationClip::getElaspedTime() const
  54. {
  55. return _elapsedTime;
  56. }
  57. void AnimationClip::setRepeatCount(float repeatCount)
  58. {
  59. assert(repeatCount == REPEAT_INDEFINITE || repeatCount > 0.0f);
  60. _repeatCount = repeatCount;
  61. if (repeatCount == REPEAT_INDEFINITE)
  62. {
  63. _activeDuration = _duration;
  64. }
  65. else
  66. {
  67. _activeDuration = _repeatCount * _duration;
  68. }
  69. }
  70. float AnimationClip::getRepeatCount() const
  71. {
  72. return _repeatCount;
  73. }
  74. void AnimationClip::setActiveDuration(unsigned long duration)
  75. {
  76. if (duration == REPEAT_INDEFINITE)
  77. {
  78. _repeatCount = REPEAT_INDEFINITE;
  79. _activeDuration = _duration;
  80. }
  81. else
  82. {
  83. _activeDuration = _duration;
  84. _repeatCount = (float) _activeDuration / (float) _duration;
  85. }
  86. }
  87. unsigned long AnimationClip::getActiveDuration() const
  88. {
  89. if (_repeatCount == REPEAT_INDEFINITE)
  90. return REPEAT_INDEFINITE;
  91. return _activeDuration;
  92. }
  93. void AnimationClip::setSpeed(float speed)
  94. {
  95. _speed = speed;
  96. }
  97. float AnimationClip::getSpeed() const
  98. {
  99. return _speed;
  100. }
  101. void AnimationClip::setBlendWeight(float blendWeight)
  102. {
  103. _blendWeight = blendWeight;
  104. }
  105. float AnimationClip::getBlendWeight() const
  106. {
  107. return _blendWeight;
  108. }
  109. bool AnimationClip::isPlaying() const
  110. {
  111. return isClipStateBitSet(CLIP_IS_PLAYING_BIT);
  112. }
  113. void AnimationClip::play()
  114. {
  115. if (isClipStateBitSet(CLIP_IS_PLAYING_BIT))
  116. {
  117. onEnd();
  118. _animation->_controller->unschedule(this);
  119. }
  120. setClipStateBit(CLIP_IS_PLAYING_BIT);
  121. _animation->_controller->schedule(this);
  122. _timeStarted = Game::getGameTime();
  123. }
  124. void AnimationClip::stop()
  125. {
  126. if (isClipStateBitSet(CLIP_IS_PLAYING_BIT))
  127. {
  128. onEnd();
  129. _animation->_controller->unschedule(this);
  130. }
  131. }
  132. void AnimationClip::crossFade(AnimationClip* clip, unsigned long duration)
  133. {
  134. assert(clip);
  135. // Check if the given clip is fading into this clip.
  136. // We should reset the clip from fading out, and this one from fading in
  137. // in order to start the crossfade back the other way.
  138. if (clip->isClipStateBitSet(CLIP_IS_FADING_OUT_BIT) && clip->_crossFadeToClip == this)
  139. {
  140. clip->resetClipStateBit(CLIP_IS_FADING_OUT_BIT);
  141. clip->_crossFadeToClip->resetClipStateBit(CLIP_IS_FADING_IN_BIT);
  142. SAFE_RELEASE(clip->_crossFadeToClip);
  143. }
  144. // If I already have a clip I'm fading to and it's not the same as the given clip release it.
  145. // Assign the new clip and increase it's ref count.
  146. if (_crossFadeToClip)
  147. {
  148. SAFE_RELEASE(_crossFadeToClip);
  149. }
  150. // Set and initialize the crossfade clip
  151. _crossFadeToClip = clip;
  152. _crossFadeToClip->addRef();
  153. _crossFadeToClip->setClipStateBit(CLIP_IS_FADING_IN_BIT);
  154. _crossFadeToClip->_blendWeight = 0.0f;
  155. // Set and intiliaze this clip to fade out
  156. setClipStateBit(CLIP_IS_FADING_OUT_STARTED_BIT);
  157. setClipStateBit(CLIP_IS_FADING_OUT_BIT);
  158. _crossFadeOutElapsed = 0L;
  159. _crossFadeOutDuration = duration;
  160. // If this clip is currently not playing, we should start playing it.
  161. if (!isClipStateBitSet(CLIP_IS_PLAYING_BIT))
  162. play();
  163. // Start playing the cross fade clip.
  164. _crossFadeToClip->play();
  165. }
  166. void AnimationClip::addListener(AnimationClip::Listener* listener, unsigned long eventTime)
  167. {
  168. assert(listener);
  169. assert(eventTime < _duration);
  170. listener->_listenerTime = eventTime;
  171. if (!_listeners)
  172. {
  173. _listeners = new std::list<Listener*>;
  174. _listeners->push_front(listener);
  175. _listenerItr = new std::list<Listener*>::iterator;
  176. if (isClipStateBitSet(CLIP_IS_PLAYING_BIT))
  177. *_listenerItr = _listeners->begin();
  178. }
  179. else
  180. {
  181. for (std::list<Listener*>::iterator itr = _listeners->begin(); itr != _listeners->begin(); itr++)
  182. {
  183. if (eventTime < (*itr)->_listenerTime)
  184. {
  185. itr = _listeners->insert(itr, listener);
  186. // If playing, update the iterator if we need to.
  187. // otherwise, it will just be set the next time the clip gets played.
  188. if (isClipStateBitSet(CLIP_IS_PLAYING_BIT))
  189. {
  190. unsigned long currentTime = _elapsedTime % _duration;
  191. if ((_speed >= 0.0f && currentTime < eventTime && (*_listenerItr == _listeners->end() || eventTime < (**_listenerItr)->_listenerTime)) ||
  192. (_speed <= 0 && currentTime > eventTime && (*_listenerItr == _listeners->begin() || eventTime > (**_listenerItr)->_listenerTime)))
  193. *_listenerItr = itr;
  194. }
  195. return;
  196. }
  197. }
  198. }
  199. }
  200. void AnimationClip::addBeginListener(AnimationClip::Listener* listener)
  201. {
  202. if (!_beginListeners)
  203. _beginListeners = new std::vector<Listener*>;
  204. _beginListeners->push_back(listener);
  205. }
  206. void AnimationClip::addEndListener(AnimationClip::Listener* listener)
  207. {
  208. if (!_endListeners)
  209. _endListeners = new std::vector<Listener*>;
  210. _endListeners->push_back(listener);
  211. }
  212. bool AnimationClip::update(unsigned long elapsedTime, std::list<AnimationTarget*>* activeTargets)
  213. {
  214. if (!isClipStateBitSet(CLIP_IS_STARTED_BIT))
  215. onBegin();
  216. else
  217. {
  218. _elapsedTime += elapsedTime * _speed;
  219. if (_repeatCount == REPEAT_INDEFINITE && _elapsedTime <= 0)
  220. _elapsedTime = _activeDuration + _elapsedTime;
  221. }
  222. unsigned long currentTime = 0L;
  223. // Check to see if clip is complete.
  224. if (_repeatCount != REPEAT_INDEFINITE && ((_speed >= 0.0f && _elapsedTime >= (long) _activeDuration) || (_speed <= 0.0f && _elapsedTime <= 0L)))
  225. {
  226. resetClipStateBit(CLIP_IS_STARTED_BIT);
  227. if (_speed >= 0.0f)
  228. {
  229. // If _duration == 0, we have a "pose". Just set currentTime to 0.
  230. if (_duration == 0)
  231. {
  232. currentTime = 0L;
  233. }
  234. else
  235. {
  236. currentTime = _activeDuration % _duration; // Get's the fractional part of the final repeat.
  237. if (currentTime == 0L)
  238. currentTime = _duration;
  239. }
  240. }
  241. else
  242. {
  243. currentTime = 0L; // If we are negative speed, the end value should be 0.
  244. }
  245. }
  246. else
  247. {
  248. // If _duration == 0, we have a "pose". Just set currentTime to 0.
  249. if (_duration == 0)
  250. currentTime = 0L;
  251. else // Gets portion/fraction of the repeat.
  252. currentTime = _elapsedTime % _duration;
  253. }
  254. // Notify any listeners of Animation events.
  255. if (_listeners)
  256. {
  257. if (_speed >= 0.0f)
  258. {
  259. while (*_listenerItr != _listeners->end() && currentTime >= (**_listenerItr)->_listenerTime)
  260. {
  261. (**_listenerItr)->animationEvent(this, Listener::DEFAULT);
  262. ++*_listenerItr;
  263. }
  264. }
  265. else
  266. {
  267. while (*_listenerItr != _listeners->begin() && currentTime <= (**_listenerItr)->_listenerTime)
  268. {
  269. (**_listenerItr)->animationEvent(this, Listener::DEFAULT);
  270. --*_listenerItr;
  271. }
  272. }
  273. }
  274. // Add back in start time, and divide by the total animation's duration to get the actual percentage complete
  275. float percentComplete = (float)(_startTime + currentTime) / (float) _animation->_duration;
  276. if (isClipStateBitSet(CLIP_IS_FADING_OUT_BIT))
  277. {
  278. if (isClipStateBitSet(CLIP_IS_FADING_OUT_STARTED_BIT)) // Calculate elapsed time since the fade out begin.
  279. {
  280. _crossFadeOutElapsed = (Game::getGameTime() - _crossFadeToClip->_timeStarted) * abs(_speed);
  281. resetClipStateBit(CLIP_IS_FADING_OUT_STARTED_BIT);
  282. }
  283. else
  284. {
  285. // continue tracking elapsed time.
  286. _crossFadeOutElapsed += elapsedTime * abs(_speed);
  287. }
  288. if (_crossFadeOutElapsed < _crossFadeOutDuration)
  289. {
  290. // Calculate this clip's blend weight.
  291. float tempBlendWeight = (float) (_crossFadeOutDuration - _crossFadeOutElapsed) / (float) _crossFadeOutDuration;
  292. // If this clip is fading in, adjust the crossfade clip's weight to be a percentage of your current blend weight
  293. if (isClipStateBitSet(CLIP_IS_FADING_IN_BIT))
  294. {
  295. _crossFadeToClip->_blendWeight = (1.0f - tempBlendWeight) * _blendWeight;
  296. _blendWeight -= _crossFadeToClip->_blendWeight;
  297. }
  298. else
  299. {
  300. // Just set the blend weight.
  301. _crossFadeToClip->_blendWeight = (1.0f - tempBlendWeight);
  302. _blendWeight = tempBlendWeight;
  303. }
  304. }
  305. else
  306. { // Fade is done.
  307. _crossFadeToClip->_blendWeight = 1.0f;
  308. _blendWeight = 0.0f;
  309. resetClipStateBit(CLIP_IS_STARTED_BIT);
  310. resetClipStateBit(CLIP_IS_FADING_OUT_BIT);
  311. _crossFadeToClip->resetClipStateBit(CLIP_IS_FADING_IN_BIT);
  312. SAFE_RELEASE(_crossFadeToClip);
  313. }
  314. }
  315. // Evaluate this clip.
  316. Animation::Channel* channel = NULL;
  317. AnimationValue* value = NULL;
  318. AnimationTarget* target = NULL;
  319. unsigned int channelCount = _animation->_channels.size();
  320. for (unsigned int i = 0; i < channelCount; i++)
  321. {
  322. channel = _animation->_channels[i];
  323. target = channel->_target;
  324. value = _values[i];
  325. // If the target's _animationPropertyBitFlag is clear, we can assume that this is the first
  326. // animation channel to act on the target and we can add the target to the list of
  327. // active targets stored by the AnimationController.
  328. if (target->_animationPropertyBitFlag == 0x00)
  329. activeTargets->push_front(target);
  330. // Evaluate the point on Curve
  331. channel->_curve->evaluate(percentComplete, value->_value);
  332. // Set the animation value on the target property.
  333. target->setAnimationPropertyValue(channel->_propertyId, value, _blendWeight);
  334. }
  335. // When ended. Probably should move to it's own method so we can call it when the clip is ended early.
  336. if (!isClipStateBitSet(CLIP_IS_STARTED_BIT))
  337. {
  338. onEnd();
  339. // Notify end listeners if any.
  340. if (_endListeners)
  341. {
  342. std::vector<Listener*>::iterator listener = _endListeners->begin();
  343. while (listener != _endListeners->end())
  344. {
  345. (*listener)->animationEvent(this, Listener::END);
  346. listener++;
  347. }
  348. }
  349. return true;
  350. }
  351. return false;
  352. }
  353. void AnimationClip::onBegin()
  354. {
  355. // Initialize animation to play.
  356. setClipStateBit(CLIP_IS_STARTED_BIT);
  357. if (_speed >= 0)
  358. {
  359. _elapsedTime = (Game::getGameTime() - _timeStarted) * _speed;
  360. if (_listeners)
  361. *_listenerItr = _listeners->begin();
  362. }
  363. else
  364. {
  365. _elapsedTime = _activeDuration + (Game::getGameTime() - _timeStarted) * _speed;
  366. if (_listeners)
  367. *_listenerItr = _listeners->end();
  368. }
  369. // Notify begin listeners.. if any.
  370. if (_beginListeners)
  371. {
  372. std::vector<Listener*>::iterator listener = _beginListeners->begin();
  373. while (listener != _beginListeners->end())
  374. {
  375. (*listener)->animationEvent(this, Listener::BEGIN);
  376. listener++;
  377. }
  378. }
  379. }
  380. void AnimationClip::onEnd()
  381. {
  382. _blendWeight = 1.0f;
  383. _timeStarted = 0;
  384. resetClipStateBit(CLIP_ALL_BITS);
  385. }
  386. bool AnimationClip::isClipStateBitSet(char bit) const
  387. {
  388. return (_stateBits & bit) == bit;
  389. }
  390. void AnimationClip::setClipStateBit(char bit)
  391. {
  392. _stateBits |= bit;
  393. }
  394. void AnimationClip::resetClipStateBit(char bit)
  395. {
  396. _stateBits &= ~bit;
  397. }
  398. }