Grid/Destruction/WallSegmentDamage.cs
using Sandbox;
using System.Numerics;
public sealed class WallSegmentDamage : Component
{
[Property] public int MaxHealth { get; set; } = 15;
[Property] public WallColumnDamage ParentColumn { get; set; }
private int health;
private bool destroyed;
protected override void OnStart()
{
health = MaxHealth;
}
// =========================
// CLIENT → HOST
// =========================
[Rpc.Host]
public void RpcHit(int damage, Vector3 hitWorldPos)
{
if (destroyed)
return;
ApplyDamage(damage, hitWorldPos);
}
// =========================
// HOST LOGIC
// =========================
private void ApplyDamage(int damage, Vector3 hitWorldPos)
{
if (!Networking.IsHost)
return;
health -= damage;
if (health > 0)
return;
destroyed = true;
DestroySegment(hitWorldPos);
RpcDestroySegment(hitWorldPos);
}
// =========================
// HOST → ALL
// =========================
[Rpc.Broadcast]
private void RpcDestroySegment(Vector3 hitWorldPos)
{
if (destroyed && Networking.IsHost)
return;
destroyed = true;
DestroySegment(hitWorldPos);
}
// =========================
// VISUAL + COLLIDER
// =========================
private void DestroySegment(Vector3 hitWorldPos)
{
//Log.Info($"[SEGMENT DESTROY] {GameObject.Name}");
// poista oma collider ja malli → reikä
GameObject.Components.Get<BoxCollider>()?.Destroy();
GameObject.Components.Get<ModelRenderer>()?.Destroy();
// 🔑 kerro pylväälle
ParentColumn?.OnSegmentDestroyed(hitWorldPos);
}
}