내일배움캠프/TIL

[내배캠][Unity6기][TIL] 힐러 타입 인간 추가

binary는 호남선 2025. 1. 3. 21:10

인간 캐릭터에 능력이 있으면 좋겠다는 의견이 나와 IHealer 인터페이스를 상속하는 힐러 타입의 인간을 추가했다.

기본 능력치 기반으로 쿨타임과 힐 스탯을 설정하고 일정 범위 내 인간들을 힐 하는 코드를 작성했다.

using System.Collections;
using UnityEngine;

public interface IHealer
{
    void HealFear(float amount);
}

public class HealerHuman : Human, IHealer
{
    private float _healAmount;
    private float _healRadius;
    private float _healDelay;
    private Coroutine _healCoroutine;

    protected override void Awake()
    {
        base.Awake();
        _healRadius = 2;
        _healAmount = base.controller.MaxFatigueInflicted * 0.8f;
        _healDelay = base.controller.Cooldown * 0.5f;
    }
    
    protected override void OnEnable()
    {
        base.OnEnable();
        if (_healCoroutine == null)
        {
            _healCoroutine = StartCoroutine(HealingCoroutine());
        }
    }
    
    public void HealFear(float amount)
    {
        if (isReturning) return;
        
        Collider2D[] humansInRange = Physics2D.OverlapCircleAll(transform.position, _healRadius);
        foreach (Collider2D collider in humansInRange)
        {
            Human otherHuman = collider.GetComponent<Human>();
            if (otherHuman != null && !otherHuman.isReturning)
            {
                otherHuman.DecreaseFear(amount);
            }
        }
    }
    
    private IEnumerator HealingCoroutine()
    {
        while (true)
        {
            HealFear(_healAmount);
            yield return new WaitForSeconds(_healDelay);
        }
    }

    protected override void OnDisable()
    {
        base.OnDisable();
        if (_healCoroutine != null)
        {
            StopCoroutine(_healCoroutine);
            _healCoroutine = null;
        }
    }
}