Files
EE2Clone/Assets/Scripts/Systems/MovementSystems.cs
ldbetteridge 58da5d1d71 Initial import
2026-03-31 15:59:23 +01:00

152 lines
5.6 KiB
C#

using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
using Unity.NetCode;
using EE2Clone.Components;
using EE2Clone.Core;
namespace EE2Clone.Systems
{
/// <summary>
/// Server-side system that processes MoveCommandRpc and sets MoveTarget on units.
/// </summary>
[BurstCompile]
[WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation)]
public partial struct MoveCommandSystem : ISystem
{
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
var ecb = new EntityCommandBuffer(Allocator.Temp);
foreach (var (cmd, source, entity) in
SystemAPI.Query<RefRO<MoveCommandRpc>, RefRO<ReceiveRpcCommandRequest>>()
.WithEntityAccess())
{
var unitEntity = cmd.ValueRO.UnitEntity;
if (state.EntityManager.Exists(unitEntity) &&
state.EntityManager.HasComponent<MoveTarget>(unitEntity))
{
state.EntityManager.SetComponentData(unitEntity, new MoveTarget
{
Position = cmd.ValueRO.TargetPosition,
IsActive = true
});
// Clear combat target when moving
if (state.EntityManager.HasComponent<CombatTarget>(unitEntity))
{
state.EntityManager.SetComponentData(unitEntity, new CombatTarget
{
Target = Entity.Null
});
}
// Set state to Moving
if (state.EntityManager.HasComponent<UnitStateComponent>(unitEntity))
{
state.EntityManager.SetComponentData(unitEntity, new UnitStateComponent
{
Value = UnitState.Moving
});
}
}
ecb.DestroyEntity(entity);
}
ecb.Playback(state.EntityManager);
ecb.Dispose();
}
}
/// <summary>
/// Server-side system that moves units toward their MoveTarget.
/// Placeholder for flow-field pathfinding — currently uses direct steering.
/// </summary>
[BurstCompile]
[WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation)]
[UpdateAfter(typeof(MoveCommandSystem))]
public partial struct UnitMovementSystem : ISystem
{
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
float dt = SystemAPI.Time.DeltaTime;
foreach (var (transform, moveTarget, speed, unitState) in
SystemAPI.Query<RefRW<LocalTransform>, RefRW<MoveTarget>, RefRO<MovementSpeed>, RefRW<UnitStateComponent>>()
.WithAll<UnitTag>())
{
if (!moveTarget.ValueRO.IsActive) continue;
float3 pos = transform.ValueRO.Position;
float3 target = moveTarget.ValueRO.Position;
float3 direction = target - pos;
direction.y = 0; // Keep on ground plane
float distance = math.length(direction);
if (distance < 0.5f)
{
// Arrived
moveTarget.ValueRW.IsActive = false;
if (unitState.ValueRO.Value == UnitState.Moving)
unitState.ValueRW.Value = UnitState.Idle;
}
else
{
float3 moveDir = math.normalize(direction);
float moveAmount = speed.ValueRO.Value * dt;
moveAmount = math.min(moveAmount, distance);
var newPos = pos + moveDir * moveAmount;
transform.ValueRW.Position = newPos;
// Face movement direction
transform.ValueRW.Rotation = quaternion.LookRotationSafe(moveDir, math.up());
}
}
}
}
/// <summary>
/// Server-side: processes StopCommandRpc — halts unit movement and clears targets.
/// </summary>
[BurstCompile]
[WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation)]
public partial struct StopCommandSystem : ISystem
{
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
var ecb = new EntityCommandBuffer(Allocator.Temp);
foreach (var (cmd, source, entity) in
SystemAPI.Query<RefRO<StopCommandRpc>, RefRO<ReceiveRpcCommandRequest>>()
.WithEntityAccess())
{
var unitEntity = cmd.ValueRO.UnitEntity;
if (state.EntityManager.Exists(unitEntity))
{
if (state.EntityManager.HasComponent<MoveTarget>(unitEntity))
state.EntityManager.SetComponentData(unitEntity, new MoveTarget { IsActive = false });
if (state.EntityManager.HasComponent<CombatTarget>(unitEntity))
state.EntityManager.SetComponentData(unitEntity, new CombatTarget { Target = Entity.Null });
if (state.EntityManager.HasComponent<UnitStateComponent>(unitEntity))
state.EntityManager.SetComponentData(unitEntity, new UnitStateComponent { Value = UnitState.Idle });
}
ecb.DestroyEntity(entity);
}
ecb.Playback(state.EntityManager);
ecb.Dispose();
}
}
}