using UnityEngine;
using System.Collections;

public class LineCtrl : MonoBehaviour {

	public static LineCtrl instance;

	Transform start;
	Transform end;
	LineRenderer lineRenderer;
	Material lineMaterial;
	float offset=0;

	// Use this for initialization
	void Start () {
		instance = this;
		start = end = transform;
		lineRenderer = GetComponent<LineRenderer> ();
		lineRenderer.enabled = false;
		lineMaterial = renderer.material;
	}
	
	// Update is called once per frame
	void Update () {
		if(lineRenderer.enabled == true){
			lineRenderer.SetPosition(0, start.position);
			lineRenderer.SetPosition(1, end.position);

			float tilScale = Vector3.Distance(start.position, end.position)/2;
			offset -= Time.deltaTime*2;
			lineMaterial.SetTextureScale("_MainTex",new Vector2(tilScale,1));
			lineMaterial.SetTextureOffset("_MainTex", new Vector2(offset,1));
		}
	}

	public void EnableLine(Transform startPos, Transform endPos){
		start = startPos;
		end = endPos;
		lineRenderer.SetPosition(0, start.position);
		lineRenderer.SetPosition(1, end.position);
		lineRenderer.enabled = true;
	}

	public void DisableLine(){
		end = start = transform;
		lineRenderer.SetPosition(0, start.position);
		lineRenderer.SetPosition(1, end.position);
		lineRenderer.enabled = false;
	}

}
