animator.monkey2 5.8 KB

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