UnityTowerDefense/Assets/_Project/Scripts/Gameplay/EnemyAbilities/BlinkAbilityDefinition.cs

59 lines
2.5 KiB
C#

// Assets/_Project/Scripts/Gameplay/EnemyAbilities/BlinkAbilityDefinition.cs
using UnityEngine;
using TD.Core;
namespace TD.Gameplay.EnemyAbilities
{
/// <summary>
/// Every few seconds, the enemy teleports a short distance further along its path — skipping
/// tiles, and the towers covering them.
/// </summary>
/// <remarks>
/// 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
/// <c>EnemyMovement.ServerBlinkForward</c>.
///
/// <para>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.</para>
/// </remarks>
[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 = 4f;
[Tooltip("World units to advance along the path per blink.")]
[Min(0.1f)]
public float BlinkDistanceMeters = 4f;
[Tooltip("Random spread (seconds) added to each enemy's first blink so a wave doesn't " +
"blink in one synchronized block. Applied once, at spawn.")]
[Min(0f)]
public float StartJitterSeconds = 1.5f;
public override void ServerOnSpawn(EnemyAbility instance, int abilityIndex)
{
// Seed each enemy's cooldown with a different negative offset so the wave's first
// blink is staggered. 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.
if (StartJitterSeconds > 0f)
instance.TimerFor(abilityIndex) = -Random.Range(0f, StartJitterSeconds);
}
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<EnemyMovement>()?.ServerBlinkForward(BlinkDistanceMeters);
}
}
}