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,14 @@
using UnityEngine;
namespace FramedAnimator {
[CreateAssetMenu(menuName = "Framed Animation")]
public class Animation : ScriptableObject {
[SerializeField] private Sprite[] sprites;
[SerializeField] private float frameRate;
public Sprite GetFrame(int frame) => sprites[frame];
public int FrameCount => sprites.Length;
public float FrameRate => frameRate;
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 7e87fbf7a73a4ef496960eb0469f7e88
timeCreated: 1713341980

View 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);
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 944498bf314d45ceb21a77b6ec4b0cfb
timeCreated: 1713199370

View file

@ -0,0 +1,16 @@
{
"name": "FramedAnimator.View",
"rootNamespace": "",
"references": [
"GUID:8b3a255fd5084af4883574b9fd6a4b0e"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: a8b14a26d6924d6bb65739ca56ae8187
timeCreated: 1713449102