using System; using CIGEnums; using UnityEngine; namespace CIG { public abstract class AnimatedSpriteBase : MonoBehaviour { private void Start() { if (this._sprites != null && this._playOnStart) { this.Play(); } } private void Update() { if (this._isPlaying) { this.Animate(Time.unscaledDeltaTime); } } public void ReplaceSprites(Sprite[] sprites) { this._sprites = sprites; this.UpdateSprite(sprites[this._spriteIndex]); } public void Play() { this._isPlaying = true; this.Animate(0f); } public void Pause() { this._isPlaying = false; } public void Stop() { this._isPlaying = false; this.Reset(); } public void Reset() { this._currentTime = 0f; this._spriteIndex = 0; } public float FPS { get { return this._framesPerSecond; } } public Sprite[] Sprites { get { return this._sprites; } } public bool PlayOnStart { get { return this._playOnStart; } } public AnimationMode AnimationMode { get { return this._animationMode; } } public float WaitAtEndSeconds { get { return this._waitAtEndSeconds; } } public bool IsPlaying { get { return this._isPlaying; } } protected abstract void UpdateSprite(Sprite sprite); private void Animate(float deltaTime) { if (this._sprites != null && this._sprites.Length > 0) { float num = 1f / this._framesPerSecond; this._currentTime += deltaTime; float num2 = num * (float)this._sprites.Length + this._waitAtEndSeconds / 2f; if (this._animationMode == AnimationMode.Loop) { this._spriteIndex = (int)(this._currentTime % num2 / num); } else if (this._animationMode == AnimationMode.PingPong) { this._spriteIndex = (int)(Mathf.PingPong(this._currentTime, num2) / num - this._waitAtEndSeconds / 4f / num); } else if (this._animationMode == AnimationMode.Random) { if (this._currentTime >= num) { this._spriteIndex = UnityEngine.Random.Range(0, this._sprites.Length); this._currentTime = 0f; } } else if (this._animationMode == AnimationMode.RandomNextPrev) { if (this._currentTime >= num) { this._spriteIndex += UnityEngine.Random.Range(0, 2) * 2 - 1; this._currentTime = 0f; } } else if (this._animationMode == AnimationMode.Single) { this._spriteIndex = (int)(this._currentTime / num); if (this._currentTime >= num2) { this._currentTime = 0f; this.Pause(); } } this._spriteIndex = Mathf.Clamp(this._spriteIndex, 0, this._sprites.Length - 1); this.UpdateSprite(this._sprites[this._spriteIndex]); } else { UnityEngine.Debug.LogWarningFormat("[AnimatedSprite] Missing Sprites for '{0}'", new object[] { base.name }); } } [SerializeField] protected float _framesPerSecond = 12f; [SerializeField] protected Sprite[] _sprites; [SerializeField] protected bool _playOnStart = true; [SerializeField] protected AnimationMode _animationMode; [SerializeField] protected float _waitAtEndSeconds; private int _spriteIndex; private bool _isPlaying; private float _currentTime; } }