using System;
using Colosseum.AI;
using Colosseum.Skills;
using Unity.Behavior;
using Unity.Properties;
using UnityEngine;
using Action = Unity.Behavior.Action;
///
/// 보스 패턴을 실행하는 Behavior Tree Action.
/// 패턴 내 스텝(스킬 또는 대기)을 순서대로 실행하며, 패턴 쿨타임을 관리합니다.
///
[Serializable, GeneratePropertyBag]
[NodeDescription(name: "Use Pattern", story: "[Pattern] 실행", category: "Action", id: "a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6")]
public partial class UsePatternAction : Action
{
[SerializeReference] public BlackboardVariable Pattern;
private SkillController skillController;
private int currentStepIndex;
private float waitEndTime;
private bool isWaiting;
private float lastUsedTime = float.MinValue;
protected override Status OnStart()
{
if (Pattern?.Value == null)
{
Debug.LogWarning("[UsePatternAction] 패턴이 null입니다.");
return Status.Failure;
}
if (Time.time - lastUsedTime < Pattern.Value.Cooldown)
{
return Status.Failure;
}
if (Pattern.Value.Steps.Count == 0)
{
return Status.Failure;
}
skillController = GameObject.GetComponent();
if (skillController == null)
{
Debug.LogWarning($"[UsePatternAction] SkillController를 찾을 수 없습니다: {GameObject.name}");
return Status.Failure;
}
currentStepIndex = 0;
isWaiting = 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 (skillController.IsPlayingAnimation)
return Status.Running;
}
currentStepIndex++;
if (currentStepIndex >= Pattern.Value.Steps.Count)
{
lastUsedTime = Time.time;
return Status.Success;
}
return ExecuteCurrentStep();
}
protected override void OnEnd()
{
skillController = null;
}
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;
}
bool success = skillController.ExecuteSkill(step.Skill);
if (!success)
{
Debug.LogWarning($"[UsePatternAction] 스킬 실행 실패: {step.Skill.SkillName} (index {currentStepIndex})");
return Status.Failure;
}
return Status.Running;
}
}