-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathStaticAsteroidSystem.cs
More file actions
86 lines (81 loc) · 4.21 KB
/
StaticAsteroidSystem.cs
File metadata and controls
86 lines (81 loc) · 4.21 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
using Unity.Entities;
using Unity.Transforms;
using Unity.NetCode;
using Unity.Collections;
using Unity.Burst;
using Unity.Burst.Intrinsics;
using Unity.Jobs;
namespace Asteroids.Client
{
[WorldSystemFilter(WorldSystemFilterFlags.ClientSimulation | WorldSystemFilterFlags.ServerSimulation)]
[UpdateBefore(typeof(TransformSystemGroup))]
[BurstCompile]
public partial struct StaticAsteroidSystem : ISystem
{
private EntityQuery m_StaticAsteroidsQuery;
private ComponentTypeHandle<LocalTransform> m_LocalTransforms;
private ComponentTypeHandle<StaticAsteroid> m_StaticAsteroids;
[BurstCompile]
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<StaticAsteroid>();
state.RequireForUpdate<NetworkId>();
m_StaticAsteroidsQuery = state.GetEntityQuery(new EntityQueryBuilder(Allocator.Temp).WithAll<LocalTransform, StaticAsteroid>());
m_LocalTransforms = state.GetComponentTypeHandle<LocalTransform>(isReadOnly:false);
m_StaticAsteroids = state.GetComponentTypeHandle<StaticAsteroid>(isReadOnly:true);
}
[BurstCompile]
partial struct StaticAsteroidJob : IJobChunk
{
public ComponentTypeHandle<LocalTransform> localTransformsHandle;
[ReadOnly] public ComponentTypeHandle<StaticAsteroid> staticAsteroidsHandle;
public NetworkTick tick;
public float tickFraction;
public uint simulationStepBatchSize;
public float frameTime;
public uint roundRobinFrequency;
public bool isServer;
public void Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
{
// This job is not written to support queries with enableable component types.
UnityEngine.Assertions.Assert.IsFalse(useEnabledMask);
// We update the server values infrequently, as we only need the updated
// LocalTransform positions for distance importance scaling & relevancy calculations.
if (isServer && (chunk.SequenceNumber + tick.TickIndexForValidTick) % roundRobinFrequency >= simulationStepBatchSize) return;
var localTransforms = chunk.GetNativeArray(ref localTransformsHandle).AsSpan();
var staticAsteroids = chunk.GetNativeArray(ref staticAsteroidsHandle).AsReadOnlySpan();
var entityEnumerator = new ChunkEntityEnumerator(useEnabledMask, chunkEnabledMask, chunk.Count);
while (entityEnumerator.NextEntityIndex(out var i))
{
ref var localTransform = ref localTransforms[i];
ref readonly var staticAsteroid = ref staticAsteroids[i];
localTransform.Position = staticAsteroid.GetPosition(tick, tickFraction, frameTime);
localTransform.Rotation = staticAsteroid.GetRotation(tick, tickFraction, frameTime);
}
}
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
var networkTime = SystemAPI.GetSingleton<NetworkTime>();
if (!networkTime.InterpolationTick.IsValid) return;
SystemAPI.TryGetSingleton<ClientServerTickRate>(out var tickRate);
tickRate.ResolveDefaults();
m_LocalTransforms.Update(ref state);
m_StaticAsteroids.Update(ref state);
var asteroidJob = new StaticAsteroidJob
{
localTransformsHandle = m_LocalTransforms,
staticAsteroidsHandle = m_StaticAsteroids,
tick = networkTime.InterpolationTick,
tickFraction = networkTime.InterpolationTickFraction,
simulationStepBatchSize = (uint) networkTime.SimulationStepBatchSize,
roundRobinFrequency = (uint) (tickRate.SimulationTickRate * 30), // Seconds.
frameTime = tickRate.SimulationFixedTimeStep,
isServer = state.WorldUnmanaged.IsServer(),
};
state.Dependency = asteroidJob.ScheduleParallel(m_StaticAsteroidsQuery, state.Dependency);
JobHandle.ScheduleBatchedJobs();
}
}
}