forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsteroidSwitchPredictionSystem.cs
More file actions
111 lines (95 loc) · 4.39 KB
/
Copy pathAsteroidSwitchPredictionSystem.cs
File metadata and controls
111 lines (95 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using Unity.Burst;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
using Unity.NetCode;
using Unity.Collections;
using Unity.Rendering;
namespace Asteroids.Client
{
[BurstCompile]
[WorldSystemFilter(WorldSystemFilterFlags.ClientSimulation)]
public partial struct AsteroidSwitchPredictionSystem : ISystem
{
[BurstCompile]
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<ClientSettings>();
state.RequireForUpdate<ShipCommandData>();
state.RequireForUpdate<AsteroidTagComponentData>();
state.RequireForUpdate<GhostPredictionSwitchingQueues>();
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
var settings = SystemAPI.GetSingleton<ClientSettings>();
if (settings.predictionRadius <= 0)
return;
var stateEntityManager = state.EntityManager;
if (!SystemAPI.TryGetSingletonEntity<ShipCommandData>(out var playerEnt) || !stateEntityManager.HasComponent<LocalTransform>(playerEnt))
return;
var playerPos = stateEntityManager.GetComponentData<LocalTransform>(playerEnt).Position;
var parallelEcb = SystemAPI.GetSingleton<EndSimulationEntityCommandBufferSystem.Singleton>().CreateCommandBuffer(state.WorldUnmanaged).AsParallelWriter();
var ghostPredictionSwitchingQueues = SystemAPI.GetSingletonRW<GhostPredictionSwitchingQueues>().ValueRW;
new SwitchToPredictedGhostViaRange
{
playerPos = playerPos,
parallelEcb = parallelEcb,
predictedQueue = ghostPredictionSwitchingQueues.ConvertToPredictedQueue,
enterRadiusSq = settings.predictionRadius*settings.predictionRadius,
}.ScheduleParallel();
var radiusPlusMargin = settings.predictionRadius + settings.predictionRadiusMargin;
new SwitchToInterpolatedGhostViaRange
{
playerPos = playerPos,
parallelEcb = parallelEcb,
interpolatedQueue = ghostPredictionSwitchingQueues.ConvertToInterpolatedQueue,
exitRadiusSq = radiusPlusMargin * radiusPlusMargin,
}.ScheduleParallel();
}
[BurstCompile]
[WithNone(typeof(PredictedGhost), typeof(SwitchPredictionSmoothing))]
[WithAll(typeof(AsteroidTagComponentData))]
partial struct SwitchToPredictedGhostViaRange : IJobEntity
{
public float3 playerPos;
public float enterRadiusSq;
public NativeQueue<ConvertPredictionEntry>.ParallelWriter predictedQueue;
public EntityCommandBuffer.ParallelWriter parallelEcb;
void Execute(Entity ent, [EntityIndexInQuery] int entityIndexInQuery, in LocalTransform transform)
{
if (math.distancesq(playerPos, transform.Position) < enterRadiusSq)
{
predictedQueue.Enqueue(new ConvertPredictionEntry
{
TargetEntity = ent,
TransitionDurationSeconds = 1.0f,
});
parallelEcb.AddComponent(entityIndexInQuery, ent, new URPMaterialPropertyBaseColor { Value = new float4(0, 1, 0, 1) });
}
}
}
[BurstCompile]
[WithNone(typeof(SwitchPredictionSmoothing))]
[WithAll(typeof(PredictedGhost), typeof(AsteroidTagComponentData))]
partial struct SwitchToInterpolatedGhostViaRange : IJobEntity
{
public float3 playerPos;
public float exitRadiusSq;
public NativeQueue<ConvertPredictionEntry>.ParallelWriter interpolatedQueue;
public EntityCommandBuffer.ParallelWriter parallelEcb;
void Execute(Entity ent, [EntityIndexInQuery] int entityIndexInQuery, in LocalTransform transform)
{
if (math.distancesq(playerPos, transform.Position) > exitRadiusSq)
{
interpolatedQueue.Enqueue(new ConvertPredictionEntry
{
TargetEntity = ent,
TransitionDurationSeconds = 1.0f,
});
parallelEcb.RemoveComponent<URPMaterialPropertyBaseColor>(entityIndexInQuery, ent);
}
}
}
}
}