CreditsController.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using Sirenix.OdinInspector;
  6. using KairoEngine.Core;
  7. using KairoEngine.Core.GameActions;
  8. namespace KairoEngine.UI
  9. {
  10. [HideMonoScript]
  11. public class CreditsController : MonoBehaviour
  12. {
  13. public RectTransform textTransform;
  14. public bool rollCreditsOnStart = true;
  15. public bool listenForRestartEvent = true;
  16. public string restartEventName = "RestartRollingCredits";
  17. [BoxGroup("Rolling Credits")] public KeyCode cancelInput = KeyCode.Escape;
  18. [BoxGroup("Rolling Credits")] public KeyCode fastForwardInput = KeyCode.DownArrow;
  19. [BoxGroup("Rolling Credits")] public KeyCode revereInput = KeyCode.UpArrow;
  20. [BoxGroup("Rolling Credits")] public float normalSpeed = 10f;
  21. [BoxGroup("Rolling Credits")] public float speedTransitionTime = 0.5f;
  22. [BoxGroup("Rolling Credits")] public float fastForwardSpeed = 100f;
  23. [BoxGroup("Rolling Credits")] public float reverseSpeed = -100f;
  24. [BoxGroup("Actions"), InlineProperty, HideLabel] public GameActionContext context;
  25. [TabGroup("Actions/Tab", "OnStart"), InlineProperty, HideLabel] public GameActionsController OnStartAction;
  26. [TabGroup("Actions/Tab", "OnEnd"), InlineProperty, HideLabel] public GameActionsController OnEndAction;
  27. [TabGroup("Actions/Tab", "OnCancel"), InlineProperty, HideLabel] public GameActionsController OnCancelAction;
  28. private float speed = 0f;
  29. private float counter = 0f;
  30. private Vector2 size = new Vector2();
  31. private bool done = false;
  32. private bool running = false;
  33. private float desiredHeigth = 0f;
  34. private void Start()
  35. {
  36. Timer.ExecuteRealTime(100, () => {
  37. size = textTransform.sizeDelta;
  38. desiredHeigth = (size.y * 2) + 100 + (Screen.currentResolution.height * 2);
  39. if(listenForRestartEvent) GenericEvents.StartListening(restartEventName, StartRollingCredits);
  40. if(rollCreditsOnStart) StartRollingCredits();
  41. } );
  42. }
  43. private void OnDisable()
  44. {
  45. if(listenForRestartEvent) GenericEvents.StopListening(restartEventName, StartRollingCredits);
  46. }
  47. private void Update()
  48. {
  49. if(running)
  50. {
  51. CancelRollingCredits();
  52. RollCredits();
  53. }
  54. }
  55. [Button("Restart"), ButtonGroup("Butttons")]
  56. public void StartRollingCredits()
  57. {
  58. Reset();
  59. running = true;
  60. if(OnStartAction.context == null) OnStartAction.context = context;
  61. OnStartAction.Restart();
  62. OnStartAction.Start();
  63. }
  64. [Button("Reset"), ButtonGroup("Butttons")]
  65. public void Reset ()
  66. {
  67. var pos = textTransform.position;
  68. size = textTransform.sizeDelta;
  69. pos.y = Screen.currentResolution.height - Screen.currentResolution.height;
  70. textTransform.position = pos;
  71. counter = 0f;
  72. done = false;
  73. speed = normalSpeed;
  74. }
  75. private float t1, t2, t3;
  76. private void RollCredits()
  77. {
  78. if(done) return;
  79. if(Input.GetKey(fastForwardInput))
  80. {
  81. t1 += speedTransitionTime * Time.deltaTime;
  82. t2 -= speedTransitionTime * Time.deltaTime;
  83. t3 -= speedTransitionTime * Time.deltaTime;
  84. if(t1 > 1) t1 = 1;
  85. if(t2 < 0) t2 = 0;
  86. if(t3 < 0) t3 = 0;
  87. speed = Mathf.Lerp(speed, fastForwardSpeed, t1);
  88. }
  89. else if(Input.GetKey(revereInput))
  90. {
  91. t1 -= speedTransitionTime * Time.deltaTime;
  92. t2 += speedTransitionTime * Time.deltaTime;
  93. t3 -= speedTransitionTime * Time.deltaTime;
  94. if(t1 < 0) t1 = 0;
  95. if(t2 > 1) t2 = 1;
  96. if(t3 < 0) t3 = 0;
  97. speed = Mathf.Lerp(speed, reverseSpeed, t2);
  98. }
  99. else
  100. {
  101. t1 -= speedTransitionTime * Time.deltaTime;
  102. t2 -= speedTransitionTime * Time.deltaTime;
  103. t3 += speedTransitionTime * Time.deltaTime;
  104. if(t1 < 0) t1 = 0;
  105. if(t2 < 0) t2 = 0;
  106. if(t3 > 1) t3 = 1;
  107. speed = Mathf.Lerp(speed, normalSpeed, t3);
  108. }
  109. counter += Time.deltaTime;
  110. var pos = textTransform.position;
  111. pos.y += speed * Time.deltaTime;
  112. textTransform.position = pos;
  113. //Debug.Log($"{pos.y} > {desiredHeigth} ({pos.y > desiredHeigth})");
  114. if(pos.y > desiredHeigth)
  115. {
  116. done = true;
  117. running = false;
  118. if(OnEndAction.context == null) OnEndAction.context = context;
  119. OnEndAction.Restart();
  120. OnEndAction.Start();
  121. }
  122. }
  123. private void CancelRollingCredits()
  124. {
  125. if(!running || done) return;
  126. if(Input.GetKeyDown(cancelInput))
  127. {
  128. done = true;
  129. running = false;
  130. if(OnCancelAction.context == null) OnCancelAction.context = context;
  131. OnCancelAction.Restart();
  132. OnCancelAction.Start();
  133. }
  134. }
  135. [OnInspectorInit]
  136. private void SetupInspector()
  137. {
  138. OnStartAction.context = this.context;
  139. OnEndAction.context = this.context;
  140. }
  141. }
  142. }