last update

This commit is contained in:
2023-06-04 09:58:43 +05:00
parent dccf25674e
commit 54905c2d2b
33 changed files with 2152 additions and 3070 deletions
+173 -38
View File
@@ -1,32 +1,61 @@
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 _sberText;
[SerializeField] private Transform _mirror;
[SerializeField] private Transform _sberPrimeLogo;
[SerializeField] private ParticleSystem _sberEffect;
private Vector3 _sberScale, _sberPosition;
[SerializeField] private List<Transform> _firstGroup = new List<Transform>();
[SerializeField] private List<Transform> _secondGroup = new List<Transform>();
[SerializeField] private List<Transform> _thirdGroup = new List<Transform>();
[SerializeField] private List<Transform> _objects = new List<Transform>();
[SerializeField] private List<ParticleSystem> _effects = new List<ParticleSystem>();
[Inject] private Visualizer _visualizer;
private Dictionary<string, Vector3> _scales;
private Dictionary<string, Vector3> _positions;
private Dictionary<string, ParticleSystem> _effectDict;
private Dictionary<string, Quaternion> _rotations;
private Vector3 _baseScale;
private List<Tween> _rotationAnimations = new List<Tween>();
private List<Tween> _animations = new List<Tween>();
private static CancellationTokenSource _cts = new CancellationTokenSource();
private CancellationToken _ct = _cts.Token;
public event Action OnAnimationEnded;
private void Start()
{
_sberText.DOLocalMoveY(_sberText.localPosition.y - .25f, 2.5f)
_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<string, Vector3>();
_positions = new Dictionary<string, Vector3>();
_effectDict = new Dictionary<string, ParticleSystem>();
_visualizer.OnDetectionStatusChanged += OnFaceDetected;
_rotations = new Dictionary<string, Quaternion>();
_visualizer.OnDetectionStatusChanged += isDetected => OnFaceDetected(isDetected).Forget();
foreach(var item in _objects)
{
@@ -35,9 +64,10 @@ public class AnimationHandler : MonoBehaviour
_scales.Add(item.name, item.localScale);
item.DOScale(Vector3.zero, 0);
item.DOLocalMoveY(item.localPosition.y + (index == _objects.Count - 1 ? .2f : .5f), Random.Range(2f, 5f))
.SetEase(Ease.InOutSine)
.SetLoops(-1, LoopType.Yoyo);
_positions.Add(item.name, item.position);
item.localPosition = Vector3.zero;
_rotations.Add(item.name, item.rotation);
}
for(int i = 0; i < _objects.Count; i++)
@@ -50,49 +80,154 @@ public class AnimationHandler : MonoBehaviour
}
}
private void OnFaceDetected(bool isDetected)
private async UniTaskVoid OnFaceDetected(bool isDetected)
{
if(isDetected)
{
foreach(var tween in _rotationAnimations)
tween.Kill();
_rotationAnimations = new List<Tween>();
await UniTask.Delay(300, false, PlayerLoopTiming.Update, _ct);
foreach(var item in _objects)
_sberPrimeLogo.DOScale(_sberScale, .2f);
var sberEmission = _sberEffect.emission;
sberEmission.enabled = true;
foreach(var effect in _effects)
{
int index = _objects.IndexOf(item);
if(index != _objects.Count - 1 || index % 2 == 0)
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(() =>
{
var emission = _effectDict[item.name].emission;
emission.enabled = true;
});
var emission = effect.emission;
emission.enabled = true;
}
}
else
{
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;
}
foreach(var item in _objects)
item.DOScale(Vector3.zero, .2f);
sberEmission.enabled = false;
}
else
{
_cts.Cancel();
_cts = new CancellationTokenSource();
_ct = _cts.Token;
foreach(var tween in _animations)
tween.Kill();
_animations = new List<Tween>();
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<Tween>();
// 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<Transform> 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("конец группы");
}
}