Commit 9d5f64fd authored by Gerard Gascón's avatar Gerard Gascón
Browse files

fix: animator not locking itself if changing percentage before ending

parent 2b17041d
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
fileFormatVersion: 2
guid: 2c2047b3315144708938e7d2d9435cb6
timeCreated: 1713449063
 No newline at end of file
+51 −0
Original line number Diff line number Diff line
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;
		}
	}
}
 No newline at end of file
+3 −0
Original line number Diff line number Diff line
fileFormatVersion: 2
guid: e11aeabcdf97474b9a739f0f42fc8f75
timeCreated: 1713449229
 No newline at end of file
+3 −0
Original line number Diff line number Diff line
{
  "name": "FramedAnimator.Domain"
}
 No newline at end of file
+3 −0
Original line number Diff line number Diff line
fileFormatVersion: 2
guid: 8b3a255fd5084af4883574b9fd6a4b0e
timeCreated: 1713449089
 No newline at end of file
Loading