using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using DG.Tweening; using Zenject; using UltraFace; using Cysharp.Threading.Tasks; using System.Threading; using Random = UnityEngine.Random; public class AnimationHandler : MonoBehaviour { [SerializeField] private Transform _mirror; [SerializeField] private Transform _sberPrimeLogo; [SerializeField] private ParticleSystem _sberEffect; private Vector3 _sberScale, _sberPosition; [SerializeField] private List _firstGroup = new List(); [SerializeField] private List _secondGroup = new List(); [SerializeField] private List _thirdGroup = new List(); [SerializeField] private List _objects = new List(); [SerializeField] private List _effects = new List(); [Inject] private Visualizer _visualizer; private Dictionary _scales; private Dictionary _positions; private Dictionary _effectDict; private Dictionary _rotations; private Vector3 _baseScale; private List _animations = new List(); private static CancellationTokenSource _cts = new CancellationTokenSource(); private CancellationToken _ct = _cts.Token; public event Action OnAnimationEnded; private void Start() { _sberScale = _sberPrimeLogo.localScale; _sberPrimeLogo.DOLocalMoveY(_sberPrimeLogo.localPosition.y - .25f, 2.5f) .SetEase(Ease.InOutSine) .SetLoops(-1, LoopType.Yoyo); _sberPrimeLogo.localScale = Vector3.zero; var sberEffectEmission = _sberEffect.emission; sberEffectEmission.enabled = false; _scales = new Dictionary(); _positions = new Dictionary(); _effectDict = new Dictionary(); _rotations = new Dictionary(); _visualizer.OnDetectionStatusChanged += isDetected => OnFaceDetected(isDetected).Forget(); foreach(var item in _objects) { int index = _objects.IndexOf(item); _scales.Add(item.name, item.localScale); item.DOScale(Vector3.zero, 0); _positions.Add(item.name, item.position); item.localPosition = Vector3.zero; _rotations.Add(item.name, item.rotation); } for(int i = 0; i < _objects.Count; i++) _effectDict.Add(_objects[i].name, _effects[i]); foreach(var effect in _effects) { var emission = effect.emission; emission.enabled = false; } } private async UniTaskVoid OnFaceDetected(bool isDetected) { if(isDetected) { await UniTask.Delay(300, false, PlayerLoopTiming.Update, _ct); _sberPrimeLogo.DOScale(_sberScale, .2f); var sberEmission = _sberEffect.emission; sberEmission.enabled = true; foreach(var effect in _effects) { var emission = effect.emission; emission.enabled = true; } await PlayGroupAnimation(_firstGroup); await UniTask.Delay(50, false, PlayerLoopTiming.Update, _ct); await PlayGroupAnimation(_secondGroup); await UniTask.Delay(50, false, PlayerLoopTiming.Update, _ct); await PlayGroupAnimation(_thirdGroup); OnAnimationEnded?.Invoke(); foreach(var effect in _effects) { var emission = effect.emission; emission.enabled = false; } sberEmission.enabled = false; } else { _cts.Cancel(); _cts = new CancellationTokenSource(); _ct = _cts.Token; foreach(var tween in _animations) tween.Kill(); _animations = new List(); foreach(var item in _objects) { item.DOScale(0, .2f) .OnComplete(()=> { item.DOLocalMove(Vector3.zero, 0); }); } _sberPrimeLogo.DOScale(Vector3.zero, .2f); var sberEmission = _sberEffect.emission; sberEmission.enabled = false; } #region old animation // if(isDetected) // { // foreach(var tween in _rotationAnimations) // tween.Kill(); // _rotationAnimations = new List(); // foreach(var item in _objects) // { // int index = _objects.IndexOf(item); // item.DOLocalRotate(Vector3.up * 360 * 2, Random.Range(1f, 2f)) // .SetRelative() // .SetEase(Ease.InOutSine) // .OnComplete(() => // { // _rotationAnimations.Add(item.DOLocalRotate(new Vector3(0, index % 2 == 0 ? 360f : 0, 0), Random.Range(10f, 14f)) // .SetLoops(-1, LoopType.Yoyo) // .SetRelative() // .SetEase(Ease.InOutSine)); // }); // item.DOScale(_scales[item.name], 1f) // .OnComplete(async () => // { // var emission = _effectDict[item.name].emission; // emission.enabled = true; // await UniTask.Delay(1500); // emission.enabled = false; // }); // } // } // else // { // foreach(var effect in _effects) // { // var emission = effect.emission; // emission.enabled = false; // } // foreach(var item in _objects) // item.DOScale(Vector3.zero, .2f); // } #endregion } private async UniTask PlayGroupAnimation(List group) { foreach(var item in group) { item.rotation = _rotations[item.name]; item.DOScale(_scales[item.name], .9f); item.DOMove(_positions[item.name], .7f) .SetEase(Ease.OutQuad) .OnComplete(() => { bool isRight = item.transform.position.x - _mirror.position.x > 0; bool isBehind = item.transform.position.z > _mirror.position.z; int dir = isRight ? -1 : 1; _animations.Add(item.DOLocalMoveY(item.localPosition.y + .5f, Random.Range(2f, 5f)) .SetEase(Ease.InOutSine) .SetLoops(-1, LoopType.Yoyo)); _animations.Add( item.DOLocalRotate(new Vector3(0, 45, 0), Random.Range(10f, 14f)) .SetLoops(-1, LoopType.Yoyo) .SetRelative() .SetEase(Ease.InOutSine)); if(!isBehind) { _animations.Add( item.DOLocalMoveZ(-1f, Random.Range(3f, 5f)) .SetRelative() .SetLoops(-1, LoopType.Yoyo) .SetEase(Ease.InOutSine)); } _animations.Add( item.DOLocalMoveX(isBehind ? .6f * dir : .2f * dir, Random.Range(3f, 5f)) .SetRelative() .SetLoops(-1, LoopType.Yoyo) .SetEase(Ease.InOutSine)); }); await UniTask.Delay(200, false, PlayerLoopTiming.Update, _ct); } Debug.Log("конец группы"); } }