using UnityEngine;
using System.Collections;

public class LaserBeam : MonoBehaviour {

	private bool laserEnabled;

	private float offsetX;
	private float offsetY;

	private float startTime;
	private float posX;
	private float posY;

	void Start(){
		laserEnabled = false;
		collider2D.enabled = false;
		renderer.enabled = false;
		startTime = Time.time;
		offsetX = 0.0f;
		offsetY = 0.0f;
		posX = 0.0f;
		posY = 0.0f;
	}

	void FixedUpdate(){
		if (laserEnabled) {
			offsetX = Random.Range (-1 * Constants.CAM_SHAKE_MAGNITUDE, Constants.CAM_SHAKE_MAGNITUDE);
			offsetY = Random.Range (-1 * Constants.CAM_SHAKE_MAGNITUDE, Constants.CAM_SHAKE_MAGNITUDE);
		} else {
			offsetX = Mathf.Lerp(posX , 0.0f, (Time.time - startTime) / Constants.CAM_POSITION_RESET_TIME);
			offsetY = Mathf.Lerp(posY , 0.0f, (Time.time - startTime) / Constants.CAM_POSITION_RESET_TIME);
		}
		Camera.main.transform.position = new Vector3 (offsetX,offsetY, Camera.main.transform.position.z);		
	}

	public void Fire(Transform t){
		laserEnabled = true;
		GameObject.FindGameObjectWithTag ("Kilcal").renderer.enabled = true;
		collider2D.enabled = true;
		renderer.enabled = true;
	}

	public void StopFire(){
		laserEnabled = false;
		GameObject.FindGameObjectWithTag ("Kilcal").renderer.enabled = false;
		collider2D.enabled = false;
		renderer.enabled = false;
		startTime = Time.time;
		posX = Camera.main.transform.position.x;
		posY = Camera.main.transform.position.y;
		Camera.main.transform.position = new Vector3 (0.0f, 0.0f, -10.0f);
	}
}