e362aaeb43
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>
132 lines
5.8 KiB
C#
132 lines
5.8 KiB
C#
using ProjectM.Simulation;
|
|
using Unity.Burst;
|
|
using Unity.Collections;
|
|
using Unity.Entities;
|
|
using Unity.Mathematics;
|
|
using Unity.NetCode;
|
|
using Unity.Transforms;
|
|
|
|
namespace ProjectM.Server
|
|
{
|
|
/// <summary>
|
|
/// Server-authoritative Husk AI: each tick every Husk seeks the nearest LIVING player and strikes on
|
|
/// contact. Husks are OWNERLESS INTERPOLATED ghosts (not predicted), so this runs SERVER-ONLY in the plain
|
|
/// <see cref="SimulationSystemGroup"/> — writing <see cref="LocalTransform"/> directly (replicated to clients
|
|
/// by the stock LocalTransform default variant; no hand-written <c>[GhostField]</c>). Ordered
|
|
/// <c>[UpdateAfter(PredictedSimulationSystemGroup)]</c> (the predicted group is OrderFirst, so UpdateBefore is ignored) so a contact <see cref="DamageEvent"/> appended this
|
|
/// tick is drained the following tick by <see cref="HealthApplyDamageSystem"/> (which runs inside the predicted
|
|
/// group on the server). No <c>Simulate</c> filter: interpolated ghosts are not predicted and the server has
|
|
/// no rollback, so every Husk advances exactly once per tick. Movement/attack math is the pure, deterministic
|
|
/// <see cref="EnemyAIMath"/>; server fixed-step <c>SystemAPI.Time.DeltaTime</c> is correct here (not the
|
|
/// rollback loop). Structural-free: the only deferred op is appending to the player's DamageEvent buffer.
|
|
/// </summary>
|
|
[BurstCompile]
|
|
[WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation)]
|
|
[UpdateInGroup(typeof(SimulationSystemGroup))]
|
|
[UpdateAfter(typeof(PredictedSimulationSystemGroup))]
|
|
public partial struct EnemyAISystem : ISystem
|
|
{
|
|
[BurstCompile]
|
|
public void OnCreate(ref SystemState state)
|
|
{
|
|
state.RequireForUpdate<NetworkTime>();
|
|
state.RequireForUpdate(state.GetEntityQuery(ComponentType.ReadOnly<EnemyTag>()));
|
|
}
|
|
|
|
[BurstCompile]
|
|
public void OnUpdate(ref SystemState state)
|
|
{
|
|
// Snapshot living player targets once this tick (stable query order).
|
|
var playerEntities = new NativeList<Entity>(Allocator.Temp);
|
|
var playerPositions = new NativeList<float3>(Allocator.Temp);
|
|
foreach (var (xform, health, entity) in
|
|
SystemAPI.Query<RefRO<LocalTransform>, RefRO<Health>>()
|
|
.WithAll<PlayerTag>()
|
|
.WithEntityAccess())
|
|
{
|
|
if (health.ValueRO.Current <= 0f)
|
|
continue; // don't chase or strike a corpse
|
|
playerEntities.Add(entity);
|
|
playerPositions.Add(xform.ValueRO.Position);
|
|
}
|
|
|
|
if (playerEntities.Length == 0)
|
|
{
|
|
playerEntities.Dispose();
|
|
playerPositions.Dispose();
|
|
return;
|
|
}
|
|
|
|
float dt = SystemAPI.Time.DeltaTime;
|
|
var serverTick = SystemAPI.GetSingleton<NetworkTime>().ServerTick;
|
|
var ecb = new EntityCommandBuffer(Allocator.Temp);
|
|
|
|
foreach (var (xform, stats, cooldown) in
|
|
SystemAPI.Query<RefRW<LocalTransform>, RefRO<EnemyStats>, RefRW<EnemyAttackCooldown>>()
|
|
.WithAll<EnemyTag>())
|
|
{
|
|
float3 pos = xform.ValueRO.Position;
|
|
|
|
// Nearest living player (planar XZ).
|
|
int best = -1;
|
|
float bestSq = float.MaxValue;
|
|
for (int i = 0; i < playerPositions.Length; i++)
|
|
{
|
|
float2 d = playerPositions[i].xz - pos.xz;
|
|
float sq = math.lengthsq(d);
|
|
if (sq < bestSq)
|
|
{
|
|
bestSq = sq;
|
|
best = i;
|
|
}
|
|
}
|
|
|
|
float3 targetPos = playerPositions[best];
|
|
|
|
// Seek: stop just inside strike range so the Husk holds position to attack.
|
|
float stopDistance = stats.ValueRO.AttackRange * 0.9f;
|
|
float3 vel = EnemyAIMath.SeekVelocity(pos, targetPos, stats.ValueRO.MoveSpeed, stopDistance);
|
|
float3 newPos = pos + vel * dt;
|
|
newPos.y = pos.y; // hold the movement plane
|
|
xform.ValueRW.Position = newPos;
|
|
|
|
// Face the target (planar) for presentation.
|
|
float3 toTarget = targetPos - pos;
|
|
toTarget.y = 0f;
|
|
if (math.lengthsq(toTarget) > 1e-6f)
|
|
xform.ValueRW.Rotation = quaternion.LookRotationSafe(math.normalize(toTarget), math.up());
|
|
|
|
// Strike on contact once the cooldown has elapsed.
|
|
if (EnemyAIMath.InAttackRange(pos, targetPos, stats.ValueRO.AttackRange))
|
|
{
|
|
uint nextRaw = cooldown.ValueRO.NextAttackTick;
|
|
bool ready = true;
|
|
if (nextRaw != 0)
|
|
{
|
|
var nextTick = new NetworkTick(nextRaw);
|
|
if (nextTick.IsValid && nextTick.IsNewerThan(serverTick))
|
|
ready = false;
|
|
}
|
|
|
|
if (ready)
|
|
{
|
|
ecb.AppendToBuffer(playerEntities[best], new DamageEvent
|
|
{
|
|
Amount = stats.ValueRO.AttackDamage,
|
|
SourceNetworkId = -1, // environment / Husk, not a player
|
|
});
|
|
uint cooldownTicks = (uint)math.max(1, stats.ValueRO.AttackCooldownTicks);
|
|
cooldown.ValueRW.NextAttackTick = TickUtil.NonZero(serverTick.TickIndexForValidTick + cooldownTicks);
|
|
}
|
|
}
|
|
}
|
|
|
|
ecb.Playback(state.EntityManager);
|
|
|
|
ecb.Dispose();
|
|
playerEntities.Dispose();
|
|
playerPositions.Dispose();
|
|
}
|
|
}
|
|
}
|