Browse Source

Added Rolling Credits Controller

James Peret 2 years ago
parent
commit
154012b056
3 changed files with 161 additions and 1 deletions
  1. 148 0
      Runtime/CreditsController.cs
  2. 11 0
      Runtime/CreditsController.cs.meta
  3. 2 1
      Runtime/UiSystemElement.cs

+ 148 - 0
Runtime/CreditsController.cs

@@ -0,0 +1,148 @@
+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;
+        [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);
+                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;
+        }
+    }
+}

+ 11 - 0
Runtime/CreditsController.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 17f128ef4b9e4e3438fba01df5131120
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 2 - 1
Runtime/UiSystemElement.cs

@@ -1,10 +1,11 @@
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
-using UnityEngine.UI;
+using Sirenix.OdinInspector;
 
 namespace KairoEngine.UI
 {
+    [HideMonoScript]
     public class UiSystemElement : MonoBehaviour, IUiSystemElement
     {
         public string elementTitle = "New UI Element";