// Assets/_Project/Scripts/Gameplay/EnemyStatus.cs
using System.Collections.Generic;
using Unity.Netcode;
using UnityEngine;
using TD.Core;
namespace TD.Gameplay
{
///
/// A single active lingering effect on an enemy.
///
///
/// Magnitude semantics by source:
///
/// - Cold — fraction of speed retained (0.5 = half speed)
/// - Fire — damage per second applied as a DoT tick
/// - Poison — damage per second applied as a DoT tick
/// - Others — unused (magnitude = 0)
///
///
public struct StatusEffect
{
public DamageType Source;
public float Magnitude;
public float RemainingDuration;
}
///
/// Tracks and ticks lingering status effects (slow, burn, poison) on an enemy.
///
///
/// Authority: The active-effect list is server-local (not replicated).
/// Only the derived NetworkVariable is replicated,
/// so EnemyMovement (Phase 1.5/1.6) can scale speed on all peers without
/// re-broadcasting the full effect list.
///
/// Stacking rule: A second hit of the same refreshes
/// the duration and magnitude rather than stacking. Cross-type interactions (e.g.
/// Cold + Fire) are not yet implemented; is the hook for
/// when that design is worked out.
///
/// DoT damage is applied by calling each
/// tick so resistance lookups remain in one place.
///
[RequireComponent(typeof(NetworkObject))]
public class EnemyStatus : NetworkBehaviour
{
// Replicated so EnemyMovement can read it on all clients without
// knowing anything about which effects are active.
private readonly NetworkVariable speedMultiplier = new NetworkVariable(
1f,
NetworkVariableReadPermission.Everyone,
NetworkVariableWritePermission.Server);
// Server-local — only the derived speedMultiplier NV crosses the wire.
private readonly List activeEffects = new List();
// Resolved once; used by Tick for DoT TakeDamage calls.
private EnemyHealth health;
// ----- NGO lifecycle -----------------------------------------------
public override void OnNetworkSpawn()
{
health = GetComponent();
}
// ----- Public API --------------------------------------------------
/// Current speed fraction (0–1). 1 = full speed, 0.5 = half speed, etc.
public float GetSpeedMultiplier() => speedMultiplier.Value;
/// True if an effect of the given type is currently active on this enemy.
public bool HasEffect(DamageType type)
{
for (int i = 0; i < activeEffects.Count; i++)
if (activeEffects[i].Source == type) return true;
return false;
}
///
/// Applies or refreshes a lingering effect. Server-only; no-op on clients.
/// Re-hitting with the same damage type refreshes duration and magnitude.
///
public void ApplyEffect(DamageType source, float magnitude, float duration)
{
if (!IsServer) return;
for (int i = 0; i < activeEffects.Count; i++)
{
if (activeEffects[i].Source != source) continue;
var e = activeEffects[i];
e.Magnitude = magnitude;
e.RemainingDuration = duration;
activeEffects[i] = e;
RecalculateSpeedMultiplier();
return;
}
activeEffects.Add(new StatusEffect
{
Source = source,
Magnitude = magnitude,
RemainingDuration = duration,
});
RecalculateSpeedMultiplier();
}
// ----- Server tick ------------------------------------------------
private void Update()
{
if (!IsServer || activeEffects.Count == 0) return;
TickEffects(Time.deltaTime);
}
private void TickEffects(float dt)
{
bool anyExpired = false;
for (int i = activeEffects.Count - 1; i >= 0; i--)
{
var e = activeEffects[i];
// Apply DoT for Fire and Poison.
if (e.Source == DamageType.Fire || e.Source == DamageType.Poison)
{
if (health != null && !health.IsDead)
health.TakeDamage(e.Magnitude * dt, e.Source);
}
e.RemainingDuration -= dt;
if (e.RemainingDuration <= 0f)
{
activeEffects.RemoveAt(i);
anyExpired = true;
}
else
{
activeEffects[i] = e;
}
}
if (anyExpired)
RecalculateSpeedMultiplier();
}
private void RecalculateSpeedMultiplier()
{
float mult = 1f;
for (int i = 0; i < activeEffects.Count; i++)
{
if (activeEffects[i].Source == DamageType.Cold)
mult = Mathf.Min(mult, activeEffects[i].Magnitude);
}
speedMultiplier.Value = mult;
}
}
}