forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShipRelevancySphereSystem.cs
More file actions
170 lines (151 loc) · 5.86 KB
/
Copy pathShipRelevancySphereSystem.cs
File metadata and controls
170 lines (151 loc) · 5.86 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using Unity.Burst;
using Unity.NetCode;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Collections;
using Unity.Transforms;
using Unity.Jobs;
namespace Asteroids.Server
{
[BurstCompile]
[WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation)]
public partial struct ShipRelevancySphereISystem : ISystem
{
struct ConnectionRelevancy
{
public int ConnectionId;
public float3 Position;
}
NativeList<ConnectionRelevancy> m_Connections;
EntityQuery m_GhostQuery;
EntityQuery m_ConnectionQuery;
#if !ENABLE_TRANSFORM_V1
ComponentLookup<LocalTransform> m_Transforms;
#else
ComponentLookup<Translation> m_Translations;
#endif
[BurstCompile]
public void OnCreate(ref SystemState state)
{
var builder = new EntityQueryBuilder(Allocator.Temp).WithAll<GhostComponent>();
m_GhostQuery = state.GetEntityQuery(builder);
builder.Reset();
builder.WithAll<NetworkIdComponent>();
m_ConnectionQuery = state.GetEntityQuery(builder);
#if !ENABLE_TRANSFORM_V1
m_Transforms = state.GetComponentLookup<LocalTransform>(true);
#else
m_Translations = state.GetComponentLookup<Translation>(true);
#endif
m_Connections = new NativeList<ConnectionRelevancy>(16, Allocator.Persistent);
state.RequireForUpdate(m_ConnectionQuery);
state.RequireForUpdate<ServerSettings>();
}
[BurstCompile]
public void OnDestroy(ref SystemState state)
{
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
var settings = SystemAPI.GetSingleton<ServerSettings>();
ref var ghostRelevancy = ref SystemAPI.GetSingletonRW<GhostRelevancy>().ValueRW;
if (settings.levelData.relevancyRadius == 0)
{
ghostRelevancy.GhostRelevancyMode = GhostRelevancyMode.Disabled;
return;
}
ghostRelevancy.GhostRelevancyMode = GhostRelevancyMode.SetIsIrrelevant;
m_Connections.Clear();
var relevantSet = ghostRelevancy.GhostRelevancySet;
var parallelRelevantSet = relevantSet.AsParallelWriter();
var maxRelevantSize = m_GhostQuery.CalculateEntityCount() * m_ConnectionQuery.CalculateEntityCount();
var clearJob = new ClearRelevancySet
{
maxRelevantSize = maxRelevantSize,
relevantSet = relevantSet
};
var clearHandle = clearJob.Schedule(state.Dependency);
#if !ENABLE_TRANSFORM_V1
m_Transforms.Update(ref state);
#else
m_Translations.Update(ref state);
#endif
var connectionJob = new ConnectionRelevancyJob
{
#if !ENABLE_TRANSFORM_V1
transFromEntity = m_Transforms,
#else
transFromEntity = m_Translations,
#endif
connections = m_Connections
};
var connectionHandle = connectionJob.Schedule(state.Dependency);
var updateJob = new UpdateConnectionRelevancyJob
{
relevancyRadius = settings.levelData.relevancyRadius,
connections = m_Connections,
parallelRelevantSet = parallelRelevantSet
};
state.Dependency = JobHandle.CombineDependencies(state.Dependency, connectionHandle, clearHandle);
state.Dependency = updateJob.ScheduleParallel(state.Dependency);
}
[BurstCompile]
partial struct UpdateConnectionRelevancyJob : IJobEntity
{
public float relevancyRadius;
[ReadOnly] public NativeList<ConnectionRelevancy> connections;
public NativeParallelHashMap<RelevantGhostForConnection, int>.ParallelWriter parallelRelevantSet;
#if !ENABLE_TRANSFORM_V1
public void Execute(Entity entity, in GhostComponent ghost, in LocalTransform transform)
#else
public void Execute(Entity entity, in GhostComponent ghost, in Translation pos)
#endif
{
for (int i = 0; i < connections.Length; ++i)
{
#if !ENABLE_TRANSFORM_V1
if (math.distance(transform.Position, connections[i].Position) > relevancyRadius)
#else
if (math.distance(pos.Value, connections[i].Position) > relevancyRadius)
#endif
parallelRelevantSet.TryAdd(new RelevantGhostForConnection(connections[i].ConnectionId, ghost.ghostId), 1);
}
}
}
[BurstCompile]
partial struct ConnectionRelevancyJob : IJobEntity
{
[ReadOnly]
#if !ENABLE_TRANSFORM_V1
public ComponentLookup<LocalTransform> transFromEntity;
#else
public ComponentLookup<Translation> transFromEntity;
#endif
public NativeList<ConnectionRelevancy> connections;
public void Execute(in NetworkIdComponent netId, in CommandTargetComponent target)
{
if (target.targetEntity == Entity.Null || !transFromEntity.HasComponent(target.targetEntity))
return;
#if !ENABLE_TRANSFORM_V1
var pos = transFromEntity[target.targetEntity].Position;
#else
var pos = transFromEntity[target.targetEntity].Value;
#endif
connections.Add(new ConnectionRelevancy{ConnectionId = netId.Value, Position = pos});
}
}
[BurstCompile]
struct ClearRelevancySet : IJob
{
public int maxRelevantSize;
public NativeParallelHashMap<RelevantGhostForConnection, int> relevantSet;
public void Execute()
{
relevantSet.Clear();
if (relevantSet.Capacity < maxRelevantSize)
relevantSet.Capacity = maxRelevantSize;
}
}
}
}