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
129 lines (108 loc) · 5.19 KB
/
Copy pathPredictionSwitchingSystem.cs
File metadata and controls
129 lines (108 loc) · 5.19 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
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<GhostOwner> m_GhostOwnerFromEntity;
[BurstCompile]
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<PredictionSwitchingSettings>();
state.RequireForUpdate<CommandTarget>();
state.RequireForUpdate<GhostPredictionSwitchingQueues>();
m_GhostOwnerFromEntity = state.GetComponentLookup<GhostOwner>(true);
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
var playerEnt = SystemAPI.GetSingleton<CommandTarget>().targetEntity;
if (playerEnt == Entity.Null)
return;
var playerPos = state.EntityManager.GetComponentData<LocalTransform>(playerEnt).Position;
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(PredictedGhost), typeof(SwitchPredictionSmoothing))]
partial struct SwitchToPredictedGhostViaRange : IJobEntity
{
public float3 playerPos;
public float enterRadiusSq;
public NativeQueue<ConvertPredictionEntry>.ParallelWriter predictedQueue;
public EntityCommandBuffer.ParallelWriter parallelEcb;
[ReadOnly]
public ComponentLookup<GhostOwner> ghostOwnerFromEntity;
public float transitionDurationSeconds;
public byte ballColorChangingEnabled;
void Execute(Entity ent, [EntityIndexInQuery] int entityIndexInQuery, in LocalTransform transform, in GhostInstance ghostInstance)
{
if (ghostInstance.ghostType < 0) return;
if (math.distancesq(playerPos, transform.Position) < enterRadiusSq)
{
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(PredictedGhost))]
partial struct SwitchToInterpolatedGhostViaRange : IJobEntity
{
public float3 playerPos;
public float exitRadiusSq;
public NativeQueue<ConvertPredictionEntry>.ParallelWriter interpolatedQueue;
public EntityCommandBuffer.ParallelWriter parallelEcb;
[ReadOnly]
public ComponentLookup<GhostOwner> ghostOwnerFromEntity;
public float transitionDurationSeconds;
void Execute(Entity ent, [EntityIndexInQuery] int entityIndexInQuery, in LocalTransform transform, in GhostInstance ghostInstance)
{
if (ghostInstance.ghostType < 0) return;
if (math.distancesq(playerPos, transform.Position) > exitRadiusSq)
{
interpolatedQueue.Enqueue(new ConvertPredictionEntry
{
TargetEntity = ent,
TransitionDurationSeconds = transitionDurationSeconds,
});
if (!ghostOwnerFromEntity.HasComponent(ent))
parallelEcb.RemoveComponent<URPMaterialPropertyBaseColor>(entityIndexInQuery, ent);
}
}
}
}