Made the moving windows move

This commit is contained in:
2026-01-14 19:43:20 +00:00
parent 0c43fc1112
commit f10d0bf7ca
7 changed files with 95 additions and 13 deletions

View File

@@ -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; }

View File

@@ -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() { }
}

View File

@@ -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
);
}
}

View File

@@ -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;

View File

@@ -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()