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

public class Character : MonoBehaviour
{
    [Header("基本属性")]
    public float maxHealth;
    public float currentHealth;

    [Header("受伤无敌")]
    public float invulnerableDuration;//无敌时间
    private float invulnerableCounter;//计数器
    public bool invulnerable;//无敌

    private void Start()
    {
        currentHealth = maxHealth;
    }


    private void Update()
    {
        if (invulnerable)
        {
            invulnerableCounter -= Time.deltaTime;
            if(invulnerableCounter <= 0)
            {
                invulnerable = false;
            }
        }
    }

    //接收伤害
    public void TakeDamage(Attack attacker)
    {
        if(invulnerable)
        {
            return;
        }
        if(currentHealth - attacker.damage > 0)//如果血量满足受伤就可以受伤
        {
            currentHealth -= attacker.damage;
            TriggerInvulnerable();
        }
        else
        {
            currentHealth = 0;//触发死亡
        }
       
    }

    //触发无敌
    private void TriggerInvulnerable()
    {
        if(!invulnerable)
        {
            invulnerable = true;
            invulnerableCounter = invulnerableDuration;
        }
    }
}
