Files
Project-M/Assets/_Project/Scripts/Server/Combat/HealthApplyDamageSystem.cs
T
kronic e362aaeb43 Import art/VFX asset packs + game-feel systems; normalize texture extensions to lowercase for LFS
Add BefourStudios SciFi environment packs, Gabriel Aguiar VFX, and the
ShaderCrew Toon Shader embedded packages, plus combat/enemy/wave/death
gameplay systems and supporting vault docs/screenshots.

Rename 11 vendor textures from uppercase .PNG/.HDR to lowercase so the
case-sensitive Git LFS filters (*.png/*.hdr) match on case-sensitive
filesystems (Linux CI, case-sensitive macOS), not just locally where
core.ignorecase=true masks the gap. Each .meta moved with its asset so
GUID references are preserved. All ~1000 binaries tracked via LFS.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-02 22:50:43 -07:00

85 lines
3.9 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;
// 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();
}
}
}