From f10d0bf7ca2f193aedee4c852cf8d33722bf7f5e Mon Sep 17 00:00:00 2001 From: Pasha Date: Wed, 14 Jan 2026 19:43:20 +0000 Subject: [PATCH] Made the moving windows move --- Assets/Prefabs/SampleChild.prefab | 44 +++++++++++++++++++++++--- Assets/Scenes/GameScene.unity | 1 + Assets/Scripts/CanvasManager.cs | 2 ++ Assets/Scripts/WindowBase.cs | 39 +++++++++++++++++++++++ Assets/Scripts/WindowComponents.cs | 11 ++++--- Assets/Scripts/Windows/ImageWindow.cs | 8 ++--- Assets/Scripts/Windows/MovingWindow.cs | 3 ++ 7 files changed, 95 insertions(+), 13 deletions(-) diff --git a/Assets/Prefabs/SampleChild.prefab b/Assets/Prefabs/SampleChild.prefab index f3af726..67afde0 100644 --- a/Assets/Prefabs/SampleChild.prefab +++ b/Assets/Prefabs/SampleChild.prefab @@ -34,7 +34,7 @@ RectTransform: m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_AnchorMin: {x: 0.5, y: 0.5} m_AnchorMax: {x: 0.5, y: 0.5} - m_AnchoredPosition: {x: 0, y: 0} + m_AnchoredPosition: {x: 0, y: -20} m_SizeDelta: {x: 200, y: 200} m_Pivot: {x: 0.5, y: 0.5} --- !u!222 &9139133142503423203 @@ -154,6 +154,41 @@ MonoBehaviour: m_VerticalOverflow: 0 m_LineSpacing: 1 m_Text: +--- !u!1 &2281649515289431590 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 6489330010487774388} + m_Layer: 5 + m_Name: CollisionRect + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!224 &6489330010487774388 +RectTransform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2281649515289431590} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 7011418079103154681} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} + m_AnchorMin: {x: 0.5, y: 0.5} + m_AnchorMax: {x: 0.5, y: 0.5} + m_AnchoredPosition: {x: 0, y: 20} + m_SizeDelta: {x: 200, y: 240} + m_Pivot: {x: 0.5, y: 0.5} --- !u!1 &3890928559484794403 GameObject: m_ObjectHideFlags: 0 @@ -190,7 +225,7 @@ RectTransform: m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_AnchorMin: {x: 0.5, y: 0.5} m_AnchorMax: {x: 0.5, y: 0.5} - m_AnchoredPosition: {x: 0, y: 120} + m_AnchoredPosition: {x: 0, y: 100} m_SizeDelta: {x: 200, y: 40} m_Pivot: {x: 0.5, y: 0.5} --- !u!222 &895478976559852706 @@ -395,12 +430,13 @@ RectTransform: m_Children: - {fileID: 25269235060726753} - {fileID: 8377239034391175029} + - {fileID: 6489330010487774388} m_Father: {fileID: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_AnchorMin: {x: 0.5, y: 0.5} m_AnchorMax: {x: 0.5, y: 0.5} m_AnchoredPosition: {x: 0, y: 0} - m_SizeDelta: {x: 100, y: 100} + m_SizeDelta: {x: 205, y: 245} m_Pivot: {x: 0.5, y: 0.5} --- !u!114 &5345782142064721643 MonoBehaviour: @@ -415,7 +451,7 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: k__BackingField: {fileID: 3009414327569646558} - k__BackingField: {fileID: 7011418079103154681} + k__BackingField: {fileID: 7011418079103154681} k__BackingField: {fileID: 2038821404454815492} --- !u!114 &3265764588146964386 MonoBehaviour: diff --git a/Assets/Scenes/GameScene.unity b/Assets/Scenes/GameScene.unity index dcdc401..d380a28 100644 --- a/Assets/Scenes/GameScene.unity +++ b/Assets/Scenes/GameScene.unity @@ -540,6 +540,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: e090ce5928c145c0b2e514412bc9cdb2, type: 3} m_Name: m_EditorClassIdentifier: + k__BackingField: {fileID: 1539476655} k__BackingField: {fileID: 1539476654} k__BackingField: {fileID: 1810675969122322477, guid: 792c010445685104eac99ebe5c3831ce, type: 3} diff --git a/Assets/Scripts/CanvasManager.cs b/Assets/Scripts/CanvasManager.cs index 39a4c72..68631a7 100644 --- a/Assets/Scripts/CanvasManager.cs +++ b/Assets/Scripts/CanvasManager.cs @@ -4,7 +4,9 @@ namespace InterfaceOff { public class CanvasManager : MonoBehaviour { + [field: SerializeField] public RectTransform CanvasRect { get; private set; } [field: SerializeField] public Canvas GameCanvas { get; private set; } + [field: SerializeField] public GameObject ImagePrefab { get; private set; } [field: SerializeField] public ImageRegistry Images { get; set; } diff --git a/Assets/Scripts/WindowBase.cs b/Assets/Scripts/WindowBase.cs index 3834bf8..8322120 100644 --- a/Assets/Scripts/WindowBase.cs +++ b/Assets/Scripts/WindowBase.cs @@ -1,3 +1,4 @@ +using System; using UnityEngine; namespace InterfaceOff @@ -6,6 +7,8 @@ namespace InterfaceOff { public WindowInteractions Interactions { get; set; } public WindowComponents Components { get; set; } + + protected Vector2 Velocity; public void InstantiateWindowBase() { @@ -14,6 +17,42 @@ namespace InterfaceOff OnWindowInstantiation(); } + protected void LateUpdate() + { + /* Moves the component and calculates new position and half size(s) */ + Components.RectTransform.anchoredPosition += Velocity * Time.deltaTime; + Vector2 position = Components.RectTransform.anchoredPosition; + + Vector2 hCanvasSize = CanvasManager.Instance.CanvasRect.sizeDelta * 0.5f; + Vector2 hSize = Components.RectTransform.sizeDelta * 0.5f; + + /* Calculates outcome of a horizontal bounce (if there is one) */ + if (position.x + hSize.x > hCanvasSize.x || position.x - hSize.x < -hCanvasSize.x) + { + Velocity.x *= -1f; // Inverts X velocity + position.x = Math.Clamp + ( + position.x, + 0 - hCanvasSize.x + hSize.x, + 0 + hCanvasSize.x - hSize.x + ); + } + + /* Calculates outcome of vertical bounce (if there is one) */ + if (position.y + hSize.y > hCanvasSize.y || position.y - hSize.y < -hCanvasSize.y) + { + Velocity.y *= -1f; // Inverts Y velocity + position.y = Math.Clamp + ( + position.y, + 0 - hCanvasSize.y + hSize.y, + 0 + hCanvasSize.y - hSize.y + ); + } + + Components.RectTransform.anchoredPosition = position; // Updates position if it is supposed to] change + } + public virtual void OnWindowInstantiation() { } public virtual void OnWindowClicked() { } } diff --git a/Assets/Scripts/WindowComponents.cs b/Assets/Scripts/WindowComponents.cs index ab0fb5c..5a5e227 100644 --- a/Assets/Scripts/WindowComponents.cs +++ b/Assets/Scripts/WindowComponents.cs @@ -1,20 +1,21 @@ using UnityEngine.UI; using UnityEngine; +using UnityEngine.Serialization; namespace InterfaceOff { public class WindowComponents : MonoBehaviour { [field: SerializeField] public Image WindowImage { get; private set; } - [field: SerializeField] public RectTransform Transform { get; private set; } + [field: SerializeField] public RectTransform RectTransform { get; private set; } [field: SerializeField] public Text InfoText { 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 + RectTransform.position.x - RectTransform.rect.width / 2, + RectTransform.position.y - RectTransform.rect.height / 2, + RectTransform.rect.width, + RectTransform.rect.height ); } } \ No newline at end of file diff --git a/Assets/Scripts/Windows/ImageWindow.cs b/Assets/Scripts/Windows/ImageWindow.cs index e065f9d..a34d722 100644 --- a/Assets/Scripts/Windows/ImageWindow.cs +++ b/Assets/Scripts/Windows/ImageWindow.cs @@ -7,10 +7,10 @@ namespace InterfaceOff { private static Vector3[] Positions = { - new(-45, 45f), - new(45f, 45f), - new(-45, -45), - new(45f, -45) + new(-45, 25f), + new(45f, 25f), + new(-45, -65), + new(45f, -65) }; private int m_TilesRotatedCorrectly; diff --git a/Assets/Scripts/Windows/MovingWindow.cs b/Assets/Scripts/Windows/MovingWindow.cs index 70be58e..139942d 100644 --- a/Assets/Scripts/Windows/MovingWindow.cs +++ b/Assets/Scripts/Windows/MovingWindow.cs @@ -11,6 +11,9 @@ namespace InterfaceOff /* Creates a random health value */ m_Health = Random.Range(2, 6); Components.InfoText.text = $"{m_Health}"; + + float angle = Random.Range(0, Mathf.PI * 2); + Velocity = new Vector2(Mathf.Cos(angle), Mathf.Sin(angle)) * 100; } public override void OnWindowClicked()