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

public class Player1 : Player
{
    // Update is called once per frame
    private Animator _ani;
    private float counter = 0;

    private void Start()
    {
        OnStart();
        _ani = GetComponent<Animator>();
    }
    private void Update()
    {
        if (!Manager.Instance.begin) return;

        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        _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("Mouse X");
        float vangle = Input.GetAxis("Mouse Y");
        transform.Rotate(0, hangle, 0);
        currentPitch -= vangle;
        currentPitch = Mathf.Clamp(currentPitch, -80, 80);
        Camera.localRotation = Quaternion.Euler(currentPitch, 0f, 0f);

        if (Input.GetKeyDown(KeyCode.Space))
        {
            rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
            if (lastJump)
            {
                lastJump = false;
                Instantiate(BubblePrefab, transform.position-Vector3.up*1, Quaternion.identity);
            }
        }
        if (Input.GetMouseButtonDown(0) && lastShootTime >= 1)
        {
            Shoot();
            lastShootTime = 0;
        }
        if(lastShootTime < 1)
        {
            lastShootTime += Time.deltaTime;
        }

        OnUpdate();
    }

    public override void TakeDamage(int hurt, Vector3 dir)
    {
        base.TakeDamage(hurt, dir);
        PlayerUI.Instance.SetHealth1(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);
            }
        }
    }
}
