Advertisement
MaoChessy

test view

Mar 2nd, 2022
1,054
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.92 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 Pathfinding.Util;
  13. using Sirenix.Utilities;
  14. using TMPro.SpriteAssetUtilities;
  15. using UnityEngine;
  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 : MonoBehaviour
  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.         private void Awake() => _animacer = GetComponent<AnimancerComponent>();
  43.  
  44.         public void View(RootModel root)
  45.         {
  46.             _unsubscribes.ForEach(x=>x());
  47.             _animacer.Stop();
  48.             _unsubscribes.Clear();
  49.             _stopActions.Clear();
  50.            
  51.             _clips = GetClips(root);
  52.             _clips.ForEach(x=>Subscribe(x));
  53.         }
  54.  
  55.         private void Subscribe(RootModel rootModel)
  56.         {
  57.             var idClip = rootModel.GetT<AliasString>(x => x.Alias == AliasClip);
  58.             var idCont = rootModel.GetT<AliasString>(x => x.Alias == AliasContainer);
  59.             var IsPlayed = rootModel.GetT<AliasBool>(x => x.Alias == AliasIsPlaying);
  60.  
  61.             var clip = (_resources.Get(idCont.Alias) as ClipTransitionAsset).Get(idClip.Alias);
  62.             if (clip == null)
  63.             {
  64.                 Debug.LogWarning($"Нет клипа в контейнере - {idCont.Alias} под id - {idClip.Alias}");
  65.                 return;
  66.             }
  67.  
  68.             IsPlayed.Changed += PlayHandler(clip, rootModel);
  69.             PlayHandler(clip, rootModel).Invoke(!IsPlayed.Value, IsPlayed.Value);
  70.             _unsubscribes.Add(new Action(() => IsPlayed.Changed -= PlayHandler(clip, rootModel)));
  71.         }
  72.  
  73.         private Action<bool, bool> PlayHandler(ClipTransition clip, RootModel root)
  74.         {
  75.             return (bool pastV, bool newV) => {
  76.                 if (pastV == newV) return;
  77.                 if (newV == false)
  78.                 {
  79.                     if (_stopActions.TryGetValue(root.Alias, out var r)) r();
  80.                 }
  81.                 else
  82.                 {
  83.                     var layer = root.GetIdT<AliasInt>($"I_{AliasLayer}");
  84.                     var speed = root.GetIdT<AliasFloat>($"F_{AliasSpeed}");
  85.                    
  86.                     var endEvent = root.GetIdT<JsEvent>($"Event_{AliasEndEvent}");
  87.                     var stopEvent = root.GetIdT<JsEvent>($"Event_{AliasStopEvent}");
  88.                     var otherEvent = root.GetTAll<JsEvent>(x => true).Except(new []{endEvent, stopEvent});
  89.                    
  90.                     var state = _animacer.Layers[layer != null ? 0 : layer.Value].Play(clip);
  91.                     if (speed != null) state.Speed = speed.Value;
  92.                     if (endEvent != null) state.Events.OnEnd = endEvent.Trig;
  93.                     otherEvent.ForEach(x =>
  94.                     {
  95.                         if (state.Events.Names.Contains(x.Alias))
  96.                             state.Events.SetCallback(x.Alias, x.Trig);
  97.                     });
  98.                    
  99.                     _stopActions.Add(root.Alias, new Action(() =>
  100.                     {
  101.                         stopEvent?.Trig();
  102.                         otherEvent.ForEach(x =>
  103.                         {
  104.                             if (state.Events.Names.Contains(x.Alias))
  105.                                 state.Events.RemoveCallback(x.Alias, x.Trig);
  106.                         });
  107.                         state.Stop();
  108.                     }));
  109.                 }
  110.             };
  111.         }
  112.  
  113.         private List<RootModel> GetClips(RootModel root)
  114.         {
  115.             return root.GetTAll<RootModel>(x =>
  116.             {
  117.                 var idClip = x.GetT<AliasString>(x => x.Alias == AliasClip);
  118.                 var idCont = x.GetT<AliasString>(x => x.Alias == AliasContainer);
  119.                 var idIsPlayed = x.GetT<AliasBool>(x => x.Alias == AliasIsPlaying);
  120.                 return idClip != null && idCont != null & idIsPlayed != null;
  121.             });
  122.         }
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement