Added basic bouncing
This commit is contained in:
@@ -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
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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() { }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -8,10 +8,12 @@ namespace InterfaceOff
|
|||||||
public class WindowSpawner : MonoBehaviour
|
public class WindowSpawner : MonoBehaviour
|
||||||
{
|
{
|
||||||
private List<Type> WindowTypes { get; } = new();
|
private List<Type> WindowTypes { get; } = new();
|
||||||
|
|
||||||
[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 */
|
||||||
@@ -23,7 +25,7 @@ namespace InterfaceOff
|
|||||||
WindowTypes.Add(t);
|
WindowTypes.Add(t);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Logs the amount of types found and errors if there is none */
|
/* Logs the amount of types found and errors if there is none */
|
||||||
Debug.Log($"Found [{WindowTypes.Count}] different window types ");
|
Debug.Log($"Found [{WindowTypes.Count}] different window types ");
|
||||||
if (WindowTypes.Count == 0)
|
if (WindowTypes.Count == 0)
|
||||||
@@ -37,10 +39,10 @@ namespace InterfaceOff
|
|||||||
/* Creates the gameobject with a random class */
|
/* Creates the gameobject with a random class */
|
||||||
GameObject go = Instantiate(SampleChild, GameCanvas.transform);
|
GameObject go = Instantiate(SampleChild, GameCanvas.transform);
|
||||||
go.SetActive(true);
|
go.SetActive(true);
|
||||||
|
|
||||||
Type type = WindowTypes.GetRandom();
|
Type type = WindowTypes.GetRandom();
|
||||||
WindowBase windowBase = go.AddComponent(type) as WindowBase;
|
WindowBase windowBase = go.AddComponent(type) as WindowBase;
|
||||||
|
|
||||||
/* Checks it created correctly before instantiating further */
|
/* Checks it created correctly before instantiating further */
|
||||||
if (DebugUtils.IsNull(windowBase))
|
if (DebugUtils.IsNull(windowBase))
|
||||||
{
|
{
|
||||||
@@ -51,18 +53,32 @@ namespace InterfaceOff
|
|||||||
/* Makes sure the WindowInteractions and WindowComponents are setup before passing to user code */
|
/* Makes sure the WindowInteractions and WindowComponents are setup before passing to user code */
|
||||||
windowBase.Interactions = go.GetComponent<WindowInteractions>();
|
windowBase.Interactions = go.GetComponent<WindowInteractions>();
|
||||||
windowBase.Interactions.SetAttachedTo(windowBase);
|
windowBase.Interactions.SetAttachedTo(windowBase);
|
||||||
|
|
||||||
windowBase.Components = go.GetComponent<WindowComponents>();
|
windowBase.Components = go.GetComponent<WindowComponents>();
|
||||||
windowBase.InstantiateWindowBase();
|
windowBase.InstantiateWindowBase();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void FixedUpdate()
|
private void FixedUpdate()
|
||||||
{
|
{
|
||||||
/* Checks if it should spawn a window */
|
if (AutoSpawn)
|
||||||
bool shouldSpawn = Random.Range(0, 40) == 0;
|
|
||||||
if (shouldSpawn)
|
|
||||||
{
|
{
|
||||||
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user