﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpaceBoundary : MonoBehaviour
{
    public Vector3 size;

    private void OnDrawGizmos()
    {
        Gizmos.DrawWireCube(transform.position, size);
    }

    private void FixedUpdate()
    {
        Bounds bounds = new Bounds(transform.position, size);

        foreach (var obj in SpaceObject.All)
        {
            Transform t = obj.transform;
            Vector3 position = t.position;

            if (!bounds.Contains(position))
            {
                //loop when we hit an edge
                if (position.x > bounds.max.x) position.x = bounds.min.x;
                else if (position.x < bounds.min.x) position.x = bounds.max.x;

                if (position.y > bounds.max.y) position.y = bounds.min.y;
                else if (position.y < bounds.min.y) position.y = bounds.max.y;

                if (position.z > bounds.max.z) position.z = bounds.min.z;
                else if (position.z < bounds.min.z) position.z = bounds.max.z;

                //set position
                t.position = position;
            }

        }
    }
}
