forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPredictionSwitchingConnect.cs
More file actions
65 lines (60 loc) · 2.67 KB
/
Copy pathPredictionSwitchingConnect.cs
File metadata and controls
65 lines (60 loc) · 2.67 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
using Unity.Entities;
using Unity.Mathematics;
using Unity.NetCode;
using Unity.Transforms;
[WorldSystemFilter(WorldSystemFilterFlags.ClientSimulation | WorldSystemFilterFlags.ThinClientSimulation)]
[UpdateInGroup(typeof(InitializationSystemGroup))]
public partial class PredictionSwitchingConnectClientSystem : SystemBase
{
protected override void OnCreate()
{
RequireForUpdate<PredictionSwitchingSettings>();
}
protected override void OnUpdate()
{
Entities
.WithStructuralChanges()
.WithoutBurst()
.WithNone<NetworkStreamInGame>()
.WithAll<NetworkId>()
.ForEach((Entity entity) =>
{
EntityManager.AddComponentData(entity, default(NetworkStreamInGame));
}).Run();
}
}
[WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation)]
[UpdateInGroup(typeof(InitializationSystemGroup))]
public partial class PredictionSwitchingConnectServerSystem : SystemBase
{
protected override void OnCreate()
{
RequireForUpdate<PredictionSwitchingSettings>();
}
protected override void OnUpdate()
{
var settings = SystemAPI.GetSingleton<PredictionSwitchingSettings>();
Entities
.WithStructuralChanges()
.WithoutBurst()
.WithNone<NetworkStreamInGame>()
.ForEach((Entity entity, ref CommandTarget target, in NetworkId netId) =>
{
target.targetEntity = EntityManager.Instantiate(settings.Player);
EntityManager.SetComponentData(target.targetEntity, new GhostOwner{NetworkId = netId.Value});
// Spawn at the edge of the field, in a line.
var isEven = (netId.Value & 1) == 0;
const float halfCharacterSpawnSeparation = 1.3f;
const float spawnStaggeredOffset = 0.65f;
var staggeredXPos = netId.Value * math.@select(halfCharacterSpawnSeparation, -halfCharacterSpawnSeparation, isEven) + math.@select(-spawnStaggeredOffset, spawnStaggeredOffset, isEven);
const float halfCapsuleHeight = 1f;
const float nearMapWall = -22.5f;
var transform = EntityManager.GetComponentData<LocalTransform>(target.targetEntity);
EntityManager.SetComponentData(target.targetEntity,
transform.WithPosition(new float3(staggeredXPos, halfCapsuleHeight, nearMapWall)));
EntityManager.AddComponentData(entity, default(NetworkStreamInGame));
// Add the player to the linked entity group so it is destroyed automatically on disconnect
EntityManager.GetBuffer<LinkedEntityGroup>(entity).Add(new LinkedEntityGroup{Value = target.targetEntity});
}).Run();
}
}