using UnityEngine;
using System.Collections;

public class LasorController : MonoBehaviour, IInteractable, IResetable
{
	private Vector3 shotAngle;
	private float shotDistance;
	[SerializeField]
	private GameObject beam;
	[SerializeField]
	private float distanceScaleMultiplier = 0.4f;
	[SerializeField]
	private float distanceScaleTweak = 0.01f;
	[SerializeField]
	private Transform emmisionPoint;
	private bool shooting = true;
	[SerializeField]
	private ParticleSystem particles;
	[SerializeField]
	private float spinRate = 1f;

	void Update()
	{
		if(shooting)
		{
			shotAngle = transform.TransformDirection(Vector2.up);
			RaycastHit2D cast = Physics2D.Raycast(emmisionPoint.position, shotAngle);
			shotDistance = cast.distance;
			Vector3 newScale = new Vector3(0.05f, (cast.distance*distanceScaleMultiplier) - distanceScaleTweak, 1f);
			beam.transform.localScale = newScale;
			if(cast.collider.CompareTag("Player"))
			{
				Debug.Log("Kill Player");
			}
		}
		else
		{
			beam.transform.localScale = Vector3.zero;
		}
	}

	public void Interact()
	{
		shooting = false;
		GetComponent<Animator>().SetTrigger("stop");
		particles.Stop();
	}

	public void Reset()
	{
		shooting = true;
		GetComponent<Animator>().SetTrigger("reset");
		particles.Play();
	}
}