인간 캐릭터에 능력이 있으면 좋겠다는 의견이 나와 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;
}
}
}'내일배움캠프 > TIL' 카테고리의 다른 글
| [내배캠][Unity6기][TIL] 다중 스폰 포인트 (0) | 2025.01.07 |
|---|---|
| [내배캠][Unity6기][TIL] ObjectDisposedException 트러블슈팅 (0) | 2025.01.06 |
| [내배캠][Unity6기][TIL] 데이터 로드 리팩토링 (2) (0) | 2024.12.31 |
| [내배캠][Unity6기][TIL] 데이터 로드 리팩토링 (0) | 2024.12.30 |
| [내배캠][Unity6기][TIL] 동적 생성 메서드 경로 설정 리팩토링 (0) | 2024.12.27 |