Replaces the bots-only MVP scene with a real server-authoritative combat loop driven over the relay transport shipped last round. MatchHostSimulator validates deploys, spends elixir, spawns units, runs AutoUnitAI targeting and attack resolution against real nearest-enemy selection, resolves unit/tower damage, and drives the win-condition clock, broadcasting a state_snapshot at 10Hz. MatchSnapshotRenderer applies those snapshots on the guest without running a second simulation. MatchSimulationController ties it to NetworkBootstrap's host/guest role and is the one entry point (RequestLocalDeploy) the HUD needs for card placement.
49 lines
1.6 KiB
C#
49 lines
1.6 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
namespace Siegebreak.Units
|
|
{
|
|
/// <summary>
|
|
/// Server-authoritative troop HP, same shape as
|
|
/// Core/TowerController so combat resolution code (host simulator)
|
|
/// can treat a unit and a tower identically once it has a target's
|
|
/// health component. The client (guest) never calls
|
|
/// <see cref="ApplyDamage"/> directly — it only reconciles
|
|
/// <see cref="CurrentHp"/> from the host's state_snapshot via
|
|
/// <see cref="SyncFromSnapshot"/>.
|
|
/// </summary>
|
|
public class UnitHealth : MonoBehaviour
|
|
{
|
|
[SerializeField] private int maxHp = 100;
|
|
|
|
public int MaxHp => maxHp;
|
|
public int CurrentHp { get; private set; }
|
|
public bool IsDestroyed => CurrentHp <= 0;
|
|
|
|
public event Action<UnitHealth> OnDestroyed;
|
|
|
|
public void Initialize(int startingMaxHp)
|
|
{
|
|
maxHp = startingMaxHp;
|
|
CurrentHp = startingMaxHp;
|
|
}
|
|
|
|
// Host-side combat resolution only.
|
|
public void ApplyDamage(int amount)
|
|
{
|
|
if (IsDestroyed) return;
|
|
CurrentHp = Mathf.Max(0, CurrentHp - amount);
|
|
if (CurrentHp == 0) OnDestroyed?.Invoke(this);
|
|
}
|
|
|
|
// Guest-side reconciliation: the host already decided the HP, this
|
|
// just mirrors it for rendering (health bars, destroy VFX).
|
|
public void SyncFromSnapshot(int hp)
|
|
{
|
|
var wasDestroyed = IsDestroyed;
|
|
CurrentHp = Mathf.Max(0, hp);
|
|
if (!wasDestroyed && IsDestroyed) OnDestroyed?.Invoke(this);
|
|
}
|
|
}
|
|
}
|