using System; using UnityEngine; namespace InterfaceOff { public abstract class WindowBase : MonoBehaviour { public WindowInteractions Interactions { get; set; } public WindowComponents Components { get; set; } protected Vector2 Velocity; public void InstantiateWindowBase() { transform.position = CanvasManager.GetRandomPositionOnCanvas(); 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() { } /* Default close button closes the window */ public virtual void OnWindowCloseButtonClicked() => DestroyWindow(); protected void DestroyWindow() { Destroy(gameObject); } } }