225 lines
9 KiB
C#
225 lines
9 KiB
C#
// Assets/_Project/Scripts/Gameplay/EnemyMovement.cs
|
|
using System.Collections.Generic;
|
|
using Unity.Netcode;
|
|
using UnityEngine;
|
|
using TD.Core;
|
|
using TD.Levels;
|
|
|
|
namespace TD.Gameplay
|
|
{
|
|
/// <summary>
|
|
/// Server-authoritative enemy movement along a dynamically computed A* path.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// <b>Initialization:</b> Call <see cref="InitializeServer"/> after <c>Instantiate</c>
|
|
/// and before <c>NetworkObject.Spawn()</c>. Provides the base move speed (from
|
|
/// <see cref="EnemyDefinition.MoveSpeed"/>) and the tile the enemy spawned on.
|
|
///
|
|
/// <b>Path lifecycle:</b>
|
|
/// <list type="bullet">
|
|
/// <item><see cref="OnNetworkSpawn"/> (server): queries <see cref="PathfindingService"/>
|
|
/// and stores the tile waypoint list.</item>
|
|
/// <item>Each frame: moves toward the world center of <c>remainingPath[0]</c>.
|
|
/// When within snap distance, pops the waypoint and checks for zone transitions.</item>
|
|
/// <item>When <see cref="PathfindingService.OnPathsInvalidated"/> fires (tower placed /
|
|
/// sold), <see cref="RecomputePath"/> reruns A* from the current tile.</item>
|
|
/// <item>When <c>remainingPath</c> is empty after a pop, the enemy has reached the
|
|
/// goal — <see cref="OnReachedGoal"/> fires and the enemy is despawned.</item>
|
|
/// </list>
|
|
///
|
|
/// <b>Zone leak tracking:</b> Each time a waypoint is popped, <see cref="LevelLoader.GetOwner"/>
|
|
/// is compared against <c>currentZone</c>. A change means the enemy has crossed a zone
|
|
/// boundary. <see cref="OnZoneLeaked"/> fires with the zone being LEFT so <c>WaveManager</c>
|
|
/// can debit the correct player's life pool.
|
|
///
|
|
/// <b>Speed:</b> Effective speed = <c>moveSpeed * EnemyStatus.GetSpeedMultiplier()</c>.
|
|
/// <c>EnemyStatus</c> replicates the multiplier as a NetworkVariable so movement looks
|
|
/// correct on all peers.
|
|
///
|
|
/// <b>Movement replication:</b> Requires a <c>NetworkTransform</c> on the prefab.
|
|
/// Position is written by the server; <c>NetworkTransform</c> interpolates on clients.
|
|
/// </remarks>
|
|
[RequireComponent(typeof(NetworkObject))]
|
|
[RequireComponent(typeof(EnemyHealth))]
|
|
[RequireComponent(typeof(EnemyStatus))]
|
|
public class EnemyMovement : NetworkBehaviour
|
|
{
|
|
// Snap threshold in world units. When sqrMagnitude to the waypoint center
|
|
// drops below this, we count the tile as reached and pop it.
|
|
private const float WaypointSnapSq = 0.04f; // 0.2 world units
|
|
|
|
// ----- Pre-spawn init (server-local) ----------------------------------
|
|
|
|
private float pendingMoveSpeed;
|
|
private Vector2Int pendingSpawnerTile;
|
|
private bool hasPendingInit;
|
|
|
|
// ----- Server-local runtime state -------------------------------------
|
|
|
|
private float moveSpeed;
|
|
private List<Vector2Int> remainingPath = new List<Vector2Int>();
|
|
private PlayerSlot currentZone = PlayerSlot.None;
|
|
private EnemyStatus status;
|
|
|
|
// ----- Events ---------------------------------------------------------
|
|
|
|
/// <summary>
|
|
/// Fired on the server when this enemy crosses from one player zone into another
|
|
/// (or from a neutral zone into a player zone). The argument is the zone being
|
|
/// LEFT — the zone that should be debited a life.
|
|
/// <c>WaveManager</c> subscribes to deduct from the correct player's life pool.
|
|
/// </summary>
|
|
public event System.Action<PlayerSlot> OnZoneLeaked;
|
|
|
|
/// <summary>
|
|
/// Fired on the server when the enemy reaches the goal tile.
|
|
/// Carries this component and the enemy's <see cref="EnemyHealth.LivesCost"/>
|
|
/// so <c>WaveManager</c> can deduct the right number of lives in one call.
|
|
/// The NetworkObject is despawned immediately after subscribers return.
|
|
/// </summary>
|
|
public event System.Action<EnemyMovement, int> OnReachedGoal;
|
|
|
|
// ----- Server-only pre-spawn init -------------------------------------
|
|
|
|
/// <summary>
|
|
/// Called by <c>WaveManager</c> on the server after <c>Instantiate</c> and
|
|
/// before <c>NetworkObject.Spawn()</c>. <paramref name="speed"/> comes from
|
|
/// <see cref="EnemyDefinition.MoveSpeed"/>; <paramref name="spawnerTile"/> is
|
|
/// the tile the enemy spawns on (used as the A* start node).
|
|
/// </summary>
|
|
public void InitializeServer(float speed, Vector2Int spawnerTile)
|
|
{
|
|
pendingMoveSpeed = speed;
|
|
pendingSpawnerTile = spawnerTile;
|
|
hasPendingInit = true;
|
|
}
|
|
|
|
// ----- NGO lifecycle --------------------------------------------------
|
|
|
|
public override void OnNetworkSpawn()
|
|
{
|
|
status = GetComponent<EnemyStatus>();
|
|
|
|
if (!IsServer) return;
|
|
|
|
if (!hasPendingInit)
|
|
{
|
|
Debug.LogError("[EnemyMovement] OnNetworkSpawn reached without InitializeServer " +
|
|
"having been called. Enemy will not move.");
|
|
return;
|
|
}
|
|
|
|
moveSpeed = pendingMoveSpeed;
|
|
|
|
// Resolve starting zone from the spawner tile.
|
|
var loader = LevelLoader.Instance;
|
|
if (loader != null)
|
|
currentZone = loader.GetOwner(pendingSpawnerTile);
|
|
|
|
// Compute the initial path from the spawn tile to the nearest goal.
|
|
ComputeAndStorePath(pendingSpawnerTile);
|
|
|
|
// Recompute when a tower is placed or sold.
|
|
if (PathfindingService.Instance != null)
|
|
PathfindingService.Instance.OnPathsInvalidated += RecomputePath;
|
|
}
|
|
|
|
public override void OnNetworkDespawn()
|
|
{
|
|
if (PathfindingService.Instance != null)
|
|
PathfindingService.Instance.OnPathsInvalidated -= RecomputePath;
|
|
}
|
|
|
|
// ----- Server update --------------------------------------------------
|
|
|
|
private void Update()
|
|
{
|
|
if (!IsServer) return;
|
|
if (remainingPath.Count == 0) return;
|
|
|
|
float effectiveSpeed = moveSpeed * (status != null ? status.GetSpeedMultiplier() : 1f);
|
|
Vector3 targetWorld = GridCoordinates.GridToWorld(remainingPath[0]);
|
|
Vector3 toTarget = targetWorld - transform.position;
|
|
|
|
if (toTarget.sqrMagnitude <= WaypointSnapSq)
|
|
{
|
|
// Snap to the tile center then handle the waypoint transition.
|
|
transform.position = targetWorld;
|
|
AdvanceWaypoint();
|
|
}
|
|
else
|
|
{
|
|
transform.position += toTarget.normalized * (effectiveSpeed * Time.deltaTime);
|
|
|
|
// Face the direction of travel.
|
|
if (toTarget.sqrMagnitude > 0.0001f)
|
|
transform.rotation = Quaternion.LookRotation(toTarget);
|
|
}
|
|
}
|
|
|
|
// ----- Path management ------------------------------------------------
|
|
|
|
private void AdvanceWaypoint()
|
|
{
|
|
Vector2Int arrivedTile = remainingPath[0];
|
|
remainingPath.RemoveAt(0);
|
|
|
|
CheckZoneTransition(arrivedTile);
|
|
|
|
if (remainingPath.Count == 0)
|
|
HandleGoalReached();
|
|
}
|
|
|
|
private void CheckZoneTransition(Vector2Int tile)
|
|
{
|
|
var loader = LevelLoader.Instance;
|
|
if (loader == null) return;
|
|
|
|
PlayerSlot newZone = loader.GetOwner(tile);
|
|
if (newZone == currentZone) return;
|
|
|
|
// The enemy is leaving currentZone — debit that player's life pool.
|
|
if (currentZone != PlayerSlot.None)
|
|
OnZoneLeaked?.Invoke(currentZone);
|
|
|
|
currentZone = newZone;
|
|
}
|
|
|
|
private void HandleGoalReached()
|
|
{
|
|
var health = GetComponent<EnemyHealth>();
|
|
int livesCost = health != null ? health.LivesCost : 1;
|
|
|
|
OnReachedGoal?.Invoke(this, livesCost);
|
|
NetworkObject.Despawn();
|
|
}
|
|
|
|
// ----- Path invalidation ----------------------------------------------
|
|
|
|
// Called on server when LevelLoader.OnWalkabilityChanged fires (tower placed/sold).
|
|
private void RecomputePath()
|
|
{
|
|
if (!IsServer) return;
|
|
|
|
// Recompute from the enemy's current tile position.
|
|
Vector2Int currentTile = GridCoordinates.WorldToGrid(transform.position);
|
|
ComputeAndStorePath(currentTile);
|
|
}
|
|
|
|
private void ComputeAndStorePath(Vector2Int fromTile)
|
|
{
|
|
var service = PathfindingService.Instance;
|
|
if (service == null)
|
|
{
|
|
Debug.LogWarning("[EnemyMovement] PathfindingService not found. Enemy will not move.");
|
|
return;
|
|
}
|
|
|
|
remainingPath = service.ComputePath(fromTile);
|
|
|
|
if (remainingPath.Count == 0)
|
|
Debug.LogWarning($"[EnemyMovement] No path found from {fromTile}. " +
|
|
"TowerPlacementManager should have prevented a full block.");
|
|
}
|
|
}
|
|
}
|