Files
beyond/Assets/AI/_Summoner/SA_SpawnMinions.cs
SzymonMis 06f9c7349d Summoner
2026-02-19 21:34:07 +01:00

165 lines
4.7 KiB
C#

using Invector.vCharacterController.AI.FSMBehaviour;
using UnityEngine;
namespace DemonBoss.Summoner
{
/// <summary>
/// FSM State Action that triggers minion spawning
/// Calls SummonerAI.StartSpawning() on state enter
/// </summary>
[CreateAssetMenu(menuName = "Invector/FSM/Actions/Summoner/Spawn Minions")]
public class SA_SpawnMinions : vStateAction
{
public override string categoryName => "Summoner";
public override string defaultName => "Spawn Minions";
[Header("Animation")]
[Tooltip("Animator trigger parameter for summoning animation")]
public string summonTriggerName = "Summon";
[Tooltip("Animator bool for summoning state")]
public string summoningBoolName = "IsSummoning";
[Header("Behavior")]
[Tooltip("Wait for spawning to complete before allowing state exit")]
public bool waitForCompletion = true;
[Tooltip("If true, spawn starts only after an animation event calls SummonerAI.OnSummonAnimationEvent")]
public bool spawnOnAnimationEvent = true;
[Tooltip("Fallback: start spawn if animation event doesn't fire within this time (seconds). 0 = no fallback.")]
public float animationEventTimeout = 1.2f;
[Header("Debug")]
[Tooltip("Enable debug logging")]
public bool enableDebug = false;
private SummonerAI summoner;
private Animator animator;
private bool hasStartedSpawning = false;
private float spawnRequestTime = -1f;
private bool spawnTriggered = false;
public override void DoAction(vIFSMBehaviourController fsmBehaviour, vFSMComponentExecutionType executionType = vFSMComponentExecutionType.OnStateUpdate)
{
if (executionType == vFSMComponentExecutionType.OnStateEnter)
{
OnEnter(fsmBehaviour);
}
else if (executionType == vFSMComponentExecutionType.OnStateUpdate)
{
OnUpdate(fsmBehaviour);
}
else if (executionType == vFSMComponentExecutionType.OnStateExit)
{
OnExit(fsmBehaviour);
}
}
private void OnEnter(vIFSMBehaviourController fsmBehaviour)
{
summoner = fsmBehaviour.gameObject.GetComponent<SummonerAI>();
animator = fsmBehaviour.gameObject.GetComponent<Animator>();
if (summoner == null)
{
Debug.LogError("[SA_SpawnMinions] No SummonerAI component found!");
return;
}
// Trigger animation
if (animator != null)
{
if (!string.IsNullOrEmpty(summonTriggerName))
{
animator.SetTrigger(summonTriggerName);
}
if (!string.IsNullOrEmpty(summoningBoolName))
{
animator.SetBool(summoningBoolName, true);
}
}
if (spawnOnAnimationEvent)
{
summoner.RequestSpawn();
hasStartedSpawning = true;
spawnTriggered = false;
spawnRequestTime = Time.time;
if (enableDebug) Debug.Log("[SA_SpawnMinions] Waiting for animation event to spawn");
}
else
{
// Start spawning minions immediately
summoner.StartSpawning();
hasStartedSpawning = true;
spawnTriggered = true;
if (enableDebug) Debug.Log("[SA_SpawnMinions] Started spawning minions");
}
}
private void OnUpdate(vIFSMBehaviourController fsmBehaviour)
{
if (summoner != null && spawnOnAnimationEvent && !spawnTriggered)
{
if (summoner.IsSpawning)
{
spawnTriggered = true;
}
else if (animationEventTimeout > 0f && spawnRequestTime > 0f && (Time.time - spawnRequestTime) >= animationEventTimeout)
{
summoner.StartSpawning();
spawnTriggered = true;
if (enableDebug) Debug.Log("[SA_SpawnMinions] Animation event timeout - spawning fallback");
}
}
// If waiting for completion, keep state active until spawning is done
if (waitForCompletion && summoner != null && summoner.IsSpawning)
{
// State will continue until spawning is complete
if (enableDebug && Time.frameCount % 60 == 0) // Log once per second
{
Debug.Log("[SA_SpawnMinions] Waiting for spawning to complete...");
}
}
}
private void OnExit(vIFSMBehaviourController fsmBehaviour)
{
// Reset animation bool
if (animator != null && !string.IsNullOrEmpty(summoningBoolName))
{
animator.SetBool(summoningBoolName, false);
}
// If spawning was interrupted, stop it
if (summoner != null && summoner.IsSpawning)
{
summoner.StopSpawning();
if (enableDebug) Debug.Log("[SA_SpawnMinions] Spawning interrupted on state exit");
}
else if (summoner != null)
{
summoner.CancelSpawnRequest();
}
hasStartedSpawning = false;
spawnTriggered = false;
spawnRequestTime = -1f;
if (enableDebug) Debug.Log("[SA_SpawnMinions] State exited");
}
/// <summary>
/// Check if spawning is complete (for FSM decision nodes)
/// </summary>
public bool IsSpawningComplete()
{
if (summoner == null) return true;
return hasStartedSpawning && !summoner.IsSpawning;
}
}
}