using UnityEngine;
/// <summary>
/// GAME 1303 Tier 5 — Go off one edge, come back on the other. Optional for Weenie Maze
/// (classic Snake has open edges; a walled maze doesn't need this) and useful for any
/// single-screen game.
///
/// SETUP
/// - Add to anything that moves. Set Bounds either by dragging in a BoxCollider2D that
/// covers the play field, or by typing Area Size (centred on Area Centre).
/// - Works with GridMover and TopDownMover alike — it just teleports the transform.
/// </summary>
public class WrapAround : MonoBehaviour
{
public BoxCollider2D boundsCollider;
public Vector2 areaCentre = Vector2.zero;
public Vector2 areaSize = new Vector2(20f, 12f);
public bool wrapHorizontally = true;
public bool wrapVertically = true;
void LateUpdate()
{
Vector2 centre = boundsCollider != null ? (Vector2)boundsCollider.bounds.center : areaCentre;
Vector2 size = boundsCollider != null ? (Vector2)boundsCollider.bounds.size : areaSize;
Vector2 half = size * 0.5f;
Vector3 p = transform.position;
if (wrapHorizontally)
{
if (p.x > centre.x + half.x) p.x = centre.x - half.x;
else if (p.x < centre.x - half.x) p.x = centre.x + half.x;
}
if (wrapVertically)
{
if (p.y > centre.y + half.y) p.y = centre.y - half.y;
else if (p.y < centre.y - half.y) p.y = centre.y + half.y;
}
transform.position = p;
}
void OnDrawGizmosSelected()
{
Gizmos.color = Color.cyan;
Vector3 c = boundsCollider != null ? boundsCollider.bounds.center : (Vector3)areaCentre;
Vector3 s = boundsCollider != null ? boundsCollider.bounds.size : (Vector3)areaSize;
Gizmos.DrawWireCube(c, s);
}
}