using UnityEngine;
using System.Collections;

public class MovePlayer : MonoBehaviour 
{
    Quaternion OriginalRotation;
    Quaternion NewRot;
    public GameObject NPCinRange;
    public GameObject Arm;
    public AudioClip[] Greetings;

    public int Waved = 0;

	// Use this for initialization
	void Start () 
	{
        OriginalRotation = Arm.transform.localRotation;
        NewRot = Arm.transform.localRotation;
	}
	
	// Update is called once per frame
	void Update () 
	{
        transform.Translate(Vector3.forward * Input.GetAxis("Vertical") * 0.125f);
        transform.Rotate(Vector3.up * Input.GetAxis("Horizontal") * 2.5f);

        float fZ = 0f;
        NewRot = OriginalRotation;
        fZ = Input.GetAxis("Wave");

        NewRot.z -= fZ * 0.2f;
        Arm.transform.localRotation = Quaternion.Slerp(Arm.transform.localRotation, NewRot, Time.deltaTime * 10f);


        RaycastHit hit;
        Ray downRay = new Ray(transform.position, transform.forward);
        if (Physics.Raycast(downRay, out hit))
        {
            if (hit.collider.gameObject.name == "NPC" && Vector3.Distance(transform.position, hit.collider.gameObject.transform.position) < 7.5f)
            {
                if (Arm.transform.localRotation.z < -0.15f)
                {
                    if (!gameObject.GetComponent<AudioSource>().isPlaying)
                    {
                        gameObject.GetComponent<AudioSource>().clip = Greetings[Random.Range(0, 99) / 25];
                        gameObject.GetComponent<AudioSource>().Play();
                    }
                    if (hit.collider.gameObject.GetComponent<NPC>().WaveChecks == 0)
                        hit.collider.gameObject.GetComponent<NPC>().WaveChecks = 1;
                }
                else if (Arm.transform.localRotation.z > 0.15f)
                {
                    if (hit.collider.gameObject.GetComponent<NPC>().WaveChecks == 1)
                    {
                        hit.collider.gameObject.GetComponent<NPC>().WaveChecks = 2;
                        Waved++;
                        hit.collider.gameObject.GetComponent<Rigidbody>().AddForce(Vector3.up * 400f);
                    }
                }
            }
        }
    }
}
