using UnityEngine;
using System.Collections;

public class Movement : MonoBehaviour
{
	public float maxSpeed;
	public float follow;
	private Vector3 oldPos;
	public GameObject Manager;
	public Transform splatter;
	public Transform blood;
	public Transform splattersound;
	
	// Use this for initialization
	void Start ()
	{

		oldPos = transform.position;
	
	
	}

	private void setOrientation (int orientation)
	{
		transform.eulerAngles = new Vector3 (0, orientation > 0 ? 0 : 180, 0);
	}
	

	// Update is called once per frame
	void Update ()
	{
		Vector3 poschange = -transform.position + Camera.main.ScreenToWorldPoint (Input.mousePosition) + new Vector3 (0, 0, 10);
		Vector3 pos = Vector3.ClampMagnitude (poschange, maxSpeed) + transform.position;
			

		transform.position = Vector3.Lerp (transform.position, pos, follow);

		setOrientation (-(int)Mathf.Sign (oldPos.x - transform.position.x));

		oldPos = transform.position;

	}

	void OnTriggerEnter2D (Collider2D col)
	{
		if (col.gameObject.tag.Equals ("Wolf") && gameObject.tag.Equals("FreeToKill")) {
			Manager.GetComponent<AudioPlayer> ().song5.Play ();
			Manager.GetComponent<AudioPlayer> ().song.Stop();
			Manager.GetComponent<AudioPlayer> ().song1.Stop();
			Manager.GetComponent<AudioPlayer> ().song2.Stop();
			Manager.GetComponent<AudioPlayer> ().song3.Stop();
			Manager.GetComponent<AudioPlayer> ().song4.Stop();
			Instantiate (splatter, transform.position + new Vector3(0,0,1), Quaternion.identity);
			Instantiate (blood, transform.position+ new Vector3(0,0,1), Quaternion.identity);
			Instantiate (splattersound, transform.position+ new Vector3(0,0,1), Quaternion.identity);
			Manager.GetComponent<HungryWolfes>().Dead = true;
			gameObject.GetComponent<SpriteRenderer>().enabled = false;
		}
	}
}
