You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

178 lines
3.4 KiB

  1. using System;
  2. using CIGEnums;
  3. using UnityEngine;
  4. namespace CIG
  5. {
  6. public abstract class AnimatedSpriteBase : MonoBehaviour
  7. {
  8. private void Start()
  9. {
  10. if (this._sprites != null && this._playOnStart)
  11. {
  12. this.Play();
  13. }
  14. }
  15. private void Update()
  16. {
  17. if (this._isPlaying)
  18. {
  19. this.Animate(Time.unscaledDeltaTime);
  20. }
  21. }
  22. public void ReplaceSprites(Sprite[] sprites)
  23. {
  24. this._sprites = sprites;
  25. this.UpdateSprite(sprites[this._spriteIndex]);
  26. }
  27. public void Play()
  28. {
  29. this._isPlaying = true;
  30. this.Animate(0f);
  31. }
  32. public void Pause()
  33. {
  34. this._isPlaying = false;
  35. }
  36. public void Stop()
  37. {
  38. this._isPlaying = false;
  39. this.Reset();
  40. }
  41. public void Reset()
  42. {
  43. this._currentTime = 0f;
  44. this._spriteIndex = 0;
  45. }
  46. public float FPS
  47. {
  48. get
  49. {
  50. return this._framesPerSecond;
  51. }
  52. }
  53. public Sprite[] Sprites
  54. {
  55. get
  56. {
  57. return this._sprites;
  58. }
  59. }
  60. public bool PlayOnStart
  61. {
  62. get
  63. {
  64. return this._playOnStart;
  65. }
  66. }
  67. public AnimationMode AnimationMode
  68. {
  69. get
  70. {
  71. return this._animationMode;
  72. }
  73. }
  74. public float WaitAtEndSeconds
  75. {
  76. get
  77. {
  78. return this._waitAtEndSeconds;
  79. }
  80. }
  81. public bool IsPlaying
  82. {
  83. get
  84. {
  85. return this._isPlaying;
  86. }
  87. }
  88. protected abstract void UpdateSprite(Sprite sprite);
  89. private void Animate(float deltaTime)
  90. {
  91. if (this._sprites != null && this._sprites.Length > 0)
  92. {
  93. float num = 1f / this._framesPerSecond;
  94. this._currentTime += deltaTime;
  95. float num2 = num * (float)this._sprites.Length + this._waitAtEndSeconds / 2f;
  96. if (this._animationMode == AnimationMode.Loop)
  97. {
  98. this._spriteIndex = (int)(this._currentTime % num2 / num);
  99. }
  100. else if (this._animationMode == AnimationMode.PingPong)
  101. {
  102. this._spriteIndex = (int)(Mathf.PingPong(this._currentTime, num2) / num - this._waitAtEndSeconds / 4f / num);
  103. }
  104. else if (this._animationMode == AnimationMode.Random)
  105. {
  106. if (this._currentTime >= num)
  107. {
  108. this._spriteIndex = UnityEngine.Random.Range(0, this._sprites.Length);
  109. this._currentTime = 0f;
  110. }
  111. }
  112. else if (this._animationMode == AnimationMode.RandomNextPrev)
  113. {
  114. if (this._currentTime >= num)
  115. {
  116. this._spriteIndex += UnityEngine.Random.Range(0, 2) * 2 - 1;
  117. this._currentTime = 0f;
  118. }
  119. }
  120. else if (this._animationMode == AnimationMode.Single)
  121. {
  122. this._spriteIndex = (int)(this._currentTime / num);
  123. if (this._currentTime >= num2)
  124. {
  125. this._currentTime = 0f;
  126. this.Pause();
  127. }
  128. }
  129. this._spriteIndex = Mathf.Clamp(this._spriteIndex, 0, this._sprites.Length - 1);
  130. this.UpdateSprite(this._sprites[this._spriteIndex]);
  131. }
  132. else
  133. {
  134. UnityEngine.Debug.LogWarningFormat("[AnimatedSprite] Missing Sprites for '{0}'", new object[]
  135. {
  136. base.name
  137. });
  138. }
  139. }
  140. [SerializeField]
  141. protected float _framesPerSecond = 12f;
  142. [SerializeField]
  143. protected Sprite[] _sprites;
  144. [SerializeField]
  145. protected bool _playOnStart = true;
  146. [SerializeField]
  147. protected AnimationMode _animationMode;
  148. [SerializeField]
  149. protected float _waitAtEndSeconds;
  150. private int _spriteIndex;
  151. private bool _isPlaying;
  152. private float _currentTime;
  153. }
  154. }