| | 1 | | using DCL.Controllers; |
| | 2 | | using DCL.ECS7.InternalComponents; |
| | 3 | | using DCL.ECSRuntime; |
| | 4 | | using DCL.Models; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using UnityEngine.Pool; |
| | 7 | |
|
| | 8 | | namespace DCL.ECSComponents |
| | 9 | | { |
| | 10 | | public class AnimatorHandler : IECSComponentHandler<PBAnimator> |
| | 11 | | { |
| 1 | 12 | | private static readonly ObjectPool<List<InternalAnimationPlayer.State>> animationsStatePool = |
| 3 | 13 | | new ObjectPool<List<InternalAnimationPlayer.State>>(() => new List<InternalAnimationPlayer.State>()); |
| | 14 | |
|
| | 15 | | private readonly IInternalECSComponent<InternalAnimationPlayer> internalAnimationPlayer; |
| | 16 | |
|
| 6 | 17 | | public AnimatorHandler(IInternalECSComponent<InternalAnimationPlayer> internalAnimationPlayer) |
| | 18 | | { |
| 6 | 19 | | this.internalAnimationPlayer = internalAnimationPlayer; |
| 6 | 20 | | } |
| | 21 | |
|
| 1 | 22 | | public void OnComponentCreated(IParcelScene scene, IDCLEntity entity) { } |
| | 23 | |
|
| | 24 | | public void OnComponentRemoved(IParcelScene scene, IDCLEntity entity) |
| | 25 | | { |
| 2 | 26 | | var storedData = internalAnimationPlayer.GetFor(scene, entity); |
| | 27 | |
|
| 2 | 28 | | if (storedData.HasValue) |
| | 29 | | { |
| 1 | 30 | | animationsStatePool.Release(storedData.Value.model.States); |
| 1 | 31 | | internalAnimationPlayer.RemoveFor(scene, entity); |
| | 32 | | } |
| 2 | 33 | | } |
| | 34 | |
|
| | 35 | | public void OnComponentModelUpdated(IParcelScene scene, IDCLEntity entity, PBAnimator model) |
| | 36 | | { |
| 4 | 37 | | var storedData = internalAnimationPlayer.GetFor(scene, entity); |
| | 38 | |
|
| 4 | 39 | | InternalAnimationPlayer internalModel = storedData?.model ?? new InternalAnimationPlayer(animationsStatePool |
| 4 | 40 | | List<InternalAnimationPlayer.State> stateList = internalModel.States; |
| 4 | 41 | | stateList.Clear(); |
| | 42 | |
|
| 16 | 43 | | for (int i = 0; i < model.States.Count; i++) |
| | 44 | | { |
| 4 | 45 | | var stateData = model.States[i]; |
| | 46 | |
|
| 4 | 47 | | stateList.Add(new InternalAnimationPlayer.State( |
| | 48 | | stateData.Clip, |
| | 49 | | stateData.Playing, |
| | 50 | | stateData.GetWeight(), |
| | 51 | | stateData.GetSpeed(), |
| | 52 | | stateData.GetLoop(), |
| | 53 | | stateData.GetShouldReset() |
| | 54 | | )); |
| | 55 | | } |
| | 56 | |
|
| 4 | 57 | | internalAnimationPlayer.PutFor(scene, entity, internalModel); |
| 4 | 58 | | } |
| | 59 | | } |
| | 60 | | } |