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

/// <summary>
/// Class for dealing with the outer boundary
/// </summary>
public class BoundaryManager : MonoBehaviour
{
    // The amout of force that the object is pushed back
    public float pushbackForce;
    public GameObject hitLight;

    private MeshCollider _meshCollier;

    // Start is called before the first frame update
    void Awake()
    {
        _meshCollier = gameObject.GetComponent<MeshCollider>();
    }

    void OnCollisionStay(Collision collision)
    {        
        DoCollision(collision.gameObject, collision);
    }

    void OnCollisionEnter(Collision collision)
    {     
        DoCollision(collision.gameObject, collision);
    }

    private void DoCollision(GameObject collidedObject, Collision collisionPoint)
    {
        BoundaryCheck collidedBoundaryCheck = collidedObject.GetComponent<BoundaryCheck>();
        if (collidedBoundaryCheck)
        {
            GameObject newLight = Instantiate(hitLight);
            newLight.transform.position = collisionPoint.GetContact(0).point;
            newLight.SetActive(true);

            collidedBoundaryCheck.DoBoundaryPushback(collisionPoint.GetContact(0).normal, pushbackForce);
        }
    }    
}
