BaseSpriteItemData.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. namespace Funly.SkyStudio
  6. {
  7. // Data for representing an instanced sprite sheet object.
  8. public class BaseSpriteItemData : System.Object
  9. {
  10. // Tracking state for the splash effect.
  11. public enum SpriteState
  12. {
  13. Unknown,
  14. NotStarted,
  15. Animating,
  16. Complete
  17. }
  18. public SpriteSheetData spriteSheetData;
  19. public Matrix4x4 modelMatrix { get; protected set; }
  20. public SpriteState state { get; protected set; }
  21. public Vector3 spritePosition { get; set; }
  22. public float startTime { get; protected set; }
  23. public float endTime { get; protected set; }
  24. public float delay;
  25. public BaseSpriteItemData()
  26. {
  27. state = SpriteState.NotStarted;
  28. }
  29. public void SetTRSMatrix(Vector3 worldPosition, Quaternion rotation, Vector3 scale)
  30. {
  31. spritePosition = worldPosition;
  32. modelMatrix = Matrix4x4.TRS(
  33. worldPosition,
  34. rotation,
  35. scale);
  36. }
  37. public void Start()
  38. {
  39. state = SpriteState.Animating;
  40. // Schedule the start/end time of this sprite sheet animation in the GPU.
  41. startTime = BaseSpriteItemData.CalculateStartTimeWithDelay(delay);
  42. endTime = BaseSpriteItemData.CalculateEndTime(startTime,
  43. spriteSheetData.frameCount,
  44. spriteSheetData.frameRate);
  45. }
  46. public void Continue()
  47. {
  48. if (state != SpriteState.Animating) {
  49. return;
  50. }
  51. if (Time.time > endTime) {
  52. state = SpriteState.Complete;
  53. return;
  54. }
  55. }
  56. public void Reset()
  57. {
  58. state = SpriteState.NotStarted;
  59. startTime = -1.0f;
  60. endTime = -1.0f;
  61. }
  62. public static float CalculateStartTimeWithDelay(float delay)
  63. {
  64. return Time.time + delay;
  65. }
  66. public static float CalculateEndTime(float startTime, int itemCount, int animationSpeed)
  67. {
  68. float singleFrameDuration = 1.0f / (float)animationSpeed;
  69. float duration = (float)itemCount * singleFrameDuration;
  70. return startTime + duration;
  71. }
  72. /*
  73. int GetSpriteTargetIndex(int itemCount, int animationSpeed, float seed)
  74. {
  75. float startTime = m_StartTime + startDelay;
  76. float delta = Time.time - startTime;
  77. float timePerFrame = 1.0f / animationSpeed;
  78. int frameIndex = (int)(delta / timePerFrame);
  79. return frameIndex;
  80. }
  81. */
  82. }
  83. }