forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPredictionSwitchingInput.cs
More file actions
169 lines (154 loc) · 5.98 KB
/
Copy pathPredictionSwitchingInput.cs
File metadata and controls
169 lines (154 loc) · 5.98 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
using Unity.Collections;
using Unity.Entities;
using Unity.NetCode;
using Unity.Transforms;
using Unity.Mathematics;
using Unity.NetCode.Samples.Common;
using Unity.Physics;
using Unity.Physics.Systems;
using UnityEngine;
[GhostComponent(OwnerSendType = SendToOwnerType.SendToNonOwner)]
public struct PredictionSwitchingInput : ICommandData
{
[GhostField] public NetworkTick Tick{get; set;}
[GhostField] public int horizontal;
[GhostField] public int vertical;
public FixedString512Bytes ToFixedString() => $"h:{horizontal},v:{vertical}";
}
[UpdateInGroup(typeof(GhostInputSystemGroup))]
public partial class PredictionSwitchingSampleInputSystem : SystemBase
{
protected override void OnCreate()
{
RequireForUpdate<PredictionSwitchingSettings>();
RequireForUpdate<NetworkStreamInGame>();
}
protected override void OnUpdate()
{
var tick = SystemAPI.GetSingleton<NetworkTime>().ServerTick;
var connection = SystemAPI.GetSingletonEntity<CommandTarget>();
Entities
.WithoutBurst()
.WithAll<GhostOwnerIsLocal>()
.ForEach((Entity entity, DynamicBuffer<PredictionSwitchingInput> inputBuffer) => {
var input = default(PredictionSwitchingInput);
input.Tick = tick;
if (UnityEngine.Input.GetKey("left") || TouchInput.GetKey(TouchInput.KeyCode.Left))
input.horizontal -= 1;
if (UnityEngine.Input.GetKey("right") || TouchInput.GetKey(TouchInput.KeyCode.Right))
input.horizontal += 1;
if (UnityEngine.Input.GetKey("down") || TouchInput.GetKey(TouchInput.KeyCode.Down))
input.vertical -= 1;
if (UnityEngine.Input.GetKey("up") || TouchInput.GetKey(TouchInput.KeyCode.Up))
input.vertical += 1;
inputBuffer.AddCommandData(input);
if (EntityManager.GetComponentData<CommandTarget>(connection).targetEntity == Entity.Null)
{
EntityManager.SetComponentData(connection, new CommandTarget{targetEntity = entity});
}
}).Run();
}
}
[UpdateInGroup(typeof(GhostInputSystemGroup))]
[WorldSystemFilter(WorldSystemFilterFlags.ThinClientSimulation)]
public partial class PredictionSwitchingThinInputSystem : SystemBase
{
protected override void OnCreate()
{
RequireForUpdate<NetworkStreamInGame>();
RequireForUpdate<PredictionSwitchingSettings>();
}
protected override void OnUpdate()
{
var networkTime = SystemAPI.GetSingleton<NetworkTime>();
if (!networkTime.ServerTick.IsValid)
return;
var inputTargetTick = networkTime.ServerTick;
if (!SystemAPI.TryGetSingletonBuffer<PredictionSwitchingInput>(out var input))
{
if (SystemAPI.TryGetSingletonRW<CommandTarget>(out var commandTarget))
{
if (commandTarget.ValueRO.targetEntity == Entity.Null)
commandTarget.ValueRW.targetEntity = EntityManager.CreateEntity(ComponentType.ReadOnly<PredictionSwitchingInput>(), ComponentType.ReadOnly<CommandTarget>());
input = EntityManager.AddBuffer<PredictionSwitchingInput>(commandTarget.ValueRW.targetEntity); // Returns GetBuffer if the buffer already exists.
}
}
var horizontal = (int) (networkTime.ServerTick.TickIndexForValidTick % 400);
if (horizontal < 100)
horizontal = 0;
else if (horizontal < 200)
horizontal = -1;
else if (horizontal < 300)
horizontal = 0;
else horizontal = 1;
input.AddCommandData(new PredictionSwitchingInput {Tick = inputTargetTick, horizontal = horizontal, vertical = 0});
}
}
[UpdateInGroup(typeof(PhysicsSystemGroup))]
[UpdateBefore(typeof(PhysicsInitializeGroup))]
public partial class PredictionSwitchingApplyInputSystem : SystemBase
{
protected override void OnCreate()
{
RequireForUpdate<PredictionSwitchingSettings>();
RequireForUpdate<NetworkTime>();
}
protected override void OnUpdate()
{
var tick = SystemAPI.GetSingleton<NetworkTime>().ServerTick;
var speed = SystemAPI.GetSingleton<PredictionSwitchingSettings>().PlayerSpeed;
Entities
.WithAll<Simulate>()
.ForEach((DynamicBuffer<PredictionSwitchingInput> inputBuffer, ref PhysicsVelocity vel) => {
inputBuffer.GetDataAtTick(tick, out var input);
float3 dir = default;
if (input.horizontal > 0)
dir.x += 1;
if (input.horizontal < 0)
dir.x -= 1;
if (input.vertical > 0)
dir.z += 1;
if (input.vertical < 0)
dir.z -= 1;
if (math.lengthsq(dir) > 0.5)
{
dir = math.normalize(dir);
dir *= speed;
}
vel.Linear.x = dir.x;
vel.Linear.z = dir.z;
}).Schedule();
}
}
[UpdateAfter(typeof(TransformSystemGroup))]
[WorldSystemFilter(WorldSystemFilterFlags.ClientSimulation)]
public partial class PredictionSwitchingCameraFollowSystem : SystemBase
{
Transform m_CameraTransform;
float3 m_CameraOffset;
protected override void OnCreate()
{
RequireForUpdate<PredictionSwitchingSettings>();
RequireForUpdate<NetworkStreamInGame>();
}
protected override void OnUpdate()
{
if (!m_CameraTransform)
{
var camera = Camera.main;
if (camera)
{
m_CameraTransform = camera.transform;
m_CameraOffset = m_CameraTransform.position;
}
else return;
}
Entities
.WithoutBurst()
.WithAll<GhostOwnerIsLocal>()
.WithAll<PredictionSwitchingInput>()
.ForEach((ref LocalToWorld trans) => {
m_CameraTransform.transform.position = trans.Position + m_CameraOffset;
}).Run();
}
}