Advertisement
MaoChessy

V_AnimacerAnim

Mar 12th, 2022
932
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.22 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Animancer;
  5. using CorePresenter;
  6. using DIContainer;
  7. using ManagerResourcess;
  8. using ModelCore;
  9. using ModelCore.Universal;
  10. using ModelCore.Universal.AliasValue;
  11. using ModelCore.Universal.FuncToJss;
  12. using Sirenix.Utilities;
  13. using TMPro.SpriteAssetUtilities;
  14. using UnityEngine;
  15. using UnityEngine.PlayerLoop;
  16. using ClipTransitionAsset = ManagerResourcess.ClipTransitionAsset;
  17.  
  18. namespace MVP.Views
  19. {
  20.     [AddComponentMenu(RootPresenter.PathToView+"V _ Anims")][RequireComponent(typeof(AnimancerComponent))]
  21.     public class V_AnimacerAnim : ViewRootBase
  22.     {
  23.         private AnimancerComponent _animacer;
  24.        
  25.         [DI] private PackOfResources _resources;
  26.        
  27.         private List<RootModel> _clips;
  28.         private List<Action> _unsubscribes = new List<Action>();
  29.         private Dictionary<string, Action> _stopActions = new Dictionary<string, Action>();
  30.        
  31.         // Main
  32.         private const string AliasClip = "IdClip";
  33.         private const string AliasContainer = "IdContainer";
  34.         private const string AliasIsPlaying = "IsPlaying";
  35.        
  36.         // Extra
  37.         private const string AliasSpeed = "Speed";
  38.         private const string AliasLayer = "Layer";
  39.         private const string AliasEndEvent = "End";
  40.         private const string AliasStopEvent = "Stop";
  41.        
  42.         protected override void CustomAwake() => _animacer = GetComponent<AnimancerComponent>();
  43.  
  44.         public override void View(RootModel rootModel) {
  45.             _unsubscribes.ForEach(x=>x());
  46.             _animacer.Stop();
  47.             _unsubscribes.Clear();
  48.             _stopActions.Clear();
  49.            
  50.             _clips = GetClips(rootModel);
  51.             _clips.ForEach(x=>Subscribe(x));
  52.         }
  53.  
  54.         private void Subscribe(RootModel rootModel) {
  55.             var idClip = rootModel.GetT<AliasString>(x => x.Alias == AliasClip);
  56.             var idCont = rootModel.GetT<AliasString>(x => x.Alias == AliasContainer);
  57.             var IsPlayed = rootModel.GetT<AliasBool>(x => x.Alias == AliasIsPlaying);
  58.  
  59.             var container = _resources.Get(idCont.Value) as ClipTransitionAsset;
  60.             if (container == null) {
  61.                 Debug.LogWarning($"Нет контейнерf - {idCont.Value}");
  62.                 return;
  63.             }
  64.             var clip = container.Get(idClip.Value);
  65.             if (clip == null) {
  66.                 Debug.LogWarning($"Нет клипа в контейнере - {idCont.Value} под id - {idClip.Value}");
  67.                 return;
  68.             }
  69.  
  70.             IsPlayed.Update += PlayHandler(clip, rootModel);
  71.             PlayHandler(clip, rootModel).Invoke(!IsPlayed.Value, IsPlayed.Value);
  72.             _unsubscribes.Add(new Action(() => IsPlayed.Update -= PlayHandler(clip, rootModel)));
  73.         }
  74.  
  75.         private Action<bool, bool> PlayHandler(ClipTransition clip, RootModel rootModel) {
  76.             return (bool pastV, bool newV) => {
  77.                 if (pastV == newV) return;
  78.                 if (newV == false) {
  79.                     if (_stopActions.TryGetValue(rootModel.Alias, out var r)) r();
  80.                 }
  81.                 else {
  82.                     var layer = rootModel.GetIdT<AliasInt>($"I_{AliasLayer}");
  83.                     var speed = rootModel.GetIdT<AliasFloat>($"F_{AliasSpeed}");
  84.                    
  85.                     var endEvent = rootModel.GetIdT<JsEvent>($"Event_{AliasEndEvent}");
  86.                     var stopEvent = rootModel.GetIdT<JsEvent>($"Event_{AliasStopEvent}");
  87.                     var otherEvent = rootModel.GetTAll<JsEvent>(x => true).Except(new []{endEvent, stopEvent});
  88.                    
  89.                     var state = _animacer.Layers[layer != null ? layer.Value : 0].Play(clip);
  90.                     if (speed != null) state.Speed = speed.Value;
  91.                     if (endEvent != null) state.Events.OnEnd = EndHandler(state, endEvent);
  92.                     otherEvent.ForEach(x => { if (state.Events.Names.Contains(x.Alias)) state.Events.SetCallback(x.Alias, x.Trig); });
  93.                    
  94.                     if(!_stopActions.ContainsKey(rootModel.Alias)) {
  95.                         _stopActions.Add(rootModel.Alias, new Action(() => {
  96.                             stopEvent?.Trig();
  97.                             otherEvent.ForEach(x => { if (state.Events.Names.Contains(x.Alias)) state.Events.RemoveCallback(x.Alias, x.Trig); });
  98.                             state.Stop();
  99.                         }));
  100.                     }
  101.                 }
  102.             };
  103.         }
  104.  
  105.         private Action EndHandler(AnimancerState state, JsEvent endEvent) {
  106.             return ()=> {
  107.                 endEvent.Trig();
  108.                 state.NormalizedTime = 0;
  109.             };
  110.         }
  111.  
  112.         private List<RootModel> GetClips(RootModel rootModel) {
  113.             return rootModel.GetTAll<RootModel>(x => {
  114.                 var idClip = x.GetT<AliasString>(x => x.Alias == AliasClip);
  115.                 var idCont = x.GetT<AliasString>(x => x.Alias == AliasContainer);
  116.                 var idIsPlayed = x.GetT<AliasBool>(x => x.Alias == AliasIsPlaying);
  117.                 return idClip != null && idCont != null & idIsPlayed != null;
  118.             });
  119.         }
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement