Core Game Loop Additions
This commit is contained in:
@@ -0,0 +1,134 @@
|
||||
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-only resource harvest: sweeps each surviving projectile's this-tick travel segment against
|
||||
/// resource-node ghosts and deposits <see cref="ResourceNode.HarvestPerHit"/> of the node's
|
||||
/// <see cref="ResourceNode.ResourceId"/> into the GLOBAL resource ledger (the CycleDirector's
|
||||
/// <see cref="StorageEntry"/> buffer, resolved via <see cref="ResourceLedger"/> — NEVER
|
||||
/// GetSingleton<StorageEntry>, which would collide with the base storage container). Runs in the plain
|
||||
/// server SimulationSystemGroup <c>[UpdateAfter(PredictedSimulationSystemGroup)]</c> — after
|
||||
/// ProjectileDamageSystem has already consumed Health-target hits and range-expired projectiles, so this
|
||||
/// only sees true survivors. The swept segment is reconstructed from <see cref="Projectile.LastStep"/>
|
||||
/// (written by ProjectileMoveSystem in the fixed-step group), so it is tunnelling-safe WITHOUT depending on
|
||||
/// this plain group's variable-frame DeltaTime. A node hit by two projectiles in one tick deposits twice
|
||||
/// but is destroyed exactly once. Relies on the asserted ~1000-unit base/expedition coordinate gap so a
|
||||
/// base projectile can never geometrically reach an expedition node.
|
||||
/// </summary>
|
||||
[BurstCompile]
|
||||
[WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation)]
|
||||
[UpdateInGroup(typeof(SimulationSystemGroup))]
|
||||
[UpdateAfter(typeof(PredictedSimulationSystemGroup))]
|
||||
public partial struct ResourceHarvestSystem : ISystem
|
||||
{
|
||||
const float k_ProjectileRadius = 0.2f;
|
||||
|
||||
[BurstCompile]
|
||||
public void OnCreate(ref SystemState state)
|
||||
{
|
||||
state.RequireForUpdate<Projectile>();
|
||||
state.RequireForUpdate<ResourceNode>();
|
||||
state.RequireForUpdate<ResourceLedger>();
|
||||
}
|
||||
|
||||
[BurstCompile]
|
||||
public void OnUpdate(ref SystemState state)
|
||||
{
|
||||
var ledgerEntity = SystemAPI.GetSingletonEntity<ResourceLedger>();
|
||||
var ledger = SystemAPI.GetBuffer<StorageEntry>(ledgerEntity);
|
||||
|
||||
// Snapshot all nodes once this tick.
|
||||
var nodeEntities = new NativeList<Entity>(Allocator.Temp);
|
||||
var nodePos = new NativeList<float2>(Allocator.Temp);
|
||||
var nodeRadius = new NativeList<float>(Allocator.Temp);
|
||||
var nodeRemaining = new NativeList<int>(Allocator.Temp);
|
||||
var nodeResource = new NativeList<byte>(Allocator.Temp);
|
||||
var nodePerHit = new NativeList<float>(Allocator.Temp);
|
||||
|
||||
foreach (var (xform, hr, node, e) in
|
||||
SystemAPI.Query<RefRO<LocalTransform>, RefRO<HitRadius>, RefRO<ResourceNode>>().WithEntityAccess())
|
||||
{
|
||||
nodeEntities.Add(e);
|
||||
nodePos.Add(xform.ValueRO.Position.xz);
|
||||
nodeRadius.Add(hr.ValueRO.Value);
|
||||
nodeRemaining.Add(node.ValueRO.Remaining);
|
||||
nodeResource.Add(node.ValueRO.ResourceId);
|
||||
nodePerHit.Add(node.ValueRO.HarvestPerHit);
|
||||
}
|
||||
|
||||
var destroyed = new NativeArray<bool>(nodeEntities.Length, Allocator.Temp);
|
||||
var ecb = new EntityCommandBuffer(Allocator.Temp);
|
||||
|
||||
foreach (var (xform, proj, projEntity) in
|
||||
SystemAPI.Query<RefRO<LocalTransform>, RefRO<Projectile>>().WithEntityAccess())
|
||||
{
|
||||
float3 cur = xform.ValueRO.Position;
|
||||
float2 segEnd = cur.xz;
|
||||
float2 segStart = segEnd - proj.ValueRO.Direction * proj.ValueRO.LastStep;
|
||||
float2 seg = segEnd - segStart;
|
||||
float segLenSq = math.lengthsq(seg);
|
||||
|
||||
int bestIdx = -1;
|
||||
float bestT = float.MaxValue;
|
||||
for (int i = 0; i < nodeEntities.Length; i++)
|
||||
{
|
||||
if (destroyed[i]) continue;
|
||||
float2 tp = nodePos[i];
|
||||
float t = segLenSq > 1e-8f ? math.saturate(math.dot(tp - segStart, seg) / segLenSq) : 0f;
|
||||
float2 closest = segStart + t * seg;
|
||||
float hitDist = nodeRadius[i] + k_ProjectileRadius;
|
||||
if (math.distancesq(tp, closest) <= hitDist * hitDist && t < bestT)
|
||||
{
|
||||
bestT = t;
|
||||
bestIdx = i;
|
||||
}
|
||||
}
|
||||
|
||||
if (bestIdx < 0)
|
||||
continue;
|
||||
|
||||
int amount = (int)nodePerHit[bestIdx];
|
||||
StorageMath.Deposit(ledger, nodeResource[bestIdx], amount);
|
||||
int rem = nodeRemaining[bestIdx] - amount;
|
||||
nodeRemaining[bestIdx] = rem;
|
||||
ecb.DestroyEntity(projEntity);
|
||||
|
||||
if (rem <= 0)
|
||||
{
|
||||
if (!destroyed[bestIdx])
|
||||
{
|
||||
destroyed[bestIdx] = true;
|
||||
ecb.DestroyEntity(nodeEntities[bestIdx]);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Persist the decremented Remaining (replicated GhostField) so depletion carries across ticks.
|
||||
SystemAPI.SetComponent(nodeEntities[bestIdx], new ResourceNode
|
||||
{
|
||||
ResourceId = nodeResource[bestIdx],
|
||||
Remaining = rem,
|
||||
HarvestPerHit = nodePerHit[bestIdx],
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
ecb.Playback(state.EntityManager);
|
||||
ecb.Dispose();
|
||||
destroyed.Dispose();
|
||||
nodeEntities.Dispose();
|
||||
nodePos.Dispose();
|
||||
nodeRadius.Dispose();
|
||||
nodeRemaining.Dispose();
|
||||
nodeResource.Dispose();
|
||||
nodePerHit.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user