fix: grow animation spawning at correct positions

This commit is contained in:
Gerard Gascón 2024-04-22 11:49:01 +02:00
parent a06779c5c1
commit f0af2a5a07
3 changed files with 19 additions and 5 deletions

View file

@ -2458,6 +2458,7 @@ MonoBehaviour:
- {fileID: 6383810576328763588} - {fileID: 6383810576328763588}
- {fileID: 8413760021314526160} - {fileID: 8413760021314526160}
- {fileID: 3176123164317376148} - {fileID: 3176123164317376148}
growAnimation: {fileID: 902566665}
--- !u!4 &1465666065 --- !u!4 &1465666065
Transform: Transform:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

View file

@ -19,6 +19,8 @@ namespace View.Scene {
private SpawnRose _spawnRose; private SpawnRose _spawnRose;
private bool _firstUpdate = true; private bool _firstUpdate = true;
public bool Growing { private set; get; } = true;
private void Start() { private void Start() {
_score = FindObjectOfType<Dependencies>().Score; _score = FindObjectOfType<Dependencies>().Score;
_spawnRose = FindObjectOfType<Dependencies>().Spawner; _spawnRose = FindObjectOfType<Dependencies>().Spawner;
@ -35,6 +37,7 @@ namespace View.Scene {
} }
if (animationName == "Rosa_End") { if (animationName == "Rosa_End") {
Growing = true;
_spawnRose.Execute(); _spawnRose.Execute();
animator.ChangeAnimation(growAnimation); animator.ChangeAnimation(growAnimation);
animator.PlayUntil(_score.GrowPercentage); animator.PlayUntil(_score.GrowPercentage);
@ -43,17 +46,19 @@ namespace View.Scene {
public void GrowStep() { public void GrowStep() {
if (animator.CurrentAnimation == "Rosa_Grow") { if (animator.CurrentAnimation == "Rosa_Grow") {
animator.PlayUntil(IsLastGrowState(_score.Value, _score.GrowPercentage) ? 1f : _score.GrowPercentage); if (IsLastGrowState())
Growing = false;
animator.PlayUntil(IsLastGrowState() ? 1f : _score.GrowPercentage);
if (_score.GrowPercentage < 1f && !_firstUpdate) if (_score.GrowPercentage < 1f && !_firstUpdate)
RuntimeManager.PlayOneShot(growEvent); RuntimeManager.PlayOneShot(growEvent);
} }
_firstUpdate = false; _firstUpdate = false;
} }
private bool IsLastGrowState(int score, float growPercentage) { public bool IsLastGrowState() {
if (growPercentage != 0) if (_score.GrowPercentage != 0)
return false; return false;
bool isLastFrame = score % (_score.GrowIterations * _score.SpawnRate) == 0; bool isLastFrame = _score.Value % (_score.GrowIterations * _score.SpawnRate) == 0;
if (!isLastFrame) if (!isLastFrame)
return false; return false;
if (_firstUpdate) if (_firstUpdate)

View file

@ -10,6 +10,8 @@ namespace View.Scene {
[SerializeField] private GrowParticle growParticle; [SerializeField] private GrowParticle growParticle;
[SerializeField] private Transform[] growParticlePositions; [SerializeField] private Transform[] growParticlePositions;
[SerializeField] private GrowAnimation growAnimation;
private Score _score; private Score _score;
private void Awake() { private void Awake() {
@ -22,11 +24,17 @@ namespace View.Scene {
public void OnInputReceived() { public void OnInputReceived() {
float randomRotation = Random.Range(0, 360); float randomRotation = Random.Range(0, 360);
if(growAnimation.Growing)
growParticle.Spawn(GetGrowParticlePosition(), Quaternion.Euler(0, 0, randomRotation)); growParticle.Spawn(GetGrowParticlePosition(), Quaternion.Euler(0, 0, randomRotation));
} }
private Vector3 GetGrowParticlePosition() { private Vector3 GetGrowParticlePosition() {
float iterationStep = 1f / _score.SpawnRate; float iterationStep = 1f / _score.SpawnRate;
bool isLastGrowStep = _score.Value % (_score.GrowIterations * _score.SpawnRate) == 0;
if(isLastGrowStep)
return growParticlePositions[^1].position;
for (int i = 0; i < growParticlePositions.Length; i++) { for (int i = 0; i < growParticlePositions.Length; i++) {
if (_score.GrowPercentage < (i + 1) * iterationStep) if (_score.GrowPercentage < (i + 1) * iterationStep)
return growParticlePositions[i].position; return growParticlePositions[i].position;