Files
Colosseum/Assets/_Game/Scripts/AI/BehaviorActions/Actions/UsePatternAction.cs
dal4segno abfc43ae76 fix: 플레이어 접촉 이동과 타깃 표면 추적 보정
- TargetSurfaceUtility를 추가해 플레이어와 적의 실제 충돌 표면 기준으로 거리, 방향, 목적지를 계산

- 플레이어 이동과 적 루트모션, 추적 로직에서 접촉 시 수평 이동을 제한해 겹침과 밀어내기 문제를 완화

- 드로그 AI 거리 판정 노드들이 표면 거리 기준을 사용하도록 맞춰 사거리 분기 오차를 줄임
2026-04-09 23:22:28 +09:00

255 lines
8.1 KiB
C#

using System;
using Colosseum;
using Colosseum.AI;
using Colosseum.Combat;
using Colosseum.Enemy;
using Colosseum.Skills;
using Unity.Behavior;
using Unity.Properties;
using UnityEngine;
using Action = Unity.Behavior.Action;
/// <summary>
/// 보스 패턴을 실행하는 Behavior Tree Action.
/// 패턴 내 스텝(스킬 또는 대기)을 순서대로 실행하며, 패턴 쿨타임을 관리합니다.
/// </summary>
[Serializable, GeneratePropertyBag]
[NodeDescription(name: "Use Pattern", story: "[Pattern] ", category: "Action", id: "a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6")]
public partial class UsePatternAction : Action
{
[SerializeReference] public BlackboardVariable<BossPatternData> Pattern;
[SerializeReference] public BlackboardVariable<GameObject> Target;
private static readonly System.Collections.Generic.Dictionary<string, float> patternReadyTimes =
new System.Collections.Generic.Dictionary<string, float>();
private SkillController skillController;
private int currentStepIndex;
private float waitEndTime;
private bool isWaiting;
private bool isSkillStepExecuting;
protected override Status OnStart()
{
if (Pattern?.Value == null)
{
Debug.LogWarning("[UsePatternAction] 패턴이 null입니다.");
return Status.Failure;
}
if (!IsPatternReady(GameObject, Pattern.Value))
{
LogDebug($"쿨다운 중: {Pattern.Value.PatternName}");
return Status.Failure;
}
if (Pattern.Value.Steps.Count == 0)
{
LogDebug("스텝이 비어 있는 패턴이라 실패합니다.");
return Status.Failure;
}
if (RequiresJumpTarget(Pattern.Value) && ResolveJumpTarget() == null)
{
LogDebug($"점프 타겟을 찾지 못해 실패: {Pattern.Value.PatternName}");
return Status.Failure;
}
skillController = GameObject.GetComponent<SkillController>();
if (skillController == null)
{
Debug.LogWarning($"[UsePatternAction] SkillController를 찾을 수 없습니다: {GameObject.name}");
return Status.Failure;
}
currentStepIndex = 0;
isWaiting = false;
isSkillStepExecuting = false;
return ExecuteCurrentStep();
}
protected override Status OnUpdate()
{
if (skillController == null)
return Status.Failure;
if (isWaiting)
{
if (Time.time < waitEndTime)
return Status.Running;
isWaiting = false;
}
else
{
if (isSkillStepExecuting)
{
if (skillController.IsPlayingAnimation)
return Status.Running;
isSkillStepExecuting = false;
if (skillController.LastExecutionResult != SkillExecutionResult.Completed)
return Status.Failure;
}
else if (skillController.IsPlayingAnimation)
return Status.Running;
}
currentStepIndex++;
if (currentStepIndex >= Pattern.Value.Steps.Count)
{
MarkPatternUsed(GameObject, Pattern.Value);
return Status.Success;
}
return ExecuteCurrentStep();
}
protected override void OnEnd()
{
skillController = null;
isSkillStepExecuting = false;
}
private Status ExecuteCurrentStep()
{
PatternStep step = Pattern.Value.Steps[currentStepIndex];
if (step.Type == PatternStepType.Wait)
{
isWaiting = true;
waitEndTime = Time.time + step.Duration;
return Status.Running;
}
// PatternStepType.Skill
if (step.Skill == null)
{
Debug.LogWarning($"[UsePatternAction] 스킬이 null입니다. (index {currentStepIndex})");
return Status.Failure;
}
GameObject jumpTarget = null;
if (step.Skill.JumpToTarget)
{
jumpTarget = ResolveJumpTarget();
if (jumpTarget == null)
{
return Status.Failure;
}
}
GameObject skillTarget = step.Skill.JumpToTarget ? jumpTarget : Target?.Value;
bool success = skillTarget != null
? skillController.ExecuteSkill(step.Skill, skillTarget)
: skillController.ExecuteSkill(step.Skill);
if (!success)
{
Debug.LogWarning($"[UsePatternAction] 스킬 실행 실패: {step.Skill.SkillName} (index {currentStepIndex})");
return Status.Failure;
}
isSkillStepExecuting = true;
LogDebug($"패턴 실행: {Pattern.Value.PatternName} / Step={currentStepIndex} / Skill={step.Skill.SkillName}");
// jumpToTarget 스킬이면 타겟 위치 전달
if (step.Skill.JumpToTarget)
{
GameObject.GetComponent<EnemyBase>()?.SetJumpTarget(jumpTarget.transform.position);
}
return Status.Running;
}
/// <summary>
/// 점프 대상이 근접 상태라면, 더 멀리 있는 유효 타겟으로 재선택합니다.
/// 도약 패턴이 전방 탱커 대신 원거리 플레이어를 징벌할 수 있도록 합니다.
/// </summary>
private GameObject ResolveJumpTarget()
{
GameObject currentTarget = Target?.Value;
EnemyBase enemyBase = GameObject.GetComponent<EnemyBase>();
float maxJumpDistance = enemyBase?.Data != null ? enemyBase.Data.AggroRange : 20f;
return IsValidJumpTarget(currentTarget, maxJumpDistance) ? currentTarget : null;
}
/// <summary>
/// 점프 타겟의 생존 여부, 팀, 거리 조건을 검사합니다.
/// </summary>
private bool IsValidJumpTarget(GameObject candidate, float maxDistance)
{
if (candidate == null || !candidate.activeInHierarchy)
return false;
if (Team.IsSameTeam(GameObject, candidate))
return false;
IDamageable damageable = candidate.GetComponent<IDamageable>();
if (damageable != null && damageable.IsDead)
return false;
float distance = TargetSurfaceUtility.GetHorizontalSurfaceDistance(GameObject.transform.position, candidate);
return distance <= maxDistance;
}
/// <summary>
/// 패턴 안에 점프 타겟이 필요한 스텝이 있는지 확인합니다.
/// </summary>
private static bool RequiresJumpTarget(BossPatternData pattern)
{
if (pattern == null || pattern.Steps == null)
return false;
for (int i = 0; i < pattern.Steps.Count; i++)
{
PatternStep step = pattern.Steps[i];
if (step.Type == PatternStepType.Skill && step.Skill != null && step.Skill.JumpToTarget)
return true;
}
return false;
}
/// <summary>
/// 동일한 보스 오브젝트가 같은 패턴을 다시 사용할 수 있는지 확인합니다.
/// </summary>
public static bool IsPatternReady(GameObject owner, BossPatternData pattern)
{
if (owner == null || pattern == null)
return false;
string cooldownKey = BuildCooldownKey(owner, pattern);
if (!patternReadyTimes.TryGetValue(cooldownKey, out float readyTime))
return true;
return Time.time >= readyTime;
}
/// <summary>
/// 패턴 사용 직후 다음 사용 가능 시점을 기록합니다.
/// </summary>
public static void MarkPatternUsed(GameObject owner, BossPatternData pattern)
{
if (owner == null || pattern == null)
return;
string cooldownKey = BuildCooldownKey(owner, pattern);
patternReadyTimes[cooldownKey] = Time.time + pattern.Cooldown;
}
private static string BuildCooldownKey(GameObject owner, BossPatternData pattern)
{
return $"{owner.GetInstanceID()}_{pattern.GetInstanceID()}";
}
private void LogDebug(string message)
{
BossBehaviorRuntimeState context = GameObject.GetComponent<BossBehaviorRuntimeState>();
context?.LogDebug(nameof(UsePatternAction), message);
}
}