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


public class Player2 : Player
{
    private Animator _ani;
    private float counter = 0;

    private void Start()
    {
        OnStart();
        _ani = GetComponent<Animator>();
    }
    
    void Update()
    {
        float h = Input.GetAxis("Horizontal_Left");
        float v = Input.GetAxis("Vertical_Left");
        _ani.SetFloat("MoveX", h);
        _ani.SetFloat("MoveY", v);
        transform.position += transform.right * h * speed * Time.deltaTime;
        transform.position -= transform.forward * v * speed * Time.deltaTime;
        float hangle = Input.GetAxis("Horizontal_Right");
        float vangle = Input.GetAxis("Vertical_Right");
        //Debug.Log("left: " + v + "right: " + vanglel);
        transform.Rotate(0, hangle*15, 0);
        currentPitch += vangle*2;
        currentPitch = Mathf.Clamp(currentPitch, -80, 80);
        Camera.localRotation = Quaternion.Euler(currentPitch, 0f, 0f);
        if (Input.GetKeyDown(KeyCode.JoystickButton0))
        {
            rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
            if (lastJump)
            {
                lastJump = false;
                Instantiate(BubblePrefab, transform.position - Vector3.up * 1, Quaternion.identity);
            }
        }
        if (Input.GetKeyDown(KeyCode.JoystickButton2) && lastShootTime >= 1)
        {
            Shoot();
            lastShootTime = 0;
        }
        if (lastShootTime < 1)
        {
            lastShootTime += Time.deltaTime;
        }
        OnUpdate();
    }
    private void OnMove()
    {
        
    }

    public override void TakeDamage(int hurt, Vector3 dir)
    {
        base.TakeDamage(hurt, dir);
        PlayerUI.Instance.SetHealth2(hurt);
    }

    private void OnTriggerStay(Collider other)
    {
        if (!Manager.Instance.begin) return;
        if (other.gameObject.layer == LayerMask.NameToLayer("Bottom"))
        {
            counter += Time.deltaTime;
            if (counter > 0.5f)
            {
                counter = 0;
                TakeDamage(1, Vector3.zero);
            }
        }
    }
}
