fix: animator not locking itself if changing percentage before ending

This commit is contained in:
Gerard Gascón 2024-04-18 16:59:01 +02:00
parent 2b17041d49
commit 9d5f64fd19
17 changed files with 138 additions and 75 deletions

View file

@ -0,0 +1,51 @@
using UnityEngine;
namespace FramedAnimator.Domain {
public class AnimatorModel {
public int FrameCount { private set; get; }
public float FrameRate { private set; get; }
public int Limit { private set; get; }
public int RenderingFrame { private set; get; }
private float _currentFrame;
private bool _animationEnded;
public bool PlayToEnd { set; get; }
public AnimatorModel(float frameRate, int frameCount) {
_animationEnded = false;
Limit = RenderingFrame = 0;
_currentFrame = 0f;
FrameRate = frameRate;
FrameCount = frameCount;
}
public void PlayUntil(float fraction) {
Limit = Mathf.RoundToInt(FrameCount * Mathf.Clamp01(fraction));
Limit = Mathf.Clamp(Limit, 0, FrameCount - 1);
}
public void UpdateAnimationFrame(float deltaTime) {
if (RenderingFrame >= Limit && !PlayToEnd)
return;
_currentFrame += FrameRate * deltaTime;
RenderingFrame = Mathf.Clamp(Mathf.FloorToInt(_currentFrame), 0, PlayToEnd ? FrameCount - 1: Limit);
}
public bool AnimationEnded(float deltaTime) {
if (RenderingFrame < FrameCount - 1 || _animationEnded) return false;
_currentFrame += deltaTime * FrameRate;
RenderingFrame = Mathf.FloorToInt(_currentFrame);
if (RenderingFrame <= Limit) return false;
_animationEnded = true;
return true;
}
}
}