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

public class PlayerController : BaseUnit
{
    public ControlInputs controlInputs;
    public int PlayerId { get; private set; }
    public Color PlayerColor { get; private set; }
    

    public HealthBar healthBarPrefab;


    public BobAnimateController bobController;


    int score;

    public void InitPlayer(int playerIndex, Color color)
    {
        PlayerId = playerIndex;
        PlayerColor = color;
        

        GetComponent<Renderer>().material.color = color;

        healthBar = Instantiate(healthBarPrefab, transform.position, healthBarPrefab.transform.rotation);
        healthBar.SetPlayer(this, color);

        bobController = GetComponentInChildren<BobAnimateController>();
    }

    private void Start()
    {
        SetPlayerControls(); 
    }

    void SetPlayerControls()
    {
        controlInputs = GetComponent<ControlInputs>();
        controlInputs.InitControls(PlayerId);
    }



    private void Update()
    {
        if(IsDead == false )
        {
            if (controlInputs.IsReady && dashAbility.IsDashing == false)
            {
                Move(controlInputs.InputVector.x, controlInputs.InputVector.y);
            }

            if(controlInputs.RightTriggerPressed == true)
            {
                if(Mathf.Abs(controlInputs.RightJoystickInputVector.y) > 0.8f || Mathf.Abs(controlInputs.RightJoystickInputVector.x) > 0.8f)
                    attackAbility.ActivateAttack();
            }

            if(controlInputs.LeftTriggerPressed == true)
            {
                if (controlInputs.InputVector != Vector2.zero)
                    dashAbility.ActivateDash();
            }

            CheckFall();
        }

        
    }


    void CheckFall()
    {
        if(transform.position.y <-10)
        {
            OnDeath();
        }
    }






}
