using System; using Cysharp.Threading.Tasks; using DG.Tweening; using GadGame.Event.Type; using TMPro; using UnityEngine; using UnityEngine.Serialization; using UnityEngine.UI; using UnityEngine.Video; using GadGame.Network; namespace GadGame.Scripts.Coffee { public class CoffeeController : MonoBehaviour { [SerializeField] private VoidEvent _engageReadyEvent; [SerializeField] private StringEvent _generateImageSuccessEvent; [SerializeField] private BoolEvent _playPassByAnimEvent; [SerializeField] private BoolEvent _playVideoEvent; [SerializeField] private FloatEvent _readyCountDownEvent; [SerializeField] private StringEvent _encodeImageEvent; [SerializeField] private VideoPlayer _idleVideo; [SerializeField] private LoadImageEncoded _userImager; [SerializeField] private Image _process; [SerializeField] private Image _faceGuide; [SerializeField] private TextMeshProUGUI _hintText; [SerializeField] private TextMeshProUGUI _notifyText; [SerializeField] private GameObject _notify; [SerializeField] private GameObject _hint; [SerializeField] private Image _loading; [SerializeField] private float _loadingSpeed = 100; [SerializeField] private string[] _texts; [SerializeField] private string[] _loadingTexts; private int _indexText; private bool _isLoading; private float _timer; private async void Awake() { _idleVideo.targetCameraAlpha = 1; _loading.transform.DOLocalRotate(new Vector3(0, 0, 360), 10 / _loadingSpeed, RotateMode.FastBeyond360) .SetLoops(-1) .SetRelative(true) .SetEase(Ease.Linear); _notify.SetActive(false); _hint.SetActive(false); _faceGuide.enabled = true; } private void OnEnable() { _engageReadyEvent.Register(OnEngageReady); _generateImageSuccessEvent.Register(OnGenerateImageSuccess); _playPassByAnimEvent.Register(Play); _playVideoEvent.Register(SetPlayVideo); _readyCountDownEvent.Register(SetReadyCountDown); _encodeImageEvent.Register(OnGetEncodeImage); } private void OnDisable() { _engageReadyEvent.Unregister(OnEngageReady); _generateImageSuccessEvent.Unregister(OnGenerateImageSuccess); _playPassByAnimEvent.Unregister(Play); _playVideoEvent.Unregister(SetPlayVideo); _readyCountDownEvent.Unregister(SetReadyCountDown); _encodeImageEvent.Unregister(OnGetEncodeImage); } private void Update() { if(!_isLoading) return; _timer += Time.deltaTime; // if (_timer >= 3) // { // _timer = 0; // _indexText++; // if (_indexText > _loadingTexts.Length - 1) // { // _indexText = 0; // } // _hintText.text = _loadingTexts[_indexText]; // } Debug.Log(_timer); if(UdpSocket.Instance.DataReceived.Gender != null && _timer > 3) { _notifyText.text = UdpSocket.Instance.DataReceived.Gender == "Male" ? "Hmmm, để xem kiếp trước bạn là ai nào, chàng trai" : "Hmmm, để xem kiếp trước bạn là ai nào, cô gái"; } else { _notifyText.text = "Khuấy đều tay, kiếp trước của bạn sẽ hiện ra, chờ một chút nhé"; } } private void Play(bool engage) { // videoPlayer.gameObject.SetActive(!passBy); // _transform.DOAnchorPosX(engage ? -1000 : 0, 1); // _hintText.text = _texts[0]; } private void OnGenerateImageSuccess(string desc) { _isLoading = false; _loading.DOFade(0, 0.5f); // _hintText.text = desc; // _notify.SetActive(false); _hint.SetActive(false); _notifyText.text = UdpSocket.Instance.DataReceived.Description; } private void OnGetEncodeImage(string filePath) { _userImager.LoadImage(filePath); _notify.SetActive(true); } private void OnEngageReady() { _process.fillAmount = 0; _isLoading = true; // _hintText.text = _loadingTexts[_indexText]; _loading.DOFade(1, 1f); _hint.SetActive(true); // _notifyText.text = UdpSocket.Instance.DataReceived.Gender < 0.5 ? "Hmmm, có vẻ thú vị đấy chàng trai" : "Hmmm, có vẻ thú vị đấy cô gái"; // _notifyText.text = "Hmmm, tiền kiếp có vẻ thú vị đấy nhỉ!!!"; _faceGuide.enabled = false; UdpSocket.Instance.SendDataToPython("Begin"); } private async void SetPlayVideo(bool value){ if(value) { while (_idleVideo.targetCameraAlpha < 1) { _idleVideo.targetCameraAlpha += Time.deltaTime * 3; await UniTask.Yield(); if(_idleVideo == null){ return; } } _idleVideo.targetCameraAlpha = 1; } else { while (_idleVideo.targetCameraAlpha > 0) { if(_idleVideo == null){ return; } _idleVideo.targetCameraAlpha -= Time.deltaTime * 3; await UniTask.Yield(); if(_idleVideo == null){ return; } } _idleVideo.targetCameraAlpha = 0; } } private void SetReadyCountDown(float progress){ // _hintText.text = _texts[1]; _process.fillAmount = 1 - progress ; } } }