AnimationClip.cpp 16 KB

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