using UnityEngine;
using System.Collections;

public class CheckPlayerInteractableCollisionScript : MonoBehaviour 
{
	public Interactable hitInteractable {get; set;}

	private GameObject lastHit;

	private Transform myTransform;

	void Start()
	{
		myTransform = transform;
	}

	void OnTriggerEnter2D(Collider2D trig)
	{
		if (trig.gameObject.layer == Constants.INTERACTABLES_LAYER_ID)
		{
			hitInteractable = trig.gameObject.GetComponent<Interactable>();
			lastHit = trig.gameObject;
		}
	}

	// NOTE: This does not get called if my localScale != 1
	void OnTriggerExit2D(Collider2D trig)
	{
		if (trig.gameObject.layer == Constants.INTERACTABLES_LAYER_ID)
		{
			hitInteractable = null;
			lastHit = null;
		}
	}

	void Update()
	{
		// Check to see if we have a target assigned
		if (lastHit != null)
		{
			// Do a distance calculation to simulate OnTriggerExit2D
			if (Vector3.Distance(lastHit.transform.position, myTransform.position) > Constants.MAX_TARGET_DISTANCE)
			{
				hitInteractable = null;
				lastHit = null;
			}
		}

	}
}
