refactor: moved grow animation to its own class

This commit is contained in:
Gerard Gascón 2024-04-19 11:26:44 +02:00
parent 150b602dc3
commit 779af276d3
14 changed files with 224 additions and 116 deletions

View file

@ -0,0 +1,62 @@
using System;
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 EventReference growEvent;
[SerializeField] private EventReference gloomEvent;
private Score _score;
private bool _firstUpdate;
private void Start() {
_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);
}
}
public void GrowStep() {
if (animator.CurrentAnimation == "Rosa_Grow") {
animator.PlayUntil(IsLastGrowState(_score.Value, _score.GrowPercentage) ? 1f : _score.GrowPercentage);
if (_score.GrowPercentage < 1f)
RuntimeManager.PlayOneShot(growEvent);
}
_firstUpdate = false;
}
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;
}
}
}