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

public class PlayerController : MonoBehaviour
{
    [SerializeField] LayerMask BlockLayer;
    private enum DIRECTION_TYPE
    {
        DEFAULT,
        RIGHT,
        LEFT
    }
    private DIRECTION_TYPE dir;
    private Rigidbody2D rb2;
    private float _speed;
    private Animator anim;
    private float Speed;
    private float JumpPower;

    // Start is called before the first frame update
    void Start()
    {
        //向きの設定、初期はデフォルト状態
        dir = DIRECTION_TYPE.DEFAULT;
        
        //コンポーネント取得
        rb2 = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
        
        //移動とジャンプ値を設定
        Speed = 5.0f;
        JumpPower = 500.0f;
    }

    // Update is called once per frame
    void Update()
    {
        InputKey();
        CheckJump();
    }

    void FixedUpdate()
    {
        Move();
    }

    private void InputKey()
    {
        //キーの入力を取得し、アニメーションに反映
        float x = Input.GetAxisRaw("Horizontal");
        anim.SetFloat("AnimSpeed", Mathf.Abs(x));

        if( x == 0)
        {
            //向きはそのまま
            dir = DIRECTION_TYPE.DEFAULT;
        }
        else if( x > 0)
        {
            //右向き
            dir = DIRECTION_TYPE.RIGHT;
        }
        else
        {
            //左向き
            dir = DIRECTION_TYPE.LEFT;
        }
    }

    private void CheckJump()
    {
        //地面に設置していたら
        if(IsGround())
        {
            //スペース押されていたらジャンプ処理
            if(Input.GetButtonDown("Jump"))
            {
                rb2.AddForce(Vector2.up * JumpPower);
            }
        }
    }

    private void Move()
    {
        switch(dir)
        {
            case DIRECTION_TYPE.DEFAULT:
                //スピード無し
                _speed = 0;
                break;
            case DIRECTION_TYPE.RIGHT:
                //右に移動
                _speed = Speed;
                transform.localScale = new Vector3(2,2,2);
                break;
            case DIRECTION_TYPE.LEFT:
                //左に移動
                _speed = -(Speed);
                transform.localScale = new Vector3(-2,2,2);
                break;
            default:
                //スピード無し
                _speed = 0;
                break;
        }

        //移動処理
        rb2.velocity = new Vector2(_speed, rb2.velocity.y);
    }

    private bool IsGround()
    {
        bool isBlock = false;
        Vector3 rightStartPoint = transform.position + Vector3.right * 0.4f;
        Vector3 leftStartPoint = transform.position - Vector3.right * 0.4f;
        Vector3 endPoint = transform.position - Vector3.up * 0.2f;
        Debug.DrawLine(rightStartPoint , endPoint);
        Debug.DrawLine(leftStartPoint , endPoint);

        //地面に当たっているか判定
        if( Physics2D.Linecast(rightStartPoint , endPoint, BlockLayer) ||
            Physics2D.Linecast(leftStartPoint , endPoint ,BlockLayer) )
        {
            isBlock = true;
        }

        return isBlock;
    }
}
