animator.monkey2 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. Namespace mojo3d
  2. #rem monkeydoc The Animator class.
  3. #end
  4. Class Animator Extends Component
  5. Const Type:=New ComponentType( "Animator",0,ComponentTypeFlags.Singleton )
  6. Field Finished:Void()
  7. Method New( entity:Entity )
  8. Super.New( entity,Type )
  9. AddInstance()
  10. End
  11. Method New( entity:Entity,animator:Animator )
  12. Super.New( entity,Type )
  13. _skeleton=animator._skeleton.Slice( 0 )
  14. For Local i:=0 Until _skeleton.Length
  15. _skeleton[i]=_skeleton[i].LastCopy
  16. End
  17. _animations=animator._animations
  18. AddInstance( animator )
  19. End
  20. Property Skeleton:Entity[]()
  21. Return _skeleton
  22. Setter( skeleton:Entity[] )
  23. _skeleton=skeleton
  24. End
  25. Property Animations:Stack<Animation>()
  26. Return _animations
  27. Setter( animations:Stack<Animation> )
  28. _animations=animations
  29. End
  30. Property MasterSpeed:Float()
  31. Return _speed
  32. Setter( speed:Float )
  33. _speed=speed
  34. End
  35. Property Animating:Animation()
  36. Return _playing ? _animation Else Null
  37. End
  38. Property Paused:Bool()
  39. Return _paused
  40. Setter( paused:Bool )
  41. _paused=paused
  42. End
  43. Property Time:Float()
  44. Return _time
  45. Setter( time:Float )
  46. _time=time
  47. End
  48. Method FindAnimation:Animation( name:String )
  49. For Local animation:=Eachin _animations
  50. If animation.Name=name Return animation
  51. Next
  52. Return Null
  53. End
  54. Method Animate( index:Int=0,transition:Float=0.0,finished:Void()=Null )
  55. Animate( _animations[index],transition,finished )
  56. End
  57. Method Animate( name:String,transition:Float=0.0,finished:Void()=Null )
  58. Animate( FindAnimation( name ),transition,finished )
  59. End
  60. Method Animate( animation:Animation,transition:Float=0.0,finished:Void()=Null )
  61. If Not animation return
  62. If _playing And _animation=animation Return
  63. If _playing And transition>0
  64. _animation0=_animation
  65. _time0=_time
  66. _transition=transition
  67. _transtime=0
  68. _trans=True
  69. Else
  70. _trans=False
  71. Endif
  72. _animation=animation
  73. _time=0
  74. _finished=finished
  75. _playing=True
  76. End
  77. Method Stop()
  78. _playing=False
  79. End
  80. Protected
  81. Method OnCopy:Animator( entity:Entity ) Override
  82. Return New Animator( entity,Self )
  83. End
  84. Method OnUpdate( elapsed:Float ) Override
  85. If _paused Or Not _playing Return
  86. Local blend:=0.0
  87. If _trans
  88. _transtime+=elapsed
  89. If _transtime<_transition
  90. blend=_transtime/_transition
  91. Else
  92. _trans=False
  93. Endif
  94. Endif
  95. Local duration:=_animation.Duration/_animation.Hertz
  96. _time+=_speed*elapsed
  97. If _time>=duration
  98. Select _animation.Mode
  99. Case AnimationMode.OneShot
  100. _time=duration
  101. _playing=False
  102. Case AnimationMode.Looping
  103. _time-=duration
  104. End
  105. _finished()
  106. Endif
  107. If _trans
  108. Local duration0:=_animation0.Duration/_animation0.Hertz
  109. _time0+=_speed0*elapsed
  110. If _time0>=duration0
  111. Select _animation0.Mode
  112. Case AnimationMode.OneShot
  113. _time0=duration0
  114. Case AnimationMode.Looping
  115. _time0-=duration0
  116. End
  117. Endif
  118. UpdateSkeleton( _animation0,_time0,_animation,_time,blend )
  119. Else
  120. UpdateSkeleton( _animation,_time,Null,0,0 )
  121. Endif
  122. End
  123. Private
  124. Field _skeleton:Entity[]
  125. Field _animations:=New Stack<Animation>
  126. Field _playing:Bool=False
  127. Field _paused:Bool=False
  128. Field _transtime:Float
  129. Field _transition:Float
  130. Field _trans:Bool
  131. Field _animation0:Animation
  132. Field _speed0:Float
  133. Field _time0:Float
  134. Field _animation:Animation
  135. Field _speed:Float=1
  136. Field _time:Float
  137. Field _finished:Void()
  138. Method UpdateSkeleton( playing0:Animation,time0:Float,playing1:Animation,time1:Float,alpha:Float )
  139. time0*=playing0?.Hertz
  140. time1*=playing1?.Hertz
  141. For Local i:=0 Until _skeleton.Length
  142. Local chan0:=playing0 ? playing0.Channels[i] Else Null
  143. Local chan1:=playing1 ? playing1.Channels[i] Else Null
  144. If chan0?.PositionKeys
  145. If chan1?.PositionKeys
  146. _skeleton[i].LocalPosition=chan0.GetPosition( time0 ).Blend( chan1.GetPosition( time1 ),alpha )
  147. Else
  148. _skeleton[i].LocalPosition=chan0.GetPosition( time0 )
  149. Endif
  150. Endif
  151. If chan0?.RotationKeys
  152. If chan1?.RotationKeys
  153. _skeleton[i].LocalBasis=chan0.GetRotation( time0 ).Slerp( chan1.GetRotation( time1 ),alpha )
  154. Else
  155. _skeleton[i].LocalBasis=chan0.GetRotation( time0 )
  156. Endif
  157. Endif
  158. If chan0?.ScaleKeys
  159. If chan1?.ScaleKeys
  160. _skeleton[i].LocalScale=chan0.GetScale( time0 ).Blend( chan1.GetScale( time1 ),alpha )
  161. Else
  162. _skeleton[i].LocalScale=chan0.GetScale( time0 )
  163. Endif
  164. Endif
  165. #rem
  166. If playing0 And playing1
  167. Local pos0:=chan0 ? chan0.GetPosition( time0 ) Else New Vec3f
  168. Local rot0:=chan0 ? chan0.GetRotation( time0 ) Else New Quatf
  169. Local scl0:=chan0 ? chan0.GetScale( time0 ) Else New Vec3f( 1 )
  170. Local pos1:=chan1 ? chan1.GetPosition( time1 ) Else New Vec3f
  171. Local rot1:=chan1 ? chan1.GetRotation( time1 ) Else New Quatf
  172. Local scl1:=chan1 ? chan1.GetScale( time1 ) Else New Vec3f( 1 )
  173. _skeleton[i].LocalPosition=pos0.Blend( pos1,alpha )
  174. _skeleton[i].LocalBasis=rot0.Slerp( rot1,alpha )
  175. _skeleton[i].LocalScale=scl0.Blend( scl1,alpha )
  176. Else If playing0
  177. Local pos0:=chan0 ? chan0.GetPosition( time0 ) Else New Vec3f
  178. Local rot0:=chan0 ? chan0.GetRotation( time0 ) Else New Quatf
  179. Local scl0:=chan0 ? chan0.GetScale( time0 ) Else New Vec3f( 1 )
  180. _skeleton[i].LocalPosition=pos0
  181. _skeleton[i].LocalBasis=rot0
  182. _skeleton[i].LocalScale=scl0
  183. Else If playing1
  184. Local pos1:=chan1 ? chan1.GetPosition( time1 ) Else New Vec3f
  185. Local rot1:=chan1 ? chan1.GetRotation( time1 ) Else New Quatf
  186. Local scl1:=chan1 ? chan1.GetScale( time1 ) Else New Vec3f( 1 )
  187. _skeleton[i].LocalPosition=pos1
  188. _skeleton[i].LocalBasis=rot1
  189. _skeleton[i].LocalScale=scl1
  190. Endif
  191. #end
  192. Next
  193. End
  194. End