51 lines
No EOL
1.3 KiB
C#
51 lines
No EOL
1.3 KiB
C#
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;
|
|
}
|
|
}
|
|
} |