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
64 lines (59 loc) · 2.88 KB
/
Copy pathPredictionSwitchingConnect.cs
File metadata and controls
64 lines (59 loc) · 2.88 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
using Unity.Collections;
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()
{
var commandBuffer = new EntityCommandBuffer(Allocator.Temp);
foreach(var (_, entity) in SystemAPI.Query<RefRO<NetworkId>>().WithNone<NetworkStreamInGame>().WithEntityAccess())
{
commandBuffer.AddComponent<NetworkStreamInGame>(entity);
}
commandBuffer.Playback(EntityManager);
}
}
[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>();
var commandBuffer = new EntityCommandBuffer(Allocator.Temp);
foreach(var (target, netId, entity) in
SystemAPI.Query<RefRW<CommandTarget>, RefRO<NetworkId>>()
.WithNone<NetworkStreamInGame>()
.WithEntityAccess())
{
target.ValueRW.targetEntity = EntityManager.Instantiate(settings.Player);
EntityManager.SetComponentData(target.ValueRW.targetEntity, new GhostOwner{NetworkId = netId.ValueRO.Value});
// Spawn at the edge of the field, in a line.
var isEven = (netId.ValueRO.Value & 1) == 0;
const float halfCharacterSpawnSeparation = 1.3f;
const float spawnStaggeredOffset = 0.65f;
var staggeredXPos = netId.ValueRO.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.ValueRW.targetEntity);
EntityManager.SetComponentData(target.ValueRW.targetEntity,
transform.WithPosition(new float3(staggeredXPos, halfCapsuleHeight, nearMapWall)));
commandBuffer.AddComponent(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.ValueRW.targetEntity});
}
commandBuffer.Playback(EntityManager);
}
}