siegebreak/client/Assets/Scripts/Core/TowerController.cs
caelum0x 6172e21cfe feat: wire the MVP combat loop to the real PvP relay transport
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.
2026-10-24 00:34:05 +03:00

60 lines
2.0 KiB
C#

using System;
using UnityEngine;
namespace Siegebreak.Core
{
public enum TowerSlot { King, LeftPrincess, RightPrincess }
/// <summary>
/// Server-authoritative tower HP + destruction. Auto-units target
/// towers the same way they target each other (see AutoUnitAI) —
/// a tower is just an immobile, undying-until-it-isn't unit for
/// targeting purposes.
/// </summary>
public class TowerController : MonoBehaviour
{
[SerializeField] private TowerSlot slot;
[SerializeField] private int maxHp = 2400;
[SerializeField] private float attackRange = 7f;
[SerializeField] private int dps = 50;
public TowerSlot Slot => slot;
public int CurrentHp { get; private set; }
public bool IsDestroyed => CurrentHp <= 0;
public int Dps => dps;
public float AttackRange => attackRange;
public event Action<TowerController> OnDestroyed;
private void Awake() => CurrentHp = maxHp;
// Called server-side only. Princess towers unlock the king tower's
// targeting/aggro once both fall on one side — that coupling lives
// in MatchStateController, not here; this class only knows about
// its own HP.
public void ApplyDamage(int amount)
{
if (IsDestroyed) return;
CurrentHp = Mathf.Max(0, CurrentHp - amount);
if (CurrentHp == 0)
{
OnDestroyed?.Invoke(this);
}
}
public bool IsInRange(Vector3 attackerPosition) =>
Vector3.Distance(transform.position, attackerPosition) <= attackRange;
// Guest-side reconciliation only: the host already resolved combat,
// this just mirrors the resulting HP for rendering. Mirrors
// Units/UnitHealth.SyncFromSnapshot.
public void SyncFromSnapshot(int hp)
{
var wasDestroyed = IsDestroyed;
CurrentHp = Mathf.Max(0, hp);
if (!wasDestroyed && IsDestroyed) OnDestroyed?.Invoke(this);
}
}
}