using UnityEngine;


public class Player_Controller : MonoBehaviour {
	public int Testint;

    public float MaxSpeed;
	private int activeBody;
    int pastBody;
    bool resetBody;
    public int ActiveBody
	{
		set{ 
			activeBody = value;
			switch (value) {
			case 1:
				{
					spriteBody.sprite = SpriteBody1;
					break;
				}
			case 2:
				{
					spriteBody.sprite = SpriteBody2;
					break;
				}
			case 3:
				{
					spriteBody.sprite = SpriteBody3;
					break;
				}
			case 4:
				{
					spriteBody.sprite = SpriteBody4;
					break;
				}
                case 5: {
                        spriteBody.sprite = SpriteBodyPain;
                        resetBody = false;
                        break;
                    }



			default:
				break;
			}
			
		}get{ return activeBody;}


	}
	public Sprite SpriteBody1;
	public Sprite SpriteBody2;
	public Sprite SpriteBody3;
	public Sprite SpriteBody4;
    public Sprite SpriteBodyPain;

    private Rigidbody2D body;
	private SpriteRenderer spriteBody;



    bool facingLeft = true;


	// Use this for initialization
	void Start () {
        body = GetComponent<Rigidbody2D>();
		spriteBody = GetComponent<SpriteRenderer> ();

		ActiveBody = 1;
	}
	
	// Update is called once per frame
	void Update () {
	


	}

    void FixedUpdate()
    {
        float moveX = Input.GetAxis("Horizontal");
        float moveY = Input.GetAxis("Vertical");


        if (ActiveBody == 5 && resetBody)
        {
            ActiveBody = pastBody;
        }
        else
        {
            resetBody = true;
        }
		//Movment
        if (moveX < 0 && !facingLeft)
        {
            Flip();

        }
        else if (moveX > 0 && facingLeft)
        {
            Flip();
        }
        body.velocity = new Vector2(moveX * MaxSpeed, moveY * MaxSpeed);

		//BodyShape
		if (Input.GetKey ("1")) {
			ActiveBody = 1;
            pastBody = 1;
		}
		if (Input.GetKey ("2")) {
			ActiveBody = 2;
            pastBody = 2;
		}
		if (Input.GetKey ("3")) {
			ActiveBody = 3;
            pastBody = 3;
		}
		if (Input.GetKey ("4")) {
			ActiveBody = 4;
            pastBody = 4;
		}

    }

    void Flip()
    {
        facingLeft = !facingLeft;
        Vector3 theScale = transform.localScale;
        theScale.x *= -1;
        transform.localScale = theScale;
    }

}
