| | 1 | | using DCL.ECS7.InternalComponents; |
| | 2 | | using DCL.ECSRuntime; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | namespace ECSSystems.AnimationSystem |
| | 7 | | { |
| | 8 | | public class AnimationSystem |
| | 9 | | { |
| | 10 | | private IECSReadOnlyComponentsGroup<InternalAnimationPlayer, InternalAnimation> animationGroup; |
| | 11 | | private readonly IInternalECSComponent<InternalAnimation> animationComponent; |
| | 12 | |
|
| 2 | 13 | | public AnimationSystem(IECSReadOnlyComponentsGroup<InternalAnimationPlayer, InternalAnimation> animationGroup, |
| | 14 | | IInternalECSComponent<InternalAnimation> animationComponent) |
| | 15 | | { |
| 2 | 16 | | this.animationGroup = animationGroup; |
| 2 | 17 | | this.animationComponent = animationComponent; |
| 2 | 18 | | } |
| | 19 | |
|
| | 20 | | public void Update() |
| | 21 | | { |
| 1 | 22 | | var group = animationGroup.group; |
| | 23 | |
|
| 4 | 24 | | for (int i = 0; i < group.Count; i++) |
| | 25 | | { |
| 1 | 26 | | var elementData = group[i]; |
| 1 | 27 | | var animationPlayerModel = elementData.componentData1.model; |
| 1 | 28 | | var animationModel = elementData.componentData2.model; |
| | 29 | |
|
| 1 | 30 | | if (!animationPlayerModel.dirty && !animationModel.dirty) |
| | 31 | | continue; |
| | 32 | |
|
| 1 | 33 | | if (!animationModel.IsInitialized) |
| | 34 | | { |
| 1 | 35 | | SetupAnimation(animationModel.Animation); |
| 1 | 36 | | animationModel.IsInitialized = true; |
| 1 | 37 | | animationComponent.PutFor(elementData.scene, elementData.entity, animationModel); |
| | 38 | | } |
| | 39 | |
|
| 1 | 40 | | SetAnimationState(animationPlayerModel.States, animationModel.Animation); |
| | 41 | | } |
| 1 | 42 | | } |
| | 43 | |
|
| | 44 | | private static void SetupAnimation(Animation animation) |
| | 45 | | { |
| 1 | 46 | | int layerIndex = 0; |
| | 47 | |
|
| 1 | 48 | | animation.playAutomatically = true; |
| 1 | 49 | | animation.enabled = true; |
| 1 | 50 | | animation.Stop(); |
| | 51 | |
|
| | 52 | | //putting the component in play state if playAutomatically was true at that point. |
| 1 | 53 | | if (animation.clip) |
| 1 | 54 | | animation.clip.SampleAnimation(animation.gameObject, 0); |
| | 55 | |
|
| 6 | 56 | | foreach (AnimationState unityState in animation) |
| | 57 | | { |
| 2 | 58 | | unityState.clip.wrapMode = WrapMode.Loop; |
| 2 | 59 | | unityState.layer = layerIndex; |
| 2 | 60 | | unityState.blendMode = AnimationBlendMode.Blend; |
| 2 | 61 | | layerIndex++; |
| | 62 | | } |
| 1 | 63 | | } |
| | 64 | |
|
| | 65 | | private static void SetAnimationState(IList<InternalAnimationPlayer.State> animationState, Animation animation) |
| | 66 | | { |
| 1 | 67 | | if (animationState.Count == 0) |
| 0 | 68 | | return; |
| | 69 | |
|
| 4 | 70 | | for (int i = 0; i < animationState.Count; i++) |
| | 71 | | { |
| 1 | 72 | | var state = animationState[i]; |
| 1 | 73 | | AnimationState unityState = animation[state.Clip]; |
| | 74 | |
|
| 1 | 75 | | if (!unityState) |
| | 76 | | continue; |
| | 77 | |
|
| 1 | 78 | | unityState.weight = state.Weight; |
| | 79 | |
|
| 1 | 80 | | unityState.wrapMode = state.Loop ? WrapMode.Loop : WrapMode.Default; |
| | 81 | |
|
| 1 | 82 | | unityState.clip.wrapMode = unityState.wrapMode; |
| 1 | 83 | | unityState.speed = state.Speed; |
| 1 | 84 | | unityState.enabled = state.Playing; |
| | 85 | |
|
| 1 | 86 | | if (state.ShouldReset && animation.IsPlaying(state.Clip)) |
| | 87 | | { |
| 0 | 88 | | animation.Stop(state.Clip); |
| | 89 | |
|
| | 90 | | //Manually sample the animation. If the reset is not played again the frame 0 wont be applied |
| 0 | 91 | | unityState.clip.SampleAnimation(animation.gameObject, 0); |
| | 92 | | } |
| | 93 | |
|
| 1 | 94 | | if (state.Playing && !animation.IsPlaying(state.Clip)) |
| 0 | 95 | | animation.Play(state.Clip); |
| | 96 | | } |
| 1 | 97 | | } |
| | 98 | | } |
| | 99 | | } |