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

public class Player : MonoBehaviour
{
    float size;
    float movement;

    float screen_width;

    bool is_standing = true;

    Rigidbody2D rigidbody;
    Animator animator;


    void Start()
    {
        size = transform.localScale.x;

        rigidbody = GetComponent<Rigidbody2D>();
        animator = GetComponent<Animator>();

        screen_width = (Camera.main.orthographicSize * 2) / Screen.height * Screen.width;
    }


    void Update()
    {
        movement = Input.GetAxisRaw("Horizontal") * 7 * Time.deltaTime;

        transform.position += new Vector3(movement, 0, 0);

        if (transform.position.x < -screen_width/2) transform.position = new Vector3(screen_width/2, transform.position.y, 0);
        else if (transform.position.x > screen_width/2) transform.position = new Vector3(-screen_width/2, transform.position.y, 0);

        if (movement < 0) {
            transform.localScale = new Vector3(-size, size, 0);
            if (is_standing) animator.Play("Run");
        }
        else if (movement > 0) {
            transform.localScale = new Vector3(size, size, 0);
            if (is_standing) animator.Play("Run");
        }
        else {
            if (is_standing) animator.Play("Idle");
        }

        if (Input.GetKeyDown(KeyCode.Space) && is_standing) {
            rigidbody.velocity = new Vector2(0, 14);
            is_standing = false;
            animator.Play("Jump");
        }

        if (rigidbody.velocity.y == 0) {
            if (!is_standing) animator.Play("Idle");
            is_standing = true;
        }
    }
}
