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
127 lines (117 loc) · 4.28 KB
/
Copy pathPredictionSwitchingInput.cs
File metadata and controls
127 lines (117 loc) · 4.28 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
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;
}
[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(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();
}
}