89 lines
2.3 KiB
C#

using DG.Tweening;
using System;
using UnityEngine;
using UnityEngine.Video;
public class Promo : MonoBehaviour, IControlable
{
[Header("Анимация")]
[SerializeField] private RectTransform _panel;
[SerializeField] private float _openedAnchorY;
[SerializeField] private float _closedAnchorY;
[SerializeField] private Ease _openEase;
[SerializeField] private Ease _closeEase;
[SerializeField] private float _openDuration;
[SerializeField] private float _closeDuration;
[Header("Видео")]
[SerializeField] VideoPlayer _videoPlayer;
[SerializeField] MenuPromoTransit _menuPromoTransit;
private Sequence _animation;
public bool ControlsEnabled { get; set; }
public void Open(Action callback)
{
KillActiveAnimation();
ControlsEnabled = true;
_panel.gameObject.SetActive(true);
_videoPlayer.Play();
_videoPlayer.loopPointReached += VideoEnded;
_animation = GetTransitionSequence(open: true);
_animation.onComplete += () => callback?.Invoke();
_animation.PlayForward();
}
private void VideoEnded(VideoPlayer source)
{
_menuPromoTransit.GotoMenu();
}
public void Close(Action callback)
{
ControlsPerformer.Instance.SetControlable(null);
ControlsEnabled = false;
KillActiveAnimation();
_animation = GetTransitionSequence(open: false);
_animation.onComplete += () =>
{
_videoPlayer.Stop();
_panel.gameObject.SetActive(false);
callback?.Invoke();
};
_animation.PlayForward();
}
private Sequence GetTransitionSequence(bool open)
{
float targetY = open ? _openedAnchorY : _closedAnchorY;
float duration = open ? _openDuration : _closeDuration;
Ease ease = open ? _openEase : _closeEase;
Sequence seq = DOTween.Sequence();
seq.Append(_panel.DOAnchorPosY(targetY, duration).SetEase(ease));
return seq;
}
private void KillActiveAnimation()
{
if (_animation == null) return;
_animation.Kill();
_animation = null;
}
public void UpdateControls()
{
if (In.AnyButton())
{
_menuPromoTransit.GotoMenu();
}
}
}