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

@ -1,21 +0,0 @@
using Presenter;
namespace View.Collections {
public class RoseGrowCollection : IRoseGrow {
private readonly IRoseGrow[] _grows;
public RoseGrowCollection(IRoseGrow[] grows) {
_grows = grows;
}
public void Grow() {
foreach (IRoseGrow grow in _grows)
grow.Grow();
}
public void GrowStep() {
foreach (IRoseGrow grow in _grows)
grow.GrowStep();
}
}
}

View file

@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: 6f517298528d4766a83770b1867f905f
timeCreated: 1713475808

View file

@ -25,17 +25,16 @@ namespace View {
IRoseSpawner spawner = FindObjectOfType<RoseSpawner>();
IRoseGrow grow = FindObjectOfType<GrowParticlesSpawner>();
IRoseGrow inputGrow = FindObjectOfType<ExpressionInput>();
IRoseGrow growCollection = new RoseGrowCollection(new[] { grow, inputGrow });
IInputCallback growParticles = FindObjectOfType<GrowParticlesSpawner>();
IRoseGrow growAnimation = FindObjectOfType<GrowAnimation>();
ExpressionClick = new ExpressionClick(Score, inputCollections, spawner, growCollection);
ExpressionClick = new ExpressionClick(Score, inputCollections, spawner, growAnimation, growParticles);
CustomInput = new CustomInput();
PlayerPrefsRepository repository = new();
Saver = new SaveGame(repository, Score);
Loader = new LoadGame(repository, Score, inputCollections, spawner);
Loader = new LoadGame(repository, Score, inputCollections, spawner, growAnimation);
}
}
}

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;
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: a0d2d820274a46d68d83fd6ddef1c431
timeCreated: 1713516930

View file

@ -4,18 +4,16 @@ using UnityEngine;
using Random = UnityEngine.Random;
namespace View.Scene {
public class GrowParticlesSpawner : MonoBehaviour, IRoseGrow {
public class GrowParticlesSpawner : MonoBehaviour, IInputCallback {
[SerializeField] private GrowParticle growParticle;
[SerializeField] private Transform growParticlePositions;
[SerializeField, Range(0, 180)] private float angleRange;
public void Grow() {
public void OnInputReceived() {
float randomRotation = Random.Range(-angleRange / 2f, angleRange / 2f);
Instantiate(growParticle, growParticlePositions.position, Quaternion.Euler(0f, 0f, randomRotation));
}
public void GrowStep() { }
private void OnDrawGizmosSelected() {
Gizmos.color = Color.red;
float angle = angleRange / 2f + 90f;

View file

@ -1,81 +1,25 @@
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 {
public class ExpressionInput : MonoBehaviour, IExpressionInput {
[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() {
private void Update() =>
CheckInput();
}
public void UpdateView(int score, float growPercentage) {
if (animator.CurrentAnimation == "Rosa_Grow")
animator.PlayUntil(IsLastGrowState(score, growPercentage) ? 1f : growPercentage);
public void UpdateView(int score, float 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();