using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class PlayerMovement : MonoBehaviour {
	PlayerSkills skills; // this is what i was missing last time
	public float addedSpeedModifier = 0f; //for testing
	public float speed = 0f;
	public float moveX = 0f;
	public float moveY = 0f;
	MyManager manager;

	public List<Sprite> PlayerSprites = new List<Sprite>();

	void Awake()
	{
		manager = GameObject.Find("MyManager").GetComponent<MyManager>();
		skills = gameObject.GetComponent<PlayerSkills>(); // hopefull this works now...
	}

	void Update()
	{
		SpriteRenderer sp = GetComponent<SpriteRenderer>();

		if(moveX > 0 || moveY > 0 || moveX < 0 || moveY < 0)
		{
			if(!manager.player.stepAudio.isPlaying)
				manager.player.stepAudio.Play();
		}

		if(moveX > 0)
		{
			Vector3 myScale = transform.localScale;
			myScale.x = -1;
			transform.localScale = myScale;
			sp.sprite = PlayerSprites[2];
		}
		else if(moveX < 0)
		{
			Vector3 myScale = transform.localScale;
			myScale.x = 1;
			transform.localScale = myScale;
			sp.sprite = PlayerSprites[2];
		}
		else
		{
			if(moveY > 0 && moveX == 0)
				sp.sprite = PlayerSprites[1];
			else
				sp.sprite = PlayerSprites[0];

		}
	}

	void FixedUpdate () {
		if(manager.player.CanMove)
		{
			speed = (skills.GetSkill("Speed")/10f)+addedSpeedModifier;

			moveX = Input.GetAxis("Horizontal");
			moveY = Input.GetAxis("Vertical");
			//Debug.Log("X: " + moveX);
			//Debug.Log("Y: " + moveY);

			rigidbody2D.velocity = new Vector2(moveX * speed, moveY * speed);
		}
		else
		{
			rigidbody2D.velocity = new Vector2(0,0);
		}
	}

}
