EerieVillage/Assets/Scripts/Skills/Effectors/HomingProjectile.cs

64 lines
2.0 KiB
C#
Raw Normal View History

using UnityEngine;
using Platformer.Mechanics;
namespace EerieVillage.Skills.Effectors
{
/// <summary>
/// 유도 투사체. A15 추적 화염구 전용.
/// Projectile 파생 — Update를 FixedUpdate 기반 방향 보정으로 확장.
/// BT12-Dev Phase 2-B §4-3.
/// </summary>
public class HomingProjectile : Projectile
{
private Transform _target;
private float _homingStrength = 5f;
public override void Initialize(ActiveSkillRuntime runtime, PlayerSkillInventory inventory, Vector2 direction)
{
base.Initialize(runtime, inventory, direction);
_target = FindNearestEnemy();
}
protected override void Update()
{
// 타겟 갱신 (사망·삭제 시 재탐색)
if (_target == null || !_target.gameObject.activeInHierarchy)
{
_target = FindNearestEnemy();
}
if (_target != null)
{
Vector2 toTarget = ((Vector2)_target.position - (Vector2)transform.position).normalized;
_direction = Vector2.Lerp(_direction, toTarget, _homingStrength * Time.deltaTime).normalized;
}
// 부모 Update — 실제 이동
base.Update();
}
private Transform FindNearestEnemy()
{
var enemies = Object.FindObjectsByType<EnemyController>(FindObjectsSortMode.None);
Transform nearest = null;
float minDist = float.MaxValue;
foreach (var e in enemies)
{
if (e == null) continue;
var hp = e.GetComponent<Health>();
if (hp == null || !hp.IsAlive) continue;
float d = Vector2.Distance(transform.position, e.transform.position);
if (d < minDist)
{
minDist = d;
nearest = e.transform;
}
}
return nearest;
}
}
}