AnimationClip.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. _animation->_controller->unschedule(this);
  118. resetClipStateBit(CLIP_IS_STARTED_BIT);
  119. onEnd();
  120. }
  121. else
  122. setClipStateBit(CLIP_IS_PLAYING_BIT);
  123. _animation->_controller->schedule(this);
  124. _timeStarted = Game::getGameTime();
  125. }
  126. void AnimationClip::stop()
  127. {
  128. if (isClipStateBitSet(CLIP_IS_PLAYING_BIT))
  129. {
  130. _animation->_controller->unschedule(this);
  131. resetClipStateBit(CLIP_IS_PLAYING_BIT | CLIP_IS_STARTED_BIT);
  132. onEnd();
  133. }
  134. }
  135. void AnimationClip::crossFade(AnimationClip* clip, unsigned long duration)
  136. {
  137. assert(clip);
  138. // If I already have a clip I'm fading to and it's not the same as the given clip release it.
  139. // Assign the new clip and increase it's ref count.
  140. if (_crossFadeToClip)
  141. {
  142. _crossFadeToClip->stop();
  143. _crossFadeToClip->resetClipStateBit(CLIP_IS_FADING_IN_BIT);
  144. SAFE_RELEASE(_crossFadeToClip);
  145. }
  146. _crossFadeToClip = clip;
  147. _crossFadeToClip->addRef();
  148. // Set the crossfade clip to fading in, and initialize it's blend weight to zero.
  149. _crossFadeToClip->setClipStateBit(CLIP_IS_FADING_IN_BIT);
  150. _crossFadeToClip->_blendWeight = 0.0f;
  151. // Set this clip to fade out, set the fade duration and reset the fade elapsed time.
  152. setClipStateBit(CLIP_IS_FADING_OUT_STARTED_BIT);
  153. setClipStateBit(CLIP_IS_FADING_OUT_BIT);
  154. _crossFadeOutElapsed = 0;
  155. _crossFadeOutDuration = duration;
  156. // If this clip is currently not playing, we should start playing it.
  157. if (!isClipStateBitSet(CLIP_IS_PLAYING_BIT))
  158. play();
  159. // Start playing the cross fade clip.
  160. _crossFadeToClip->play();
  161. }
  162. void AnimationClip::addListener(AnimationClip::Listener* listener, unsigned long eventTime)
  163. {
  164. assert(listener);
  165. assert(eventTime < _duration);
  166. listener->_listenerTime = eventTime;
  167. if (!_listeners)
  168. {
  169. _listeners = new std::list<Listener*>;
  170. _listeners->push_front(listener);
  171. _listenerItr = new std::list<Listener*>::iterator;
  172. if (isClipStateBitSet(CLIP_IS_PLAYING_BIT))
  173. *_listenerItr = _listeners->begin();
  174. }
  175. else
  176. {
  177. for (std::list<Listener*>::iterator itr = _listeners->begin(); itr != _listeners->begin(); itr++)
  178. {
  179. if (eventTime < (*itr)->_listenerTime)
  180. {
  181. itr = _listeners->insert(itr, listener);
  182. // If playing, update the iterator if we need to.
  183. // otherwise, it will just be set the next time the clip gets played.
  184. if (isClipStateBitSet(CLIP_IS_PLAYING_BIT))
  185. {
  186. unsigned long currentTime = _elapsedTime % _duration;
  187. if ((_speed >= 0.0f && currentTime < eventTime && (*_listenerItr == _listeners->end() || eventTime < (**_listenerItr)->_listenerTime)) ||
  188. (_speed <= 0 && currentTime > eventTime && (*_listenerItr == _listeners->begin() || eventTime > (**_listenerItr)->_listenerTime)))
  189. *_listenerItr = itr;
  190. }
  191. return;
  192. }
  193. }
  194. }
  195. }
  196. void AnimationClip::addBeginListener(AnimationClip::Listener* listener)
  197. {
  198. if (!_beginListeners)
  199. _beginListeners = new std::vector<Listener*>;
  200. _beginListeners->push_back(listener);
  201. }
  202. void AnimationClip::addEndListener(AnimationClip::Listener* listener)
  203. {
  204. if (!_endListeners)
  205. _endListeners = new std::vector<Listener*>;
  206. _endListeners->push_back(listener);
  207. }
  208. bool AnimationClip::update(unsigned long elapsedTime, std::list<AnimationTarget*>* activeTargets)
  209. {
  210. if (!isClipStateBitSet(CLIP_IS_STARTED_BIT))
  211. onBegin();
  212. else
  213. _elapsedTime += elapsedTime * _speed;
  214. unsigned long currentTime = 0L;
  215. // Check to see if clip is complete.
  216. if (_repeatCount != REPEAT_INDEFINITE && ((_speed >= 0.0f && _elapsedTime >= (long) _activeDuration) || (_speed <= 0.0f && _elapsedTime <= 0L)))
  217. {
  218. resetClipStateBit(CLIP_IS_STARTED_BIT);
  219. if (_speed >= 0.0f)
  220. {
  221. currentTime = _activeDuration % _duration; // Get's the fractional part of the final repeat.
  222. if (currentTime == 0L)
  223. currentTime = _duration;
  224. }
  225. else
  226. {
  227. currentTime = 0L; // If we are negative speed, the end value should be 0.
  228. }
  229. }
  230. else
  231. {
  232. // Gets portion/fraction of the repeat.
  233. currentTime = _elapsedTime % _duration;
  234. }
  235. // Notify any listeners of Animation events.
  236. if (_listeners)
  237. {
  238. if (_speed >= 0.0f)
  239. {
  240. while (*_listenerItr != _listeners->end() && currentTime >= (**_listenerItr)->_listenerTime)
  241. {
  242. (**_listenerItr)->animationEvent(this, Listener::DEFAULT);
  243. ++*_listenerItr;
  244. }
  245. }
  246. else
  247. {
  248. while (*_listenerItr != _listeners->begin() && currentTime <= (**_listenerItr)->_listenerTime)
  249. {
  250. (**_listenerItr)->animationEvent(this, Listener::DEFAULT);
  251. --*_listenerItr;
  252. }
  253. }
  254. }
  255. // Add back in start time, and divide by the total animation's duration to get the actual percentage complete
  256. float percentComplete = (float)(_startTime + currentTime) / (float) _animation->_duration;
  257. if (isClipStateBitSet(CLIP_IS_FADING_OUT_BIT))
  258. {
  259. if (isClipStateBitSet(CLIP_IS_FADING_OUT_STARTED_BIT)) // Calculate elapsed time since the fade out begin.
  260. {
  261. _crossFadeOutElapsed = (Game::getGameTime() - _crossFadeToClip->_timeStarted) * abs(_speed);
  262. resetClipStateBit(CLIP_IS_FADING_OUT_STARTED_BIT);
  263. }
  264. else
  265. {
  266. // continue tracking elapsed time.
  267. _crossFadeOutElapsed += elapsedTime * abs(_speed);
  268. }
  269. if (_crossFadeOutElapsed < _crossFadeOutDuration)
  270. {
  271. // Calculate this clip's blend weight.
  272. float tempBlendWeight = (float) (_crossFadeOutDuration - _crossFadeOutElapsed) / (float) _crossFadeOutDuration;
  273. // If this clip is fading in, adjust the crossfade clip's weight to be a percentage of your current blend weight
  274. if (isClipStateBitSet(CLIP_IS_FADING_IN_BIT))
  275. {
  276. _crossFadeToClip->_blendWeight = (1.0f - tempBlendWeight) * _blendWeight;
  277. _blendWeight -= _crossFadeToClip->_blendWeight;
  278. }
  279. else
  280. {
  281. // Just set the blend weight.
  282. _crossFadeToClip->_blendWeight = (1.0f - tempBlendWeight);
  283. _blendWeight = tempBlendWeight;
  284. }
  285. }
  286. else
  287. { // Fade is done.
  288. // Set blendweights
  289. _crossFadeToClip->_blendWeight = 1.0f;
  290. // Adjust the crossfade clip's blend weight if this clip is also fading in.
  291. if (isClipStateBitSet(CLIP_IS_FADING_IN_BIT))
  292. _crossFadeToClip->_blendWeight *= _blendWeight;
  293. _blendWeight = 0.0f;
  294. // reset the states of our clips to turn off the cross fade.
  295. _crossFadeToClip->resetClipStateBit(CLIP_IS_FADING_IN_BIT);
  296. resetClipStateBit(CLIP_IS_FADING_OUT_BIT | CLIP_IS_STARTED_BIT);
  297. // Release the crossfade clip, mark ourselves as done and set our blend weight to 0.
  298. SAFE_RELEASE(_crossFadeToClip);
  299. }
  300. }
  301. // Evaluate this clip.
  302. Animation::Channel* channel = NULL;
  303. AnimationValue* value = NULL;
  304. AnimationTarget* target = NULL;
  305. unsigned int channelCount = _animation->_channels.size();
  306. for (unsigned int i = 0; i < channelCount; i++)
  307. {
  308. channel = _animation->_channels[i];
  309. target = channel->_target;
  310. value = _values[i];
  311. // If the target's _animationPropertyBitFlag is clear, we can assume that this is the first
  312. // animation channel to act on the target and we can add the target to the list of
  313. // active targets stored by the AnimationController.
  314. if (target->_animationPropertyBitFlag == 0x00)
  315. activeTargets->push_front(target);
  316. // Evaluate the point on Curve
  317. channel->_curve->evaluate(percentComplete, value->_value);
  318. // Set the animation value on the target property.
  319. target->setAnimationPropertyValue(channel->_propertyId, value, _blendWeight);
  320. }
  321. // When ended. Probably should move to it's own method so we can call it when the clip is ended early.
  322. if (!isClipStateBitSet(CLIP_IS_STARTED_BIT))
  323. {
  324. onEnd();
  325. // Notify end listeners if any.
  326. if (_endListeners)
  327. {
  328. std::vector<Listener*>::iterator listener = _endListeners->begin();
  329. while (listener != _endListeners->end())
  330. {
  331. (*listener)->animationEvent(this, Listener::END);
  332. listener++;
  333. }
  334. }
  335. return true;
  336. }
  337. return false;
  338. }
  339. void AnimationClip::onBegin()
  340. {
  341. // Initialize animation to play.
  342. setClipStateBit(CLIP_IS_STARTED_BIT);
  343. if (_speed >= 0)
  344. {
  345. _elapsedTime = 0;
  346. if (_listeners)
  347. *_listenerItr = _listeners->begin();
  348. }
  349. else
  350. {
  351. _elapsedTime = _activeDuration;
  352. if (_listeners)
  353. *_listenerItr = _listeners->end();
  354. }
  355. _elapsedTime += (Game::getGameTime() - _timeStarted) * _speed;
  356. // Notify begin listeners.. if any.
  357. if (_beginListeners)
  358. {
  359. std::vector<Listener*>::iterator listener = _beginListeners->begin();
  360. while (listener != _beginListeners->end())
  361. {
  362. (*listener)->animationEvent(this, Listener::BEGIN);
  363. listener++;
  364. }
  365. }
  366. }
  367. void AnimationClip::onEnd()
  368. {
  369. _blendWeight = 1.0f;
  370. _timeStarted = 0;
  371. resetClipStateBit(CLIP_IS_PLAYING_BIT);
  372. }
  373. bool AnimationClip::isClipStateBitSet(char bit) const
  374. {
  375. return (_stateBits & bit) == bit;
  376. }
  377. void AnimationClip::setClipStateBit(char bit)
  378. {
  379. _stateBits |= bit;
  380. }
  381. void AnimationClip::resetClipStateBit(char bit)
  382. {
  383. _stateBits &= ~bit;
  384. }
  385. }