From c15d917245c3fe66ae20f4941e93e58eba76be49 Mon Sep 17 00:00:00 2001 From: Pasha Bibko <156938226+PashaBibko@users.noreply.github.com> Date: Tue, 13 Jan 2026 12:19:07 +0000 Subject: [PATCH] Added basic bouncing --- Assets/Scenes/SampleScene.unity | 1 + Assets/Scripts/CanvasManager.cs | 8 +++++ Assets/Scripts/Extensions/ListExtensions.cs | 2 +- Assets/Scripts/WindowBase.cs | 19 ++++++++++-- Assets/Scripts/WindowComponents.cs | 12 +++++++- Assets/Scripts/WindowSpawner.cs | 34 +++++++++++++++------ 6 files changed, 63 insertions(+), 13 deletions(-) diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index dd8472e..7e63abb 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -800,6 +800,7 @@ MonoBehaviour: m_EditorClassIdentifier: k__BackingField: {fileID: 1867692258} k__BackingField: {fileID: 1539476654} + k__BackingField: 0 --- !u!4 &2038986853 Transform: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/CanvasManager.cs b/Assets/Scripts/CanvasManager.cs index be92004..3023083 100644 --- a/Assets/Scripts/CanvasManager.cs +++ b/Assets/Scripts/CanvasManager.cs @@ -28,5 +28,13 @@ namespace InterfaceOff return new Vector3(x, y, 0f); } + + public static bool IsRectWithinCanvas(Rect rect) + { + Rect b = Instance.GameCanvas.pixelRect; + Rect a = rect; + + return b.xMin <= a.xMin && b.yMin <= a.yMin && b.xMax >= a.xMax && b.yMax >= a.yMax; + } } } diff --git a/Assets/Scripts/Extensions/ListExtensions.cs b/Assets/Scripts/Extensions/ListExtensions.cs index d0329a2..718a1a9 100644 --- a/Assets/Scripts/Extensions/ListExtensions.cs +++ b/Assets/Scripts/Extensions/ListExtensions.cs @@ -5,6 +5,6 @@ public static class ListExtensions public static T GetRandom(this List list) { int index = UnityEngine.Random.Range(0, list.Count); - return list[index]; + return list[1]; } } diff --git a/Assets/Scripts/WindowBase.cs b/Assets/Scripts/WindowBase.cs index 677e344..a83364c 100644 --- a/Assets/Scripts/WindowBase.cs +++ b/Assets/Scripts/WindowBase.cs @@ -1,3 +1,4 @@ +using System; using UnityEngine; namespace InterfaceOff @@ -7,7 +8,10 @@ namespace InterfaceOff public WindowInteractions Interactions { get; set; } public WindowComponents Components { get; set; } - public Vector3 Velocity { get; set; } + protected Vector3 Velocity { get; set; } + + [SerializeField] private int m_FlipCooldown; + private static int MAX_FLIP_COOLDOWN = 20; public void InstantiateWindowBase() { @@ -16,11 +20,22 @@ namespace InterfaceOff OnWindowInstantiation(); } - public void LateUpdate() + private void LateUpdate() { + if (!CanvasManager.IsRectWithinCanvas(Components.Rect) && m_FlipCooldown == 0) + { + m_FlipCooldown = MAX_FLIP_COOLDOWN; + Velocity = -Velocity; + } + transform.position += Velocity * Time.deltaTime; } + private void FixedUpdate() + { + m_FlipCooldown = Math.Clamp(m_FlipCooldown - 1, 0, MAX_FLIP_COOLDOWN); + } + public virtual void OnWindowInstantiation() { } public virtual void OnWindowClicked() { } } diff --git a/Assets/Scripts/WindowComponents.cs b/Assets/Scripts/WindowComponents.cs index 39d0b82..9901b01 100644 --- a/Assets/Scripts/WindowComponents.cs +++ b/Assets/Scripts/WindowComponents.cs @@ -1,4 +1,5 @@ -using UnityEngine; +using TreeEditor; +using UnityEngine; using UnityEngine.UI; namespace InterfaceOff @@ -6,5 +7,14 @@ namespace InterfaceOff public class WindowComponents : MonoBehaviour { [field: SerializeField] public Image WindowImage { get; private set; } + [field: SerializeField] public RectTransform Transform { get; private set; } + + public Rect Rect => new + ( + Transform.position.x - Transform.rect.width / 2, + Transform.position.y - Transform.rect.height / 2, + Transform.rect.width, + Transform.rect.height + ); } } \ No newline at end of file diff --git a/Assets/Scripts/WindowSpawner.cs b/Assets/Scripts/WindowSpawner.cs index 9a57e49..094aa5e 100644 --- a/Assets/Scripts/WindowSpawner.cs +++ b/Assets/Scripts/WindowSpawner.cs @@ -8,10 +8,12 @@ namespace InterfaceOff public class WindowSpawner : MonoBehaviour { private List WindowTypes { get; } = new(); - + [field: SerializeField] private GameObject SampleChild { get; set; } [field: SerializeField] private Canvas GameCanvas { get; set; } + [field: SerializeField] public bool AutoSpawn { get; private set; } = true; + private void Awake() { /* Fetches all window types created */ @@ -23,7 +25,7 @@ namespace InterfaceOff WindowTypes.Add(t); } } - + /* Logs the amount of types found and errors if there is none */ Debug.Log($"Found [{WindowTypes.Count}] different window types "); if (WindowTypes.Count == 0) @@ -37,10 +39,10 @@ namespace InterfaceOff /* Creates the gameobject with a random class */ GameObject go = Instantiate(SampleChild, GameCanvas.transform); go.SetActive(true); - + Type type = WindowTypes.GetRandom(); WindowBase windowBase = go.AddComponent(type) as WindowBase; - + /* Checks it created correctly before instantiating further */ if (DebugUtils.IsNull(windowBase)) { @@ -51,18 +53,32 @@ namespace InterfaceOff /* Makes sure the WindowInteractions and WindowComponents are setup before passing to user code */ windowBase.Interactions = go.GetComponent(); windowBase.Interactions.SetAttachedTo(windowBase); - + windowBase.Components = go.GetComponent(); windowBase.InstantiateWindowBase(); } private void FixedUpdate() { - /* Checks if it should spawn a window */ - bool shouldSpawn = Random.Range(0, 40) == 0; - if (shouldSpawn) + if (AutoSpawn) { - SpawnNewRandomWindow(); + /* Checks if it should spawn a window */ + bool shouldSpawn = Random.Range(0, 40) == 0; + if (shouldSpawn) + { + SpawnNewRandomWindow(); + } + } + } + + private void Update() + { + if (!AutoSpawn) + { + if (Input.GetKeyDown(KeyCode.Space)) + { + SpawnNewRandomWindow(); + } } } }