VActorAnimationController.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. //-----------------------------------------------------------------------------
  2. // Verve
  3. // Copyright (C) 2014 - Violent Tulip
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to
  7. // deal in the Software without restriction, including without limitation the
  8. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. // sell copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. // IN THE SOFTWARE.
  22. //-----------------------------------------------------------------------------
  23. #include "VActorAnimationController.h"
  24. #include "VActor.h"
  25. #include "VActorData.h"
  26. #include "VActorAnimationStates.h"
  27. //-----------------------------------------------------------------------------
  28. VActorAnimationController::VActorAnimationController( void ) :
  29. mObject( NULL )
  30. {
  31. // Void.
  32. }
  33. VActorAnimationController::~VActorAnimationController( void )
  34. {
  35. // Clear Table.
  36. mAnimationTable.clear();
  37. }
  38. //-----------------------------------------------------------------------------
  39. //
  40. // Initialisation Methods.
  41. //
  42. //-----------------------------------------------------------------------------
  43. //-----------------------------------------------------------------------------
  44. //
  45. // VActorAnimationController::initAnimationTable();
  46. //
  47. // ...
  48. //
  49. //-----------------------------------------------------------------------------
  50. bool VActorAnimationController::initAnimationTable( void )
  51. {
  52. // Valid Object?
  53. if ( !isValidObject() )
  54. {
  55. // No, Quit Now.
  56. return false;
  57. }
  58. // Clear the Table.
  59. mAnimationTable.clear();
  60. // Fetch Sequence List.
  61. VActorData::tAnimationSequenceVector *sequenceList = getObject()->getDataBlock()->getAnimationList();
  62. // Initialise the Animation States.
  63. for ( VActorData::tAnimationSequenceVector::iterator itr = sequenceList->begin();
  64. itr != sequenceList->end();
  65. itr++ )
  66. {
  67. // Fetch Sequence Definition.
  68. const VActorData::sAnimationSequence &animSequence = ( *itr );
  69. // Valid State?
  70. if ( animSequence.State )
  71. {
  72. // Register Animation.
  73. mAnimationTable.registerState( animSequence.State, animSequence.Priority );
  74. }
  75. }
  76. // Sort the Table.
  77. mAnimationTable.sort();
  78. // Valid.
  79. return true;
  80. }
  81. //-----------------------------------------------------------------------------
  82. //
  83. // VActorAnimationController::initAnimation( pThread, pIndex, pPosition, pTimeScale );
  84. //
  85. // ...
  86. //
  87. //-----------------------------------------------------------------------------
  88. bool VActorAnimationController::initAnimation( sAnimationRef &pAnimation, const U32 &pIndex, const F32 &pPosition, const F32 &pTimeScale )
  89. {
  90. // Valid Object & Sequence?
  91. if ( !isValidObject() || !isAnimationSequence( pIndex ) )
  92. {
  93. // No, Quit Now.
  94. return false;
  95. }
  96. // Store as Current Animation.
  97. pAnimation.Index = pIndex;
  98. // Initialise Thread.
  99. return initAnimationThread( pAnimation.Thread, pAnimation.Index, pPosition, pTimeScale );
  100. }
  101. //-----------------------------------------------------------------------------
  102. //
  103. // VActorAnimationController::initAnimationThread( pThread, pIndex, pPosition, pTimeScale );
  104. //
  105. // ...
  106. //
  107. //-----------------------------------------------------------------------------
  108. bool VActorAnimationController::initAnimationThread( TSThread *&pThread, const U32 &pIndex, const F32 &pPosition, const F32 &pTimeScale )
  109. {
  110. // Valid Object & Sequence?
  111. if ( !isValidObject() || !isAnimationSequence( pIndex ) )
  112. {
  113. // No, Quit Now.
  114. return false;
  115. }
  116. // Valid Thread?
  117. if ( !pThread )
  118. {
  119. // Create a Thread.
  120. pThread = getShapeInstance()->addThread();
  121. }
  122. // Init the Sequence.
  123. getShapeInstance()->setSequence( pThread, getAnimationSequence( pIndex ), pPosition );
  124. // Set Initial Time Scale.
  125. getShapeInstance()->setTimeScale( pThread, pTimeScale );
  126. // Valid.
  127. return true;
  128. }
  129. //-----------------------------------------------------------------------------
  130. //
  131. // VActorAnimationController::initBaseAnimation( pThread, pIndex, pPosition, pTimeScale );
  132. //
  133. // ...
  134. //
  135. //-----------------------------------------------------------------------------
  136. bool VActorAnimationController::initBaseAnimation( const U32 &pIndex, const F32 &pPosition, const F32 &pTimeScale )
  137. {
  138. return initAnimation( mBaseAnimation, pIndex, pPosition, pTimeScale );
  139. }
  140. //-----------------------------------------------------------------------------
  141. //
  142. // Accessor Methods.
  143. //
  144. //-----------------------------------------------------------------------------
  145. //-----------------------------------------------------------------------------
  146. //
  147. // VActorAnimationController::isValidObject();
  148. //
  149. // ...
  150. //
  151. //-----------------------------------------------------------------------------
  152. bool VActorAnimationController::isValidObject( void )
  153. {
  154. return ( mObject != NULL && mObject->getDataBlock() != NULL );
  155. }
  156. //-----------------------------------------------------------------------------
  157. //
  158. // VActorAnimationController::getObject();
  159. //
  160. // ...
  161. //
  162. //-----------------------------------------------------------------------------
  163. VActor *VActorAnimationController::getObject( void )
  164. {
  165. return mObject;
  166. }
  167. //-----------------------------------------------------------------------------
  168. //
  169. // VActorAnimationController::setObject( pObject );
  170. //
  171. // ...
  172. //
  173. //-----------------------------------------------------------------------------
  174. void VActorAnimationController::setObject( VActor *pObject )
  175. {
  176. // Set Object.
  177. mObject = pObject;
  178. // Set Table's Reference.
  179. mAnimationTable.setObject( pObject );
  180. }
  181. //-----------------------------------------------------------------------------
  182. //
  183. // VActorAnimationController::getShape();
  184. //
  185. // ...
  186. //
  187. //-----------------------------------------------------------------------------
  188. const TSShape *VActorAnimationController::getShape( void )
  189. {
  190. if ( !isValidObject() )
  191. {
  192. return NULL;
  193. }
  194. return mObject->getShape();
  195. }
  196. //-----------------------------------------------------------------------------
  197. //
  198. // VActorAnimationController::getShapeInstance();
  199. //
  200. // ...
  201. //
  202. //-----------------------------------------------------------------------------
  203. TSShapeInstance *VActorAnimationController::getShapeInstance( void )
  204. {
  205. if ( !isValidObject() )
  206. {
  207. return NULL;
  208. }
  209. return mObject->getShapeInstance();
  210. }
  211. //-----------------------------------------------------------------------------
  212. //
  213. // Animation Methods
  214. //
  215. //-----------------------------------------------------------------------------
  216. void VActorAnimationController::update( const F32 &pDelta )
  217. {
  218. // Valid Objects?
  219. if ( !isValidObject() )
  220. {
  221. // No, Quit Now.
  222. return;
  223. }
  224. // Update Animation State.
  225. mAnimationTable.execute();
  226. // Advance Threads.
  227. getShapeInstance()->advanceTime( pDelta, mBaseAnimation.Thread );
  228. }
  229. //-----------------------------------------------------------------------------
  230. //
  231. // VActorAnimationController::isAnimationSequence( pIndex );
  232. //
  233. // ...
  234. //
  235. //-----------------------------------------------------------------------------
  236. bool VActorAnimationController::isAnimationSequence( const U32 &pIndex )
  237. {
  238. return ( getAnimationSequence( pIndex ) != -1 );
  239. }
  240. //-----------------------------------------------------------------------------
  241. //
  242. // VActorAnimationController::getAnimationSequence( pIndex );
  243. //
  244. // ...
  245. //
  246. //-----------------------------------------------------------------------------
  247. S32 VActorAnimationController::getAnimationSequence( const U32 &pIndex )
  248. {
  249. // Valid Object?
  250. if ( !mObject || !mObject->getDataBlock() )
  251. {
  252. // No, Invalid Sequence.
  253. return -1;
  254. }
  255. // Return Sequence.
  256. return mObject->getDataBlock()->getAnimationSequence( pIndex );
  257. }
  258. //-----------------------------------------------------------------------------
  259. //
  260. // VActorAnimationController::getAnimation( pIndex );
  261. //
  262. // ...
  263. //
  264. //-----------------------------------------------------------------------------
  265. U32 VActorAnimationController::getAnimation( void )
  266. {
  267. // Base Animation Initialised?
  268. if ( !mBaseAnimation.Thread )
  269. {
  270. // Null.
  271. return U32_MAX;
  272. }
  273. // Return Current Animation.
  274. return mBaseAnimation.Index;
  275. }
  276. //-----------------------------------------------------------------------------
  277. //
  278. // VActorAnimationController::setAnimation( pIndex );
  279. //
  280. // ...
  281. //
  282. //-----------------------------------------------------------------------------
  283. void VActorAnimationController::setAnimation( const U32 &pIndex )
  284. {
  285. // Base Animation Initialised?
  286. if ( !mBaseAnimation.Thread || mBaseAnimation.Index == pIndex )
  287. {
  288. // Don't Update.
  289. return;
  290. }
  291. // Store as Current Animation.
  292. mBaseAnimation.Index = pIndex;
  293. // Fetch the Sequence.
  294. const S32 &sequence = getAnimationSequence( pIndex );
  295. // Valid?
  296. if ( sequence != -1 )
  297. {
  298. // Play the Sequence.
  299. getShapeInstance()->transitionToSequence( mBaseAnimation.Thread, sequence, 0.f, 0.15f, true );
  300. //getShapeInstance()->setSequence( mBaseAnimation.Thread, sequence, 0.f );
  301. }
  302. }