92 lines
4.2 KiB
C#
92 lines
4.2 KiB
C#
using ProjectM.Simulation;
|
|
using Unity.Burst;
|
|
using Unity.Collections;
|
|
using Unity.Entities;
|
|
using Unity.Mathematics;
|
|
using Unity.NetCode;
|
|
|
|
namespace ProjectM.Server
|
|
{
|
|
/// <summary>
|
|
/// Server-authoritative damage application. Drains each damageable entity's
|
|
/// <see cref="DamageEvent"/> buffer (appended by <see cref="ProjectileDamageSystem"/> earlier
|
|
/// this tick), subtracts the summed amount from <see cref="Health"/>, then clears the buffer so
|
|
/// each hit is applied exactly once. Entities that carry character stats (players) clamp to their
|
|
/// data-driven <see cref="EffectiveCharacterStats.MaxHealth"/> ceiling; others (training dummies)
|
|
/// clamp at zero. A dead <see cref="TrainingDummyTag"/> is destroyed; player death is deferred.
|
|
/// Health.Current is a <c>[GhostField]</c>, so the new value replicates to clients for display.
|
|
///
|
|
/// Runs server-only (<see cref="WorldSystemFilterFlags.ServerSimulation"/>) inside the prediction
|
|
/// group so it shares tick timing with movement/damage, where it executes once per tick. The
|
|
/// single structural change (destroying a dead dummy) is batched through a frame-allocator
|
|
/// <see cref="EntityCommandBuffer"/> that is played back immediately to the entity manager — so a
|
|
/// plain-world EditMode test needs no separate ECB system.
|
|
/// </summary>
|
|
[WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation)]
|
|
[UpdateInGroup(typeof(PredictedSimulationSystemGroup))]
|
|
[UpdateAfter(typeof(ProjectileDamageSystem))]
|
|
[BurstCompile]
|
|
public partial struct HealthApplyDamageSystem : ISystem
|
|
{
|
|
[BurstCompile]
|
|
public void OnUpdate(ref SystemState state)
|
|
{
|
|
var ecb = new EntityCommandBuffer(Allocator.Temp);
|
|
bool haveTick = SystemAPI.TryGetSingleton<NetworkTime>(out var netTime);
|
|
|
|
foreach (var (health, dmg, entity) in
|
|
SystemAPI.Query<RefRW<Health>, DynamicBuffer<DamageEvent>>()
|
|
.WithEntityAccess())
|
|
{
|
|
if (dmg.Length == 0)
|
|
continue;
|
|
// Dev god-mode: while enabled, this entity ignores ALL damage (server-authoritative, once per tick).
|
|
if (SystemAPI.HasComponent<DebugGodMode>(entity) && SystemAPI.IsComponentEnabled<DebugGodMode>(entity))
|
|
{
|
|
dmg.Clear();
|
|
continue;
|
|
}
|
|
|
|
|
|
// Respawn invulnerability: a freshly-recovered player ignores damage for a window.
|
|
if (haveTick && netTime.ServerTick.IsValid && SystemAPI.HasComponent<RespawnInvuln>(entity))
|
|
{
|
|
uint until = SystemAPI.GetComponent<RespawnInvuln>(entity).UntilTick;
|
|
if (until != 0)
|
|
{
|
|
var untilTick = new NetworkTick(until);
|
|
if (untilTick.IsValid && untilTick.IsNewerThan(netTime.ServerTick))
|
|
{
|
|
dmg.Clear();
|
|
continue;
|
|
}
|
|
}
|
|
}
|
|
|
|
float total = 0f;
|
|
for (int i = 0; i < dmg.Length; i++)
|
|
total += dmg[i].Amount;
|
|
dmg.Clear();
|
|
|
|
float newHp = health.ValueRO.Current - total;
|
|
|
|
// Effective max health (base + modifiers) is the runtime ceiling for entities that carry
|
|
// character stats (players); others just clamp at zero. No auto-heal on a max increase.
|
|
if (SystemAPI.HasComponent<EffectiveCharacterStats>(entity))
|
|
newHp = math.clamp(newHp, 0f, SystemAPI.GetComponent<EffectiveCharacterStats>(entity).MaxHealth);
|
|
else
|
|
newHp = math.max(0f, newHp);
|
|
|
|
health.ValueRW.Current = newHp;
|
|
|
|
// Server-authoritative death: training dummies despawn; player death is deferred (clamp only).
|
|
if (health.ValueRO.Current <= 0f && (SystemAPI.HasComponent<TrainingDummyTag>(entity) || SystemAPI.HasComponent<EnemyTag>(entity)))
|
|
ecb.DestroyEntity(entity);
|
|
}
|
|
|
|
ecb.Playback(state.EntityManager);
|
|
ecb.Dispose();
|
|
}
|
|
}
|
|
}
|