From 4ace832a118108207d8069575926f132e35c2050 Mon Sep 17 00:00:00 2001 From: minenice55 <43734252+minenice55@users.noreply.github.com> Date: Mon, 16 May 2022 01:29:39 -0400 Subject: [PATCH] Global Camera Movement (#80) * Global Camera: move Spaceball to new system * Global Camera: holy shit I'm done already????? --- Assets/Scripts/GameCamera.cs | 140 ++++++++++++++++++++ Assets/Scripts/GameManager.cs | 2 +- Assets/Scripts/Games/Spaceball/Spaceball.cs | 10 +- Assets/Scripts/Minigames.cs | 22 +++ 4 files changed, 168 insertions(+), 6 deletions(-) diff --git a/Assets/Scripts/GameCamera.cs b/Assets/Scripts/GameCamera.cs index 2646c00a..ec686145 100644 --- a/Assets/Scripts/GameCamera.cs +++ b/Assets/Scripts/GameCamera.cs @@ -2,6 +2,9 @@ using System.Collections; using System.Collections.Generic; using UnityEngine; +using HeavenStudio.Util; +using System.Linq; + namespace HeavenStudio { public class GameCamera : MonoBehaviour @@ -9,6 +12,42 @@ namespace HeavenStudio public static GameCamera instance { get; private set; } public new Camera camera; + private List positionEvents = new List(); + private List rotationEvents = new List(); + private List scaleEvents = new List(); + + /** + default cam position, for quick-resetting + **/ + static Vector3 defaultPosition = new Vector3(0, 0, -10); + static Vector3 defaultRotEluer = new Vector3(0, 0, 0); + static Vector3 defaultScale = new Vector3(16, 9, 1); + + /** + camera's current transformation + TODO: stretching (the scale param) not working, will need help with this cause I don't understand Unity's camera + **/ + private static Vector3 position; + private static Vector3 rotEluer; + private static Vector3 scale; + + /** + camera's last transformation + TODO: stretching (the scaleLast param) not working, will need help with this cause I don't understand Unity's camera + **/ + private static Vector3 positionLast; + private static Vector3 rotEluerLast; + private static Vector3 scaleLast; + + /** + transformations to apply *after* the global transform, + to use in minigame scripts (Spaceball, Rhythm Rally, Built to Scale, etc.) + and NOT in the editor + **/ + public static Vector3 additionalPosition; + public static Vector3 additionalRotEluer; + public static Vector3 additionalScale; + [Header("Components")] public Color baseColor; @@ -20,7 +59,108 @@ namespace HeavenStudio private void Start() { + GameManager.instance.onBeatChanged += OnBeatChanged; camera.backgroundColor = baseColor; + + ResetTransforms(); + ResetAdditionalTransforms(); + + positionLast = defaultPosition; + rotEluerLast = defaultRotEluer; + scaleLast = defaultScale; + } + + public void OnBeatChanged(float beat) + { + ResetTransforms(); + ResetAdditionalTransforms(); + + positionLast = defaultPosition; + rotEluerLast = defaultRotEluer; + scaleLast = defaultScale; + + // this entire thing is a mess redo it later + //pos + positionEvents = EventCaller.GetAllInGameManagerList("gameManager", new string[] { "move camera" }); + + //rot + rotationEvents = EventCaller.GetAllInGameManagerList("gameManager", new string[] { "rotate camera" }); + + //scale (TODO) + // scaleEvents = EventCaller.GetAllInGameManagerList("gameManager", new string[] { "scale camera" }); + + UpdateCameraTranslate(); + UpdateCameraRotate(); + } + + private void Update() + { + UpdateCameraTranslate(); + UpdateCameraRotate(); + + Camera cam = GetCamera(); + cam.transform.localPosition = position + additionalPosition; + cam.transform.eulerAngles = rotEluer + additionalRotEluer; + cam.transform.localScale = Vector3.Scale(scale, additionalScale); + } + + private void UpdateCameraTranslate() + { + foreach (var e in positionEvents) + { + float prog = Conductor.instance.GetPositionFromBeat(e.beat, e.length); + if (prog >= 0f) + { + EasingFunction.Function func = EasingFunction.GetEasingFunction(e.ease); + float dx = func(positionLast.x, e.valA, Mathf.Min(prog, 1f)); + float dy = func(positionLast.y, e.valB, Mathf.Min(prog, 1f)); + float dz = func(positionLast.z, -e.valC, Mathf.Min(prog, 1f)); + position = new Vector3(dx, dy, dz); + } + if (prog > 1f) + { + positionLast = new Vector3(e.valA, e.valB, -e.valC); + } + } + } + + private void UpdateCameraRotate() + { + foreach (var e in rotationEvents) + { + float prog = Conductor.instance.GetPositionFromBeat(e.beat, e.length); + if (prog >= 0f) + { + EasingFunction.Function func = EasingFunction.GetEasingFunction(e.ease); + float dx = func(rotEluerLast.x, e.valA, Mathf.Min(prog, 1f)); + float dy = func(rotEluerLast.y, e.valB, Mathf.Min(prog, 1f)); + float dz = func(rotEluerLast.z, e.valC, Mathf.Min(prog, 1f)); + rotEluer = new Vector3(dx, dy, dz); + } + if (prog > 1f) + { + rotEluerLast = new Vector3(e.valA, e.valB, -e.valC); + } + } + } + + public static void ResetTransforms() + { + position = defaultPosition; + rotEluer = defaultRotEluer; + scale = defaultScale; + } + + public static void ResetAdditionalTransforms() + { + additionalPosition = new Vector3(0, 0, 0); + additionalRotEluer = new Vector3(0, 0, 0); + additionalScale = new Vector3(1, 1, 1); + } + + public static Camera GetCamera() + { + return instance.camera; } } } \ No newline at end of file diff --git a/Assets/Scripts/GameManager.cs b/Assets/Scripts/GameManager.cs index 809eedd9..afde1044 100644 --- a/Assets/Scripts/GameManager.cs +++ b/Assets/Scripts/GameManager.cs @@ -444,7 +444,7 @@ namespace HeavenStudio public void ResetCamera() { - GameCamera.transform.localPosition = new Vector3(0, 0, -10); + HeavenStudio.GameCamera.ResetAdditionalTransforms(); } } } \ No newline at end of file diff --git a/Assets/Scripts/Games/Spaceball/Spaceball.cs b/Assets/Scripts/Games/Spaceball/Spaceball.cs index 6bcf6846..48ef7863 100644 --- a/Assets/Scripts/Games/Spaceball/Spaceball.cs +++ b/Assets/Scripts/Games/Spaceball/Spaceball.cs @@ -117,27 +117,27 @@ namespace HeavenStudio.Games { if (normalizedBeat > 1) { - GameCamera.instance.camera.transform.localPosition = new Vector3(0, 0, currentZoomCamDistance); + GameCamera.additionalPosition = new Vector3(0, 0, currentZoomCamDistance + 10); } else { if (currentZoomCamLength < 0) { - GameCamera.instance.camera.transform.localPosition = new Vector3(0, 0, currentZoomCamDistance); + GameCamera.additionalPosition = new Vector3(0, 0, currentZoomCamDistance + 10); } else { EasingFunction.Function func = EasingFunction.GetEasingFunction(lastEase); - float newPosZ = func(lastCamDistance, currentZoomCamDistance, normalizedBeat); - GameCamera.instance.camera.transform.localPosition = new Vector3(0, 0, newPosZ); + float newPosZ = func(lastCamDistance + 10, currentZoomCamDistance + 10, normalizedBeat); + GameCamera.additionalPosition = new Vector3(0, 0, newPosZ); } } } else { // ? - GameCamera.instance.camera.transform.localPosition = new Vector3(0, 0, -10f); + GameCamera.additionalPosition = new Vector3(0, 0, 0); } } } diff --git a/Assets/Scripts/Minigames.cs b/Assets/Scripts/Minigames.cs index f26ab5ae..cbecd436 100644 --- a/Assets/Scripts/Minigames.cs +++ b/Assets/Scripts/Minigames.cs @@ -149,6 +149,28 @@ namespace HeavenStudio { new Param("toggle", true, "Enable Inputs") }), + + new GameAction("move camera", delegate + { + //TODO: move cam + }, 1f, true, new List() + { + new Param("valA", new EntityTypes.Float(-50, 50, 0), "Right / Left"), + new Param("valB", new EntityTypes.Float(-50, 50, 0), "Up / Down"), + new Param("valC", new EntityTypes.Float(-0, 250, 10), "In / Out"), + new Param("ease", EasingFunction.Ease.Linear, "Ease Type") + } ), + + new GameAction("rotate camera", delegate + { + //TODO: rot cam + }, 1f, true, new List() + { + new Param("valA", new EntityTypes.Integer(-360, 360, 0), "Pitch"), + new Param("valB", new EntityTypes.Integer(-360, 360, 0), "Yaw"), + new Param("valC", new EntityTypes.Integer(-360, 360, 0), "Roll"), + new Param("ease", EasingFunction.Ease.Linear, "Ease Type") + } ), }), new Minigame("countIn", "Count-Ins", "", false, true, new List() {