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
50
Assets/Scripts/FramedAnimator/View/Animator.cs
Normal file
50
Assets/Scripts/FramedAnimator/View/Animator.cs
Normal file
|
@ -0,0 +1,50 @@
|
|||
using System;
|
||||
using FramedAnimator.Domain;
|
||||
using UnityEngine;
|
||||
|
||||
namespace FramedAnimator {
|
||||
[RequireComponent(typeof(SpriteRenderer))]
|
||||
public class Animator : MonoBehaviour {
|
||||
[SerializeField] private new Animation animation;
|
||||
public string CurrentAnimation => animation.name;
|
||||
private SpriteRenderer _renderer;
|
||||
|
||||
public event Action<string> OnAnimationEnd;
|
||||
private AnimatorModel _model;
|
||||
|
||||
private void Awake() {
|
||||
_renderer = GetComponent<SpriteRenderer>();
|
||||
_model = new AnimatorModel(animation.FrameRate, animation.FrameCount);
|
||||
}
|
||||
|
||||
private void Update() {
|
||||
if (_model.RenderingFrame >= _model.FrameCount - 1)
|
||||
TryCallAnimationEnd();
|
||||
else
|
||||
UpdateAnimationFrame();
|
||||
}
|
||||
|
||||
private void UpdateAnimationFrame() {
|
||||
_model.UpdateAnimationFrame(Time.deltaTime);
|
||||
_renderer.sprite = animation.GetFrame(_model.RenderingFrame);
|
||||
}
|
||||
|
||||
private void TryCallAnimationEnd() {
|
||||
if(_model.AnimationEnded(Time.deltaTime))
|
||||
OnAnimationEnd?.Invoke(animation.name);
|
||||
}
|
||||
|
||||
public void ChangeAnimation(Animation anim) {
|
||||
_model = new AnimatorModel(anim.FrameRate, anim.FrameCount);
|
||||
animation = anim;
|
||||
_renderer.sprite = animation.GetFrame(0);
|
||||
}
|
||||
|
||||
public void PlayUntil(float fraction) {
|
||||
if (fraction >= 1f)
|
||||
_model.PlayToEnd = true;
|
||||
else
|
||||
_model.PlayUntil(fraction);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue