// Assets/_Project/Scripts/Gameplay/EnemyAbilities/BlinkAbilityDefinition.cs
using UnityEngine;
using TD.Core;
namespace TD.Gameplay.EnemyAbilities
{
///
/// Every few seconds, the enemy teleports a short distance further along its path — skipping
/// tiles, and the towers covering them.
///
///
/// Blinks follow the path rather than cutting toward the goal, so the maze still shapes the
/// route; the buff shortens time-under-fire rather than bypassing walls. See
/// EnemyMovement.ServerBlinkForward.
///
/// The cooldown lives in this enemy's own timer slot rather than on the asset. A field
/// here would be shared by every enemy carrying the buff, so the whole wave would blink in
/// perfect unison — which looks like a bug even when the timing is right.
///
[CreateAssetMenu(fileName = "BlinkAbility", menuName = "TD/Enemy Abilities/Blink")]
public class BlinkAbilityDefinition : EnemyAbilityDefinition
{
public override EnemyAbilityKind Kind => EnemyAbilityKind.Blink;
[Header("Blink")]
[Tooltip("Seconds between blinks.")]
[Min(0.1f)]
public float IntervalSeconds = 8f;
[Tooltip("World units to advance along the path per blink.")]
[Min(0.1f)]
public float BlinkDistanceMeters = 4f;
public override void ServerOnSpawn(EnemyAbility instance, int abilityIndex)
{
// Seed each enemy's cooldown with a random point within the full interval, as if it
// spawned already partway through a cooldown. A small jitter window near zero would
// still let a burst of enemies spawned together sync up and blink as a visible cluster
// every cycle; spreading across the whole interval avoids that regardless of how
// IntervalSeconds is tuned. Done once, here, rather than on the first tick — a tick-time
// check would have to distinguish "never started" from "just blinked", and both
// states read as a zero timer.
instance.TimerFor(abilityIndex) = -Random.Range(0f, IntervalSeconds);
}
public override void ServerTick(EnemyAbility instance, int abilityIndex, float dt)
{
ref float timer = ref instance.TimerFor(abilityIndex);
timer += dt;
if (timer < IntervalSeconds) return;
timer = 0f;
instance.GetComponent()?.ServerBlinkForward(BlinkDistanceMeters);
}
}
}