using System; using System.Collections; using UnityEngine; namespace InterfaceOff { public abstract class WindowBase : MonoBehaviour { private WindowSpawner Creator; public WindowInteractions Interactions { get; set; } public WindowComponents Components { get; set; } protected Vector2 Velocity; public void InstantiateWindowBase(WindowSpawner creator) { transform.position = CanvasManager.GetRandomPositionOnCanvas(); Creator = creator; 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 } protected virtual void OnWindowInstantiation() { } /* Default close button closes the window */ public virtual void OnWindowCloseButtonClicked() => DestroyWindow(); public void DestroyWindow() { StartCoroutine(routine: DeathSequence()); Creator.AlertOfDespawnedWindow(); } private IEnumerator DeathSequence() { float duration = 0f; while (duration < 1f) { duration += Time.deltaTime; float scale = 1f - duration; transform.localScale = new Vector3(scale, scale, 1f); yield return null; } Destroy(gameObject); } } }