forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplyHitMarkSystem.cs
More file actions
51 lines (46 loc) · 1.76 KB
/
Copy pathApplyHitMarkSystem.cs
File metadata and controls
51 lines (46 loc) · 1.76 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
using Unity.Entities;
using Unity.NetCode;
namespace Samples.HelloNetcode
{
[UpdateInGroup(typeof(HelloNetcodePredictedSystemGroup))]
[UpdateAfter(typeof(ShootingSystem))]
[RequireMatchingQueriesForUpdate]
public partial struct ApplyHitMarkSystem : ISystem
{
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<NetworkTime>();
state.RequireForUpdate<Hit>();
}
public void OnDestroy(ref SystemState state)
{
}
public void OnUpdate(ref SystemState state)
{
var networkTime = SystemAPI.GetSingleton<NetworkTime>();
// Do not perform hit-scan when rolling back, only when simulating the latest tick
if (!networkTime.IsFirstTimeFullyPredictingTick)
return;
var isServer = state.WorldUnmanaged.IsServer();
foreach (var (hit, serverHitMarker, clientHitMarker) in SystemAPI.Query<RefRO<Hit>, RefRW<ServerHitMarker>, RefRW<ClientHitMarker>>().WithAll<Simulate>())
{
if (hit.ValueRO.Entity == Entity.Null)
{
continue;
}
if (isServer)
{
serverHitMarker.ValueRW.Entity = hit.ValueRO.Entity;
serverHitMarker.ValueRW.HitPoint = hit.ValueRO.HitPoint;
serverHitMarker.ValueRW.ServerHitTick = hit.ValueRO.Tick;
}
else
{
clientHitMarker.ValueRW.Entity = hit.ValueRO.Entity;
clientHitMarker.ValueRW.HitPoint = hit.ValueRO.HitPoint;
clientHitMarker.ValueRW.ClientHitTick = hit.ValueRO.Tick;
}
}
}
}
}