-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetworkPlayerControls.cs
242 lines (205 loc) · 7.77 KB
/
NetworkPlayerControls.cs
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
using System.Collections.Generic;
using System.Linq;
using Unity.Netcode;
using UnityEngine;
[RequireComponent(typeof(CharacterController))]
internal class NetworkPlayerControls : NetworkBehaviour
{
[SerializeField] private CharacterController controller;
private static OrbitCamera orbitCamera;
private static bool isRunning;
private static bool canControl;
private static float playerVelocityY;
private static Vector3 moveDirection;
private static float horizontal, vertical, speed;
private int tick = 0;
private float tickRate = 1f / 60f;
private float tickDeltaTime = 0;
private const int buffer = 1024;
private InputState[] _inputStates = new InputState[buffer];
private TransformState[] _transformStates = new TransformState[buffer];
internal NetworkVariable<TransformState> currentServerTransformState = new NetworkVariable<TransformState>();
internal TransformState previousTransformState;
private int _lastProcessedTick = 0;
internal static NetworkPlayerControls singleton;
private void OnServerStateChanged(TransformState previousvalue, TransformState serverState)
{
if(!IsLocalPlayer) return;
if (previousTransformState == null)
{
previousTransformState = serverState;
}
TransformState calculatedState = _transformStates.FirstOrDefault(localState => localState.Tick == serverState.Tick);
if (calculatedState.Position != serverState.Position)
{
TeleportPlayer(serverState);
IEnumerable<InputState> inputs = _inputStates.Where(input => input != null && input.Tick > serverState.Tick).OrderBy(input => input.Tick);
inputs = from input in inputs orderby input.Tick select input;
foreach (InputState inputState in inputs)
{
Move(inputState.MovementInput, isRunning);
TransformState newTransformState = new TransformState()
{
Tick = inputState.Tick,
Position = transform.position,
Rotation = transform.rotation,
HasStartedMoving = true
};
for (int i = 0; i < _transformStates.Length; i++)
{
if (_transformStates[i].Tick == inputState.Tick)
{
_transformStates[i] = newTransformState;
break;
}
}
}
}
}
private void OnEnable()
{
currentServerTransformState.OnValueChanged += OnServerStateChanged;
}
private void TeleportPlayer(TransformState state)
{
transform.position = state.Position;
transform.rotation = state.Rotation;
Physics.SyncTransforms();
for (int i = 0; i < _transformStates.Length; i++)
{
if (_transformStates[i].Tick == state.Tick)
{
_transformStates[i] = state;
break;
}
}
}
private void ProcessLocalPlayerMovement(Vector3 _direction)
{
tickDeltaTime += Time.deltaTime;
if (tickDeltaTime > tickRate)
{
int bufferIndex = tick % buffer;
if (!IsServer)
{
MovePlayerWithServerTickServerRpc(tick, _direction, isRunning);
Move(_direction, isRunning);
SaveState(_direction, isRunning, bufferIndex);
}
else
{
Move(_direction, isRunning);
TransformState state = new TransformState()
{
Tick = tick,
Position = transform.position,
Rotation = transform.rotation,
HasStartedMoving = true
};
SaveState(_direction, isRunning, bufferIndex);
previousTransformState = currentServerTransformState.Value;
currentServerTransformState.Value = state;
}
tickDeltaTime -= tickRate;
tick++;
}
}
[ServerRpc]
private void MovePlayerWithServerTickServerRpc(int tick, Vector3 moveDirection, bool _isRunning)
{
_lastProcessedTick = tick;
Move(moveDirection, _isRunning);
transform.forward = moveDirection;
TransformState transformState = new()
{
Tick = tick,
Position = transform.position,
Rotation = transform.rotation,
HasStartedMoving = true
};
previousTransformState = currentServerTransformState.Value;
currentServerTransformState.Value = transformState;
}
private void SimulateOtherPlayers()
{
tickDeltaTime += Time.deltaTime;
if (tickDeltaTime > tickRate)
{
if (currentServerTransformState.Value != null)
{
if (currentServerTransformState.Value.HasStartedMoving)
{
transform.position = currentServerTransformState.Value.Position;
transform.rotation = currentServerTransformState.Value.Rotation;
}
}
tickDeltaTime -= tickRate;
tick++;
}
}
private void SaveState(Vector2 movementInput, bool _isRunning, int bufferIndex)
{
InputState inputState = new InputState()
{
Tick = tick,
MovementInput = movementInput,
Running = _isRunning
};
TransformState transformState = new TransformState()
{
Tick = tick,
Position = transform.position,
Rotation = transform.rotation,
HasStartedMoving = true
};
_inputStates[bufferIndex] = inputState;
_transformStates[bufferIndex] = transformState;
}
public override void OnNetworkSpawn()
{
if (IsOwner)
{
singleton = this;
orbitCamera = OrbitCamera.singleton;
orbitCamera.SetupCam(transform);
PlayerSpeedManagerServer._singleton.playerCanControl += CanControlFunction;
canControl = PlayerSpeedManagerServer._singleton.isControlling;
}
}
private void CanControlFunction(bool value)
{
canControl = value;
orbitCamera.InitializeCursor(value);
}
private void Update()
{
if (IsClient && IsLocalPlayer)
{
if (!canControl) return;
horizontal = Input.GetAxis("Horizontal");
vertical = Input.GetAxis("Vertical");
moveDirection = (orbitCamera.transform.forward * vertical + orbitCamera.transform.right * horizontal).normalized;
isRunning = Input.GetKey(KeyCode.LeftShift);
playerVelocityY += PlayerSpeedManagerServer._singleton.GetServerGravity() * tickRate;
if (controller.isGrounded && playerVelocityY < 0)
{
playerVelocityY = 0;
}
moveDirection.y = playerVelocityY;
if (moveDirection != Vector3.zero)
{
ProcessLocalPlayerMovement(moveDirection);
}
}
else
{
SimulateOtherPlayers();
}
}
private void Move(Vector3 direction, bool _isRunning)
{
speed = _isRunning ? PlayerSpeedManagerServer._singleton.GetServerRunSpeed() : PlayerSpeedManagerServer._singleton.GetServerWalkSpeed();
transform.forward = moveDirection;
controller.Move(direction * speed * tickRate);
}
}