forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPredictionSwitchingSystem.cs
More file actions
153 lines (131 loc) · 6.07 KB
/
Copy pathPredictionSwitchingSystem.cs
File metadata and controls
153 lines (131 loc) · 6.07 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using Unity.Burst;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
using Unity.NetCode;
using Unity.Collections;
using Unity.Rendering;
[BurstCompile]
[WorldSystemFilter(WorldSystemFilterFlags.ClientSimulation)]
public partial struct PredictionSwitchingSystem : ISystem
{
ComponentLookup<GhostOwnerComponent> m_GhostOwnerFromEntity;
[BurstCompile]
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<PredictionSwitchingSettings>();
state.RequireForUpdate<CommandTargetComponent>();
state.RequireForUpdate<GhostPredictionSwitchingQueues>();
m_GhostOwnerFromEntity = state.GetComponentLookup<GhostOwnerComponent>(true);
}
[BurstCompile]
public void OnDestroy(ref SystemState state) { }
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
var playerEnt = SystemAPI.GetSingleton<CommandTargetComponent>().targetEntity;
if (playerEnt == Entity.Null)
return;
#if !ENABLE_TRANSFORM_V1
var playerPos = state.EntityManager.GetComponentData<LocalTransform>(playerEnt).Position;
#else
var playerPos = state.EntityManager.GetComponentData<Translation>(playerEnt).Value;
#endif
var ghostPredictionSwitchingQueues = SystemAPI.GetSingletonRW<GhostPredictionSwitchingQueues>().ValueRW;
var parallelEcb = SystemAPI.GetSingleton<EndSimulationEntityCommandBufferSystem.Singleton>().CreateCommandBuffer(state.WorldUnmanaged).AsParallelWriter();
m_GhostOwnerFromEntity.Update(ref state);
var ghostOwnerFromEntity = m_GhostOwnerFromEntity;
var predictionSwitchingSettings = SystemAPI.GetSingleton<PredictionSwitchingSettings>();
new SwitchToPredictedGhostViaRange
{
playerPos = playerPos,
parallelEcb = parallelEcb,
predictedQueue = ghostPredictionSwitchingQueues.ConvertToPredictedQueue,
enterRadiusSq = predictionSwitchingSettings.PredictionSwitchingRadius * predictionSwitchingSettings.PredictionSwitchingRadius,
ghostOwnerFromEntity = ghostOwnerFromEntity,
transitionDurationSeconds = predictionSwitchingSettings.TransitionDurationSeconds,
ballColorChangingEnabled = predictionSwitchingSettings.BallColorChangingEnabled,
}.ScheduleParallel();
var radiusPlusMargin = (predictionSwitchingSettings.PredictionSwitchingRadius + predictionSwitchingSettings.PredictionSwitchingMargin);
new SwitchToInterpolatedGhostViaRange
{
playerPos = playerPos,
parallelEcb = parallelEcb,
interpolatedQueue = ghostPredictionSwitchingQueues.ConvertToInterpolatedQueue,
exitRadiusSq = radiusPlusMargin * radiusPlusMargin,
ghostOwnerFromEntity = ghostOwnerFromEntity,
transitionDurationSeconds = predictionSwitchingSettings.TransitionDurationSeconds,
}.ScheduleParallel();
}
[BurstCompile]
[WithNone(typeof(PredictedGhostComponent), typeof(SwitchPredictionSmoothing))]
partial struct SwitchToPredictedGhostViaRange : IJobEntity
{
public float3 playerPos;
public float enterRadiusSq;
public NativeQueue<ConvertPredictionEntry>.ParallelWriter predictedQueue;
public EntityCommandBuffer.ParallelWriter parallelEcb;
[ReadOnly]
public ComponentLookup<GhostOwnerComponent> ghostOwnerFromEntity;
public float transitionDurationSeconds;
public byte ballColorChangingEnabled;
#if !ENABLE_TRANSFORM_V1
void Execute(Entity ent, [EntityIndexInQuery] int entityIndexInQuery, in LocalTransform transform, in GhostComponent ghostComponent)
#else
void Execute(Entity ent, [EntityIndexInQuery] int entityIndexInQuery, in Translation position, in GhostComponent ghostComponent)
#endif
{
if (ghostComponent.ghostType < 0) return;
#if !ENABLE_TRANSFORM_V1
if (math.distancesq(playerPos, transform.Position) < enterRadiusSq)
#else
if (math.distancesq(playerPos, position.Value) < enterRadiusSq)
#endif
{
transitionDurationSeconds = 1.0f;
predictedQueue.Enqueue(new ConvertPredictionEntry
{
TargetEntity = ent,
TransitionDurationSeconds = transitionDurationSeconds,
});
if (ballColorChangingEnabled == 1 && !ghostOwnerFromEntity.HasComponent(ent))
parallelEcb.AddComponent(entityIndexInQuery, ent, new URPMaterialPropertyBaseColor {Value = new float4(0, 1, 0, 1)});
}
}
}
[BurstCompile]
[WithNone(typeof(SwitchPredictionSmoothing))]
[WithAll(typeof(PredictedGhostComponent))]
partial struct SwitchToInterpolatedGhostViaRange : IJobEntity
{
public float3 playerPos;
public float exitRadiusSq;
public NativeQueue<ConvertPredictionEntry>.ParallelWriter interpolatedQueue;
public EntityCommandBuffer.ParallelWriter parallelEcb;
[ReadOnly]
public ComponentLookup<GhostOwnerComponent> ghostOwnerFromEntity;
public float transitionDurationSeconds;
#if !ENABLE_TRANSFORM_V1
void Execute(Entity ent, [EntityIndexInQuery] int entityIndexInQuery, in LocalTransform transform, in GhostComponent ghostComponent)
#else
void Execute(Entity ent, [EntityIndexInQuery] int entityIndexInQuery, in Translation position, in GhostComponent ghostComponent)
#endif
{
if (ghostComponent.ghostType < 0) return;
#if !ENABLE_TRANSFORM_V1
if (math.distancesq(playerPos, transform.Position) > exitRadiusSq)
#else
if (math.distancesq(playerPos, position.Value) > exitRadiusSq)
#endif
{
interpolatedQueue.Enqueue(new ConvertPredictionEntry
{
TargetEntity = ent,
TransitionDurationSeconds = transitionDurationSeconds,
});
if (!ghostOwnerFromEntity.HasComponent(ent))
parallelEcb.RemoveComponent<URPMaterialPropertyBaseColor>(entityIndexInQuery, ent);
}
}
}
}