Added basic bouncing

This commit is contained in:
Pasha Bibko
2026-01-13 12:19:07 +00:00
parent e235c9e440
commit c15d917245
6 changed files with 63 additions and 13 deletions

View File

@@ -800,6 +800,7 @@ MonoBehaviour:
m_EditorClassIdentifier: m_EditorClassIdentifier:
<SampleChild>k__BackingField: {fileID: 1867692258} <SampleChild>k__BackingField: {fileID: 1867692258}
<GameCanvas>k__BackingField: {fileID: 1539476654} <GameCanvas>k__BackingField: {fileID: 1539476654}
<AutoSpawn>k__BackingField: 0
--- !u!4 &2038986853 --- !u!4 &2038986853
Transform: Transform:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

View File

@@ -28,5 +28,13 @@ namespace InterfaceOff
return new Vector3(x, y, 0f); 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;
}
} }
} }

View File

@@ -5,6 +5,6 @@ public static class ListExtensions
public static T GetRandom<T>(this List<T> list) public static T GetRandom<T>(this List<T> list)
{ {
int index = UnityEngine.Random.Range(0, list.Count); int index = UnityEngine.Random.Range(0, list.Count);
return list[index]; return list[1];
} }
} }

View File

@@ -1,3 +1,4 @@
using System;
using UnityEngine; using UnityEngine;
namespace InterfaceOff namespace InterfaceOff
@@ -7,7 +8,10 @@ namespace InterfaceOff
public WindowInteractions Interactions { get; set; } public WindowInteractions Interactions { get; set; }
public WindowComponents Components { 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() public void InstantiateWindowBase()
{ {
@@ -16,11 +20,22 @@ namespace InterfaceOff
OnWindowInstantiation(); 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; 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 OnWindowInstantiation() { }
public virtual void OnWindowClicked() { } public virtual void OnWindowClicked() { }
} }

View File

@@ -1,4 +1,5 @@
using UnityEngine; using TreeEditor;
using UnityEngine;
using UnityEngine.UI; using UnityEngine.UI;
namespace InterfaceOff namespace InterfaceOff
@@ -6,5 +7,14 @@ namespace InterfaceOff
public class WindowComponents : MonoBehaviour public class WindowComponents : MonoBehaviour
{ {
[field: SerializeField] public Image WindowImage { get; private set; } [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
);
} }
} }

View File

@@ -12,6 +12,8 @@ namespace InterfaceOff
[field: SerializeField] private GameObject SampleChild { get; set; } [field: SerializeField] private GameObject SampleChild { get; set; }
[field: SerializeField] private Canvas GameCanvas { get; set; } [field: SerializeField] private Canvas GameCanvas { get; set; }
[field: SerializeField] public bool AutoSpawn { get; private set; } = true;
private void Awake() private void Awake()
{ {
/* Fetches all window types created */ /* Fetches all window types created */
@@ -57,6 +59,8 @@ namespace InterfaceOff
} }
private void FixedUpdate() private void FixedUpdate()
{
if (AutoSpawn)
{ {
/* Checks if it should spawn a window */ /* Checks if it should spawn a window */
bool shouldSpawn = Random.Range(0, 40) == 0; bool shouldSpawn = Random.Range(0, 40) == 0;
@@ -66,4 +70,16 @@ namespace InterfaceOff
} }
} }
} }
private void Update()
{
if (!AutoSpawn)
{
if (Input.GetKeyDown(KeyCode.Space))
{
SpawnNewRandomWindow();
}
}
}
}
} }