fix: animator not locking itself if changing percentage before ending
This commit is contained in:
parent
2b17041d49
commit
9d5f64fd19
17 changed files with 138 additions and 75 deletions
51
Assets/Scripts/FramedAnimator/Domain/AnimatorModel.cs
Normal file
51
Assets/Scripts/FramedAnimator/Domain/AnimatorModel.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: e11aeabcdf97474b9a739f0f42fc8f75
|
||||
timeCreated: 1713449229
|
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"name": "FramedAnimator.Domain"
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 8b3a255fd5084af4883574b9fd6a4b0e
|
||||
timeCreated: 1713449089
|
Loading…
Add table
Add a link
Reference in a new issue