Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Animancer;
- using CorePresenter;
- using DIContainer;
- using ManagerResourcess;
- using ModelCore;
- using ModelCore.Universal;
- using ModelCore.Universal.AliasValue;
- using ModelCore.Universal.FuncToJss;
- using Pathfinding.Util;
- using Sirenix.Utilities;
- using TMPro.SpriteAssetUtilities;
- using UnityEngine;
- using ClipTransitionAsset = ManagerResourcess.ClipTransitionAsset;
- namespace MVP.Views
- {
- [AddComponentMenu(RootPresenter.PathToView+"V _ Anims")][RequireComponent(typeof(AnimancerComponent))]
- public class V_AnimacerAnim : MonoBehaviour
- {
- private AnimancerComponent _animacer;
- [DI] private PackOfResources _resources;
- private List<RootModel> _clips;
- private List<Action> _unsubscribes = new List<Action>();
- private Dictionary<string, Action> _stopActions = new Dictionary<string, Action>();
- // Main
- private const string AliasClip = "IdClip";
- private const string AliasContainer = "IdContainer";
- private const string AliasIsPlaying = "IsPlaying";
- // Extra
- private const string AliasSpeed = "Speed";
- private const string AliasLayer = "Layer";
- private const string AliasEndEvent = "End";
- private const string AliasStopEvent = "Stop";
- private void Awake() => _animacer = GetComponent<AnimancerComponent>();
- public void View(RootModel root)
- {
- _unsubscribes.ForEach(x=>x());
- _animacer.Stop();
- _unsubscribes.Clear();
- _stopActions.Clear();
- _clips = GetClips(root);
- _clips.ForEach(x=>Subscribe(x));
- }
- private void Subscribe(RootModel rootModel)
- {
- var idClip = rootModel.GetT<AliasString>(x => x.Alias == AliasClip);
- var idCont = rootModel.GetT<AliasString>(x => x.Alias == AliasContainer);
- var IsPlayed = rootModel.GetT<AliasBool>(x => x.Alias == AliasIsPlaying);
- var clip = (_resources.Get(idCont.Alias) as ClipTransitionAsset).Get(idClip.Alias);
- if (clip == null)
- {
- Debug.LogWarning($"Нет клипа в контейнере - {idCont.Alias} под id - {idClip.Alias}");
- return;
- }
- IsPlayed.Changed += PlayHandler(clip, rootModel);
- PlayHandler(clip, rootModel).Invoke(!IsPlayed.Value, IsPlayed.Value);
- _unsubscribes.Add(new Action(() => IsPlayed.Changed -= PlayHandler(clip, rootModel)));
- }
- private Action<bool, bool> PlayHandler(ClipTransition clip, RootModel root)
- {
- return (bool pastV, bool newV) => {
- if (pastV == newV) return;
- if (newV == false)
- {
- if (_stopActions.TryGetValue(root.Alias, out var r)) r();
- }
- else
- {
- var layer = root.GetIdT<AliasInt>($"I_{AliasLayer}");
- var speed = root.GetIdT<AliasFloat>($"F_{AliasSpeed}");
- var endEvent = root.GetIdT<JsEvent>($"Event_{AliasEndEvent}");
- var stopEvent = root.GetIdT<JsEvent>($"Event_{AliasStopEvent}");
- var otherEvent = root.GetTAll<JsEvent>(x => true).Except(new []{endEvent, stopEvent});
- var state = _animacer.Layers[layer != null ? 0 : layer.Value].Play(clip);
- if (speed != null) state.Speed = speed.Value;
- if (endEvent != null) state.Events.OnEnd = endEvent.Trig;
- otherEvent.ForEach(x =>
- {
- if (state.Events.Names.Contains(x.Alias))
- state.Events.SetCallback(x.Alias, x.Trig);
- });
- _stopActions.Add(root.Alias, new Action(() =>
- {
- stopEvent?.Trig();
- otherEvent.ForEach(x =>
- {
- if (state.Events.Names.Contains(x.Alias))
- state.Events.RemoveCallback(x.Alias, x.Trig);
- });
- state.Stop();
- }));
- }
- };
- }
- private List<RootModel> GetClips(RootModel root)
- {
- return root.GetTAll<RootModel>(x =>
- {
- var idClip = x.GetT<AliasString>(x => x.Alias == AliasClip);
- var idCont = x.GetT<AliasString>(x => x.Alias == AliasContainer);
- var idIsPlayed = x.GetT<AliasBool>(x => x.Alias == AliasIsPlaying);
- return idClip != null && idCont != null & idIsPlayed != null;
- });
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement