Roses/Assets/Scripts/View/UI/ExpressionInput.cs
2024-04-19 00:10:29 +02:00

87 lines
No EOL
2.2 KiB
C#

using Domain;
using Domain.Input;
using FMODUnity;
using Presenter;
using TMPro;
using UnityEngine;
using UnityEngine.Serialization;
using Animation = FramedAnimator.Animation;
using Animator = FramedAnimator.Animator;
namespace View.UI {
public class ExpressionInput : MonoBehaviour, IExpressionInput, IRoseGrow {
[SerializeField] private TMP_Text text;
private ExpressionClick _click;
private CustomInput _customInput;
private Score _score;
private bool _firstUpdate = true;
[SerializeField] private Animator animator;
[SerializeField] private Animation growAnimation;
[SerializeField] private Animation endAnimation;
[SerializeField] private EventReference growEvent;
[SerializeField] private EventReference gloomEvent;
private void Start() {
_click = FindObjectOfType<Dependencies>().ExpressionClick;
_customInput = FindObjectOfType<Dependencies>().CustomInput;
_score = FindObjectOfType<Dependencies>().Score;
animator.OnAnimationEnd += AnimationEnded;
}
private void AnimationEnded(string animationName) {
if (animationName == "Rosa_Grow") {
animator.ChangeAnimation(endAnimation);
RuntimeManager.PlayOneShot(gloomEvent);
animator.PlayUntil(1f);
return;
}
if (animationName == "Rosa_End") {
animator.ChangeAnimation(growAnimation);
animator.PlayUntil(_score.GrowPercentage);
}
}
private void Update() {
CheckInput();
}
public void UpdateView(int score, float growPercentage) {
if (animator.CurrentAnimation == "Rosa_Grow")
animator.PlayUntil(IsLastGrowState(score, growPercentage) ? 1f : growPercentage);
text.text = score.ToString();
_firstUpdate = false;
}
public void Grow() { }
public void GrowStep() {
if (animator.CurrentAnimation == "Rosa_Grow" && _score.GrowPercentage < 1f) {
RuntimeManager.PlayOneShot(growEvent);
}
}
private bool IsLastGrowState(int score, float growPercentage) {
if (growPercentage != 0)
return false;
bool isLastFrame = score % (_score.GrowIterations * _score.SpawnRate) == 0;
if (!isLastFrame)
return false;
if (_firstUpdate)
return false;
return true;
}
private void CheckInput() {
_customInput.UpdateInput();
if (_customInput.AnyKeyDown())
_click.Execute();
}
}
}