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:
<SampleChild>k__BackingField: {fileID: 1867692258}
<GameCanvas>k__BackingField: {fileID: 1539476654}
<AutoSpawn>k__BackingField: 0
--- !u!4 &2038986853
Transform:
m_ObjectHideFlags: 0

View File

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

View File

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

View File

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

View File

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

View File

@@ -8,10 +8,12 @@ namespace InterfaceOff
public class WindowSpawner : MonoBehaviour
{
private List<Type> 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<WindowInteractions>();
windowBase.Interactions.SetAttachedTo(windowBase);
windowBase.Components = go.GetComponent<WindowComponents>();
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();
}
}
}
}