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

public class EnvironmentManager : MonoBehaviour {

	#region variables
	[SerializeField]
	protected GameObject _player;
	protected WallController _overlapping;
	#endregion
	

	#region events
	void Awake () {

		_player = GameObject.FindGameObjectWithTag ("Player");
		if (_player == null) this.enabled = false;

	}

	void Update () {

		Vector3 target = (_player.transform.position - Camera.main.transform.position);
		Ray ray = new Ray (Camera.main.transform.position, target);
		RaycastHit hit;

		if (Physics.Raycast (ray, out hit, 100f)) {
			if (hit.transform.gameObject.layer == LayerMask.NameToLayer ("Environment")) {

				WallController wc = hit.transform.GetComponent <WallController> ();
				if (wc != _overlapping) {
					if (_overlapping != null) _overlapping.Toggle (true);

					_overlapping = wc;
					_overlapping.Toggle (false);
				}

			} else {

				if (_overlapping != null) {
					_overlapping.Toggle (true);
					_overlapping = null;
				}

			}
		}
	}

	void OnDrawGizmos () {

		if (_player != null) {
			Vector3 target = (_player.transform.position - Camera.main.transform.position);
			Ray ray = new Ray (Camera.main.transform.position, target);
			Gizmos.DrawRay (ray);
		}

	}
	#endregion
}
