using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using Sirenix.OdinInspector; using KairoEngine.Core; using KairoEngine.Core.GameActions; namespace KairoEngine.UI { [HideMonoScript] public class CreditsController : MonoBehaviour { public RectTransform textTransform; public bool rollCreditsOnStart = true; public bool listenForRestartEvent = true; public string restartEventName = "RestartRollingCredits"; [BoxGroup("Rolling Credits")] public KeyCode cancelInput = KeyCode.Escape; [BoxGroup("Rolling Credits")] public KeyCode fastForwardInput = KeyCode.DownArrow; [BoxGroup("Rolling Credits")] public KeyCode revereInput = KeyCode.UpArrow; [BoxGroup("Rolling Credits")] public float normalSpeed = 10f; [BoxGroup("Rolling Credits")] public float speedTransitionTime = 0.5f; [BoxGroup("Rolling Credits")] public float fastForwardSpeed = 100f; [BoxGroup("Rolling Credits")] public float reverseSpeed = -100f; [BoxGroup("Actions"), InlineProperty, HideLabel] public GameActionContext context; [TabGroup("Actions/Tab", "OnStart"), InlineProperty, HideLabel] public GameActionsController OnStartAction; [TabGroup("Actions/Tab", "OnEnd"), InlineProperty, HideLabel] public GameActionsController OnEndAction; [TabGroup("Actions/Tab", "OnCancel"), InlineProperty, HideLabel] public GameActionsController OnCancelAction; private float speed = 0f; private float counter = 0f; private Vector2 size = new Vector2(); private bool done = false; private bool running = false; private float desiredHeigth = 0f; private void Start() { Timer.ExecuteRealTime(100, () => { size = textTransform.sizeDelta; desiredHeigth = (size.y * 2) + 100 + (Screen.currentResolution.height * 2); if(listenForRestartEvent) GenericEvents.StartListening(restartEventName, StartRollingCredits); if(rollCreditsOnStart) StartRollingCredits(); } ); } private void OnDisable() { if(listenForRestartEvent) GenericEvents.StopListening(restartEventName, StartRollingCredits); } private void Update() { if(running) { CancelRollingCredits(); RollCredits(); } } [Button("Restart"), ButtonGroup("Butttons")] public void StartRollingCredits() { Reset(); running = true; if(OnStartAction.context == null) OnStartAction.context = context; OnStartAction.Restart(); OnStartAction.Start(); } [Button("Reset"), ButtonGroup("Butttons")] public void Reset () { var pos = textTransform.position; size = textTransform.sizeDelta; pos.y = Screen.currentResolution.height - Screen.currentResolution.height; textTransform.position = pos; counter = 0f; done = false; speed = normalSpeed; } private float t1, t2, t3; private void RollCredits() { if(done) return; if(Input.GetKey(fastForwardInput)) { t1 += speedTransitionTime * Time.deltaTime; t2 -= speedTransitionTime * Time.deltaTime; t3 -= speedTransitionTime * Time.deltaTime; if(t1 > 1) t1 = 1; if(t2 < 0) t2 = 0; if(t3 < 0) t3 = 0; speed = Mathf.Lerp(speed, fastForwardSpeed, t1); } else if(Input.GetKey(revereInput)) { t1 -= speedTransitionTime * Time.deltaTime; t2 += speedTransitionTime * Time.deltaTime; t3 -= speedTransitionTime * Time.deltaTime; if(t1 < 0) t1 = 0; if(t2 > 1) t2 = 1; if(t3 < 0) t3 = 0; speed = Mathf.Lerp(speed, reverseSpeed, t2); } else { t1 -= speedTransitionTime * Time.deltaTime; t2 -= speedTransitionTime * Time.deltaTime; t3 += speedTransitionTime * Time.deltaTime; if(t1 < 0) t1 = 0; if(t2 < 0) t2 = 0; if(t3 > 1) t3 = 1; speed = Mathf.Lerp(speed, normalSpeed, t3); } counter += Time.deltaTime; var pos = textTransform.position; pos.y += speed * Time.deltaTime; textTransform.position = pos; //Debug.Log($"{pos.y} > {desiredHeigth} ({pos.y > desiredHeigth})"); if(pos.y > desiredHeigth) { done = true; running = false; if(OnEndAction.context == null) OnEndAction.context = context; OnEndAction.Restart(); OnEndAction.Start(); } } private void CancelRollingCredits() { if(!running || done) return; if(Input.GetKeyDown(cancelInput)) { done = true; running = false; if(OnCancelAction.context == null) OnCancelAction.context = context; OnCancelAction.Restart(); OnCancelAction.Start(); } } [OnInspectorInit] private void SetupInspector() { OnStartAction.context = this.context; OnEndAction.context = this.context; } } }