miepzerino
2023-12-16 b1ad1febe26938a97bb414620c49b580caf58336
Assets/Scripts/Damageable.cs
@@ -1,15 +1,19 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Damageable : MonoBehaviour
{
    // ONLY FOR DEBUG USE
    [SerializeField]
    private bool selfDamage = false;
    Animator animator;
    [SerializeField]
    private int _maxHealth = 100;
    [SerializeField]
    private int _health;
    private int _health = 100;
    [SerializeField]
    private bool _isAlive = true;
    [SerializeField]
@@ -27,7 +31,14 @@
        get { return _health; }
        set
        {
            _health = value;
            if (value > MaxHealth)
            {
                _health = MaxHealth;
            }
            else
            {
                _health = value;
            }
            if (value <= 0)
            {
                IsAlive = false;
@@ -46,7 +57,7 @@
    private void Awake()
    {
        Health = MaxHealth;
        //Health = MaxHealth;
        animator = GetComponent<Animator>();
    }
    private void Update()
@@ -63,14 +74,37 @@
                timeSinceHit += Time.deltaTime;
            }
        }
        if (selfDamage)
        {
            Hit(10);
        }
    }
    public void Hit(int damage)
    {
        if (IsAlive && !isInvincible)
        {
            Health -= damage;
            int actualDamageAmount = Mathf.Min(damage, Health);
            Health -= actualDamageAmount;
            isInvincible = true;
            CharacterEvents.characterDamaged.Invoke(gameObject, actualDamageAmount);
        }
    }
    public bool Heal(int healAmount)
    {
        bool result = false;
        if (IsAlive && Health < MaxHealth)
        {
            int actualHealAmount = Mathf.Min(healAmount, MaxHealth - Health);
            Health += actualHealAmount;
            CharacterEvents.characterHealed.Invoke(gameObject, actualHealAmount);
            result = true;
        }
        return result;
    }
}