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
|
@ -1,62 +0,0 @@
|
||||||
using System;
|
|
||||||
using UnityEngine;
|
|
||||||
|
|
||||||
namespace FramedAnimator {
|
|
||||||
[RequireComponent(typeof(SpriteRenderer))]
|
|
||||||
public class Animator : MonoBehaviour {
|
|
||||||
[SerializeField] private new Animation animation;
|
|
||||||
public string CurrentAnimation => animation.name;
|
|
||||||
|
|
||||||
private float _currentFrame;
|
|
||||||
private int _renderingFrame;
|
|
||||||
private int _limit;
|
|
||||||
|
|
||||||
private SpriteRenderer _renderer;
|
|
||||||
|
|
||||||
private bool _animationEnded;
|
|
||||||
public event Action<string> OnAnimationEnd;
|
|
||||||
|
|
||||||
private void Awake() {
|
|
||||||
_renderer = GetComponent<SpriteRenderer>();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void Update() {
|
|
||||||
if (_renderingFrame >= _limit) {
|
|
||||||
TryCallAnimationEnd();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
UpdateAnimationFrame();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void UpdateAnimationFrame() {
|
|
||||||
_currentFrame += Time.deltaTime * animation.FrameRate;
|
|
||||||
_renderingFrame = Mathf.Clamp(Mathf.FloorToInt(_currentFrame), 0, _limit);
|
|
||||||
_renderer.sprite = animation.GetFrame(_renderingFrame);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void TryCallAnimationEnd() {
|
|
||||||
if (_renderingFrame < animation.FrameCount - 1 || _animationEnded) return;
|
|
||||||
|
|
||||||
_currentFrame += Time.deltaTime * animation.FrameRate;
|
|
||||||
_renderingFrame = Mathf.FloorToInt(_currentFrame);
|
|
||||||
|
|
||||||
if (_renderingFrame <= _limit) return;
|
|
||||||
_animationEnded = true;
|
|
||||||
OnAnimationEnd?.Invoke(animation.name);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ChangeAnimation(Animation anim) {
|
|
||||||
_animationEnded = false;
|
|
||||||
_limit = _renderingFrame = 0;
|
|
||||||
_currentFrame = 0f;
|
|
||||||
animation = anim;
|
|
||||||
|
|
||||||
_renderer.sprite = animation.GetFrame(_renderingFrame);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void PlayUntil(float fraction) {
|
|
||||||
_limit = Mathf.RoundToInt(animation.FrameCount * Mathf.Clamp01(fraction));
|
|
||||||
_limit = Mathf.Clamp(_limit, 0, animation.FrameCount - 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
3
Assets/Scripts/FramedAnimator/Domain.meta
Normal file
3
Assets/Scripts/FramedAnimator/Domain.meta
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 2c2047b3315144708938e7d2d9435cb6
|
||||||
|
timeCreated: 1713449063
|
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
|
|
@ -1,3 +0,0 @@
|
||||||
{
|
|
||||||
"name": "SantJordi.FramedAnimator"
|
|
||||||
}
|
|
|
@ -1,7 +0,0 @@
|
||||||
fileFormatVersion: 2
|
|
||||||
guid: 246088dd927065946b1746223fef2142
|
|
||||||
AssemblyDefinitionImporter:
|
|
||||||
externalObjects: {}
|
|
||||||
userData:
|
|
||||||
assetBundleName:
|
|
||||||
assetBundleVariant:
|
|
3
Assets/Scripts/FramedAnimator/View.meta
Normal file
3
Assets/Scripts/FramedAnimator/View.meta
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 6f6b70f0fef849dc8986329ff2172b4b
|
||||||
|
timeCreated: 1713449069
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,16 @@
|
||||||
|
{
|
||||||
|
"name": "FramedAnimator.View",
|
||||||
|
"rootNamespace": "",
|
||||||
|
"references": [
|
||||||
|
"GUID:8b3a255fd5084af4883574b9fd6a4b0e"
|
||||||
|
],
|
||||||
|
"includePlatforms": [],
|
||||||
|
"excludePlatforms": [],
|
||||||
|
"allowUnsafeCode": false,
|
||||||
|
"overrideReferences": false,
|
||||||
|
"precompiledReferences": [],
|
||||||
|
"autoReferenced": true,
|
||||||
|
"defineConstraints": [],
|
||||||
|
"versionDefines": [],
|
||||||
|
"noEngineReferences": false
|
||||||
|
}
|
|
@ -0,0 +1,3 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: a8b14a26d6924d6bb65739ca56ae8187
|
||||||
|
timeCreated: 1713449102
|
|
@ -15,7 +15,7 @@ namespace View {
|
||||||
public LoadGame Loader { private set; get; }
|
public LoadGame Loader { private set; get; }
|
||||||
|
|
||||||
private void Awake() {
|
private void Awake() {
|
||||||
Score = new Score(20, 5);
|
Score = new Score(10, 10);
|
||||||
|
|
||||||
IExpressionInput input = FindObjectOfType<ExpressionInput>();
|
IExpressionInput input = FindObjectOfType<ExpressionInput>();
|
||||||
IExpressionInput visibility = FindObjectOfType<UIVisibility>();
|
IExpressionInput visibility = FindObjectOfType<UIVisibility>();
|
||||||
|
|
|
@ -7,8 +7,8 @@
|
||||||
"GUID:6055be8ebefd69e48b49212b09b47b2f",
|
"GUID:6055be8ebefd69e48b49212b09b47b2f",
|
||||||
"GUID:1220ccfff01d26041a9bb8cd7ae584af",
|
"GUID:1220ccfff01d26041a9bb8cd7ae584af",
|
||||||
"GUID:58f2f98b0cec4e74998cb65ad59190b4",
|
"GUID:58f2f98b0cec4e74998cb65ad59190b4",
|
||||||
"GUID:246088dd927065946b1746223fef2142",
|
"GUID:0c752da273b17c547ae705acf0f2adf2",
|
||||||
"GUID:0c752da273b17c547ae705acf0f2adf2"
|
"GUID:a8b14a26d6924d6bb65739ca56ae8187"
|
||||||
],
|
],
|
||||||
"includePlatforms": [],
|
"includePlatforms": [],
|
||||||
"excludePlatforms": [],
|
"excludePlatforms": [],
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue