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

public class PlayerController : MonoBehaviour
{
    public PlayerInputControl inputControl;
    public Vector2 inputDirection;

    private Rigidbody2D rb;
    private PhysicsCheck physicsCheck;

    [Header("基本参数")]
    public float speed;

    public float jumpForce;

    private void Awake()
    {
        rb = GetComponent<Rigidbody2D>();
        physicsCheck = GetComponent<PhysicsCheck>();
        inputControl = new PlayerInputControl();
        inputControl.GamePlayerInput.Jump.started += Jump;
        
    }

    

    private void OnEnable()
    {
        inputControl.Enable();
    }

    private void OnDisable()
    {
        inputControl.Disable();
    }

    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        inputDirection = inputControl.GamePlayerInput.Move.ReadValue<Vector2>();
    }

    private void FixedUpdate()
    {
        Move();
    }

    //移动
    public void Move()
    {
        
        rb.velocity = new Vector2(inputDirection.x*speed * Time.deltaTime, rb.velocity.y);
        //翻转
        int faceDir = (int)transform.localScale.x;
        if(inputDirection.x > 0 )
        {
            faceDir = 1;
        }
        if (inputDirection.x < 0)
        {
            faceDir = -1;
        }

        transform.localScale = new Vector3(faceDir, transform.localScale.y, transform.localScale.z);
    }

    //跳跃
    private void Jump(InputAction.CallbackContext context)
    {
        if(physicsCheck.isGround)
        {
            rb.AddForce(transform.up * jumpForce, ForceMode2D.Impulse);
        }
       
    
    }
}
