smart-interactive-display/Assets/GadGame/Scripts/Coffee/CoffeeController.cs

162 lines
5.9 KiB
C#
Raw Normal View History

2024-06-19 04:29:32 -07:00
using System;
using Cysharp.Threading.Tasks;
using DG.Tweening;
using GadGame.Event.Type;
using TMPro;
using UnityEngine;
2024-06-19 20:25:06 -07:00
using UnityEngine.Serialization;
2024-06-19 04:29:32 -07:00
using UnityEngine.UI;
2024-06-25 01:36:44 -07:00
using UnityEngine.Video;
2024-06-27 01:08:10 -07:00
using GadGame.Network;
2024-06-19 04:29:32 -07:00
namespace GadGame.Scripts.Coffee
{
public class CoffeeController : MonoBehaviour
{
2024-06-19 20:25:06 -07:00
[SerializeField] private VoidEvent _engageReadyEvent;
2024-06-21 01:20:01 -07:00
[SerializeField] private StringEvent _generateImageSuccessEvent;
2024-06-19 04:29:32 -07:00
[SerializeField] private BoolEvent _playPassByAnimEvent;
[SerializeField] private BoolEvent _playVideoEvent;
[SerializeField] private FloatEvent _readyCountDownEvent;
2024-06-19 20:25:06 -07:00
[SerializeField] private StringEvent _encodeImageEvent;
2024-06-25 01:36:44 -07:00
[SerializeField] private VideoPlayer _idleVideo;
2024-06-19 20:25:06 -07:00
[SerializeField] private LoadImageEncoded _userImager;
2024-06-19 04:29:32 -07:00
[SerializeField] private Image _process;
2024-06-27 01:08:10 -07:00
[SerializeField] private Image _faceGuide;
2024-06-19 04:29:32 -07:00
[SerializeField] private TextMeshProUGUI _hintText;
2024-06-27 01:08:10 -07:00
[SerializeField] private TextMeshProUGUI _notifyText;
2024-06-24 01:57:54 -07:00
[SerializeField] private GameObject _notify;
[SerializeField] private GameObject _hint;
2024-06-19 04:29:32 -07:00
[SerializeField] private Image _loading;
2024-06-19 20:30:19 -07:00
[SerializeField] private float _loadingSpeed = 100;
2024-06-19 04:29:32 -07:00
[SerializeField] private string[] _texts;
[SerializeField] private string[] _loadingTexts;
private int _indexText;
private bool _isLoading;
private float _timer;
2024-06-25 01:40:52 -07:00
private async void Awake()
2024-06-19 04:29:32 -07:00
{
2024-06-25 01:36:44 -07:00
_idleVideo.targetCameraAlpha = 1;
2024-06-20 04:00:53 -07:00
_loading.transform.DOLocalRotate(new Vector3(0, 0, 360), 10 / _loadingSpeed, RotateMode.FastBeyond360)
.SetLoops(-1)
.SetRelative(true)
.SetEase(Ease.Linear);
2024-06-24 01:57:54 -07:00
_notify.SetActive(false);
_hint.SetActive(false);
2024-06-27 01:08:10 -07:00
_faceGuide.enabled = true;
2024-06-19 04:29:32 -07:00
}
private void OnEnable()
{
2024-06-19 20:25:06 -07:00
_engageReadyEvent.Register(OnEngageReady);
_generateImageSuccessEvent.Register(OnGenerateImageSuccess);
2024-06-19 04:29:32 -07:00
_playPassByAnimEvent.Register(Play);
_playVideoEvent.Register(SetPlayVideo);
_readyCountDownEvent.Register(SetReadyCountDown);
2024-06-19 20:25:06 -07:00
_encodeImageEvent.Register(OnGetEncodeImage);
2024-06-19 04:29:32 -07:00
}
private void OnDisable()
{
2024-06-19 20:25:06 -07:00
_engageReadyEvent.Unregister(OnEngageReady);
_generateImageSuccessEvent.Unregister(OnGenerateImageSuccess);
2024-06-19 04:29:32 -07:00
_playPassByAnimEvent.Unregister(Play);
_playVideoEvent.Unregister(SetPlayVideo);
_readyCountDownEvent.Unregister(SetReadyCountDown);
2024-06-19 20:25:06 -07:00
_encodeImageEvent.Unregister(OnGetEncodeImage);
2024-06-19 04:29:32 -07:00
}
private void Update()
{
if(!_isLoading) return;
_timer += Time.deltaTime;
if (_timer >= 3)
{
_timer = 0;
_indexText++;
if (_indexText > _loadingTexts.Length - 1)
{
_indexText = 0;
}
2024-06-24 01:57:54 -07:00
// _hintText.text = _loadingTexts[_indexText];
2024-06-19 04:29:32 -07:00
}
2024-06-27 01:08:10 -07:00
if (UdpSocket.Instance.DataReceived.AgeMax != 0)
{
_notifyText.text = UdpSocket.Instance.DataReceived.Gender < 0.5 ? "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é";
}
2024-06-19 04:29:32 -07:00
}
private void Play(bool engage) {
// videoPlayer.gameObject.SetActive(!passBy);
// _transform.DOAnchorPosX(engage ? -1000 : 0, 1);
2024-06-24 01:57:54 -07:00
// _hintText.text = _texts[0];
2024-06-19 04:29:32 -07:00
}
2024-06-21 01:20:01 -07:00
private void OnGenerateImageSuccess(string desc)
2024-06-19 04:29:32 -07:00
{
2024-06-21 01:20:01 -07:00
_isLoading = false;
2024-06-25 01:36:44 -07:00
_loading.DOFade(0, 0.5f);
2024-06-24 01:57:54 -07:00
// _hintText.text = desc;
2024-06-27 01:08:10 -07:00
// _notify.SetActive(false);
2024-06-24 01:57:54 -07:00
_hint.SetActive(false);
2024-06-27 01:08:10 -07:00
_notifyText.text = UdpSocket.Instance.DataReceived.Description;
2024-06-19 04:29:32 -07:00
}
2024-06-21 01:20:01 -07:00
private void OnGetEncodeImage(string filePath)
2024-06-19 20:25:06 -07:00
{
2024-06-21 01:20:01 -07:00
_userImager.LoadImage(filePath);
2024-06-24 01:57:54 -07:00
_notify.SetActive(true);
2024-06-19 20:25:06 -07:00
}
2024-06-19 04:29:32 -07:00
private void OnEngageReady()
{
_process.fillAmount = 0;
_isLoading = true;
2024-06-24 01:57:54 -07:00
// _hintText.text = _loadingTexts[_indexText];
2024-06-19 04:29:32 -07:00
_loading.DOFade(1, 1f);
2024-06-24 01:57:54 -07:00
_hint.SetActive(true);
2024-06-27 01:08:10 -07:00
// _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");
2024-06-19 04:29:32 -07:00
}
private async void SetPlayVideo(bool value){
if(value) {
2024-06-25 01:36:44 -07:00
while (_idleVideo.targetCameraAlpha < 1)
2024-06-19 04:29:32 -07:00
{
2024-06-25 01:36:44 -07:00
_idleVideo.targetCameraAlpha += Time.deltaTime * 3;
2024-06-19 04:29:32 -07:00
await UniTask.Yield();
2024-06-27 01:08:10 -07:00
if(_idleVideo == null){
return;
}
2024-06-19 04:29:32 -07:00
}
2024-06-25 01:36:44 -07:00
_idleVideo.targetCameraAlpha = 1;
2024-06-19 04:29:32 -07:00
} else {
2024-06-25 01:36:44 -07:00
while (_idleVideo.targetCameraAlpha > 0)
2024-06-19 04:29:32 -07:00
{
2024-06-27 01:08:10 -07:00
if(_idleVideo == null){
return;
}
2024-06-25 01:36:44 -07:00
_idleVideo.targetCameraAlpha -= Time.deltaTime * 3;
2024-06-19 04:29:32 -07:00
await UniTask.Yield();
2024-06-27 01:08:10 -07:00
if(_idleVideo == null){
return;
}
2024-06-19 04:29:32 -07:00
}
2024-06-25 01:36:44 -07:00
_idleVideo.targetCameraAlpha = 0;
2024-06-19 04:29:32 -07:00
}
}
private void SetReadyCountDown(float progress){
2024-06-24 01:57:54 -07:00
// _hintText.text = _texts[1];
2024-06-25 01:40:52 -07:00
_process.fillAmount = 1 - progress ;
2024-06-19 04:29:32 -07:00
}
}
}