Roses/Assets/Scripts/View/Scene/GrowAnimation.cs
2024-04-22 20:00:29 +02:00

86 lines
No EOL
2.2 KiB
C#

using System;
using System.Collections;
using Domain;
using FMODUnity;
using Presenter;
using UnityEngine;
using Animation = FramedAnimator.Animation;
using Animator = FramedAnimator.Animator;
namespace View.Scene {
public class GrowAnimation : MonoBehaviour, IRoseGrow {
[SerializeField] private Animator animator;
[SerializeField] private Animation growAnimation;
[SerializeField] private Animation endAnimation;
[SerializeField] private Animation startAnimation;
[SerializeField] private EventReference growEvent;
[SerializeField] private EventReference gloomEvent;
private Score _score;
private SpawnRose _spawnRose;
private bool _firstUpdate = true;
public bool Growing { private set; get; }
private void Start() {
_score = FindObjectOfType<Dependencies>().Score;
_spawnRose = FindObjectOfType<Dependencies>().Spawner;
animator.PlayUntil(1f);
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") {
_spawnRose.Execute();
StartCoroutine(RestartCycle());
}
if (animationName == "Rosa_Start") {
Growing = true;
animator.ChangeAnimation(growAnimation);
animator.PlayUntil(_score.GrowPercentage);
}
}
IEnumerator RestartCycle() {
animator.Renderer.enabled = false;
yield return new WaitForSeconds(2f);
animator.Renderer.enabled = true;
animator.ChangeAnimation(startAnimation);
animator.PlayUntil(1f);
}
public void GrowStep() {
if (animator.CurrentAnimation == "Rosa_Grow") {
if (IsLastGrowState())
Growing = false;
animator.PlayUntil(IsLastGrowState() ? 1f : _score.GrowPercentage);
if (_score.GrowPercentage < 1f && !_firstUpdate)
RuntimeManager.PlayOneShot(growEvent);
}
_firstUpdate = false;
}
public bool IsLastGrowState() {
if (_score.GrowPercentage != 0)
return false;
bool isLastFrame = _score.Value % (_score.GrowIterations * _score.SpawnRate) == 0;
if (!isLastFrame)
return false;
if (_firstUpdate)
return false;
return true;
}
}
}