CreditsController.cs 5.2 KB

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