using UnityEngine;
using System.Collections;

public class MovingScript : MonoBehaviour {

    public enum Dir
    {
        NORTH,
        SOUTH,
        WEST,
        EAST,
        NWEST,
        NEAST,
        SWEST,
        SEAST,
        NONE
    }

	// Use this for initialization
	void Start () {
        	
	}

    static public Dir getDirection(float hDir, float vDir)
    {
        Dir l_direction = Dir.NONE;

        if (hDir >= 1 && vDir >= 1)
        {
            l_direction = Dir.NEAST;
        }
        else if (hDir >= 1 && vDir <= -1)
        {
            l_direction = Dir.SEAST;
        }
        else if (hDir <= -1 && vDir >= 1)
        {
            l_direction = Dir.NWEST;
        }
        else if (hDir <= -1 && vDir <= -1)
        {
            l_direction = Dir.SWEST;
        }
        else if (hDir >= 1 && (vDir > -1 && vDir < 1))
        {
            l_direction = Dir.EAST;
        }
        else if (hDir <= -1 && (vDir > -1 && vDir < 1))
        {
            l_direction = Dir.WEST;
        }
        else if ((hDir > -1 && hDir < 1) && vDir >= 1)
        {
            l_direction = Dir.NORTH;
        }
        else if ((hDir > -1 && hDir < 1) && vDir <= -1)
        {
            l_direction = Dir.SOUTH;
        }

        return l_direction;
    }

    static public void setAnimation(Animator anim, Dir dir, Transform trans)
    {
        switch (dir)
        {
            case Dir.NORTH:
                anim.SetInteger("Direction", 1);
                break;
            case Dir.SOUTH:
                anim.SetInteger("Direction", 1);
                break;
            case Dir.EAST:
                trans.localRotation = Quaternion.Euler(0, 0, 0);
                anim.SetInteger("Direction", 1);
                break;
            case Dir.WEST:
                trans.localRotation = Quaternion.Euler(0, 180, 0);
                anim.SetInteger("Direction", 1);
                break;
            case Dir.NEAST:
                anim.SetInteger("Direction", 1);
                break;
            case Dir.SEAST:
                trans.localRotation = Quaternion.Euler(0, 0, 0);
                anim.SetInteger("Direction", 1);
                break;
            case Dir.NWEST:
                trans.localRotation = Quaternion.Euler(0, 180, 0);
                anim.SetInteger("Direction", 3);
                break;
            case Dir.SWEST:
                trans.localRotation = Quaternion.Euler(0, 180, 0);
                anim.SetInteger("Direction", 1);
                break;
            case Dir.NONE:
                anim.SetInteger("Direction", -1); // Idle
                break;
        }
    }

    static public void setMovement(Animator anim, Transform trans, Dir dir, float hDir, float vDir, float speed)
    {

        float l_hDir = 0, l_vDir = 0;

        if (Mathf.Abs(hDir) >= 1)
            l_hDir = 1;

        if (vDir >= 1)
            l_vDir = 1;
        else if (vDir <= -1)
            l_vDir = -1;

        if (dir != Dir.NONE)
        {
            Vector3 l_direction = new Vector3(l_hDir, l_vDir, l_vDir);
            trans.Translate(l_direction * speed * Time.deltaTime);
        }
    }
	
	// Update is called once per frame
	void Update () {
        
	}
}
