public void OnServerDataUpdate(PlayerUpdateData data) { if (IsOwn) { while (reconciliationHistory.Any() && reconciliationHistory.Peek().Frame < GameManager.Instance.LastRecievedServerTick) { reconciliationHistory.Dequeue(); } if (reconciliationHistory.Any() && reconciliationHistory.Peek().Frame == GameManager.Instance.LastRecievedServerTick) { ReconciliationInfo info = reconciliationHistory.Dequeue(); if (Vector3.Distance(info.Data.Position, data.Position) > 0.05f) { List <ReconciliationInfo> infos = reconciliationHistory.ToList(); Interpolation.CurrentData = data; transform.position = data.Position; transform.rotation = data.LookDirection; for (int i = 0; i < infos.Count; i++) { PlayerUpdateData u = Logic.GetNextFrameData(infos[i].Input, Interpolation.CurrentData); Interpolation.SetFramePosition(u); } } } } else { Interpolation.SetFramePosition(data); } }
public void PerformUpdate(int index) { if (inputs.Length > 0) { PlayerInputData input = inputs.First(); InputTick++; for (int i = 1; i < inputs.Length; i++) { InputTick++; for (int j = 0; j < input.Keyinputs.Length; j++) { input.Keyinputs[j] = input.Keyinputs[j] || inputs[i].Keyinputs[j]; } input.LookDirection = inputs[i].LookDirection; } CurrentUpdateData = Logic.GetNextFrameData(input, CurrentUpdateData); } UpdateDataHistory.Add(CurrentUpdateData); if (UpdateDataHistory.Count > 10) { UpdateDataHistory.RemoveAt(0); } transform.localPosition = CurrentUpdateData.Position; transform.localRotation = CurrentUpdateData.LookDirection; Room.UpdateDatas[index] = CurrentUpdateData; }
public PlayerUpdateData GetNextFrameData(PlayerInputData input, PlayerUpdateData currentUpdateData) { bool w = input.Keyinputs[0]; bool a = input.Keyinputs[1]; bool s = input.Keyinputs[2]; bool d = input.Keyinputs[3]; bool space = input.Keyinputs[4]; bool left = input.Keyinputs[5]; Vector3 rotation = input.LookDirection.eulerAngles; gravity = new Vector3(0, currentUpdateData.Gravity, 0); Vector3 movement = Vector3.zero; if (w) { movement += Vector3.forward; } if (a) { movement += Vector3.left; } if (s) { movement += Vector3.back; } if (d) { movement += Vector3.right; } movement = Quaternion.Euler(0, rotation.y, 0) * movement; movement.Normalize(); movement = movement * WalkSpeed; movement = movement * Time.fixedDeltaTime; movement = movement + gravity * Time.fixedDeltaTime; // the following code fixes charactercontroller issues from unity CharacterController.Move(new Vector3(0, -0.001f, 0)); if (CharacterController.isGrounded) { if (space) { gravity = new Vector3(0, JumpStrength, 0); } } else { gravity -= new Vector3(0, GravityConstant, 0); } CharacterController.Move(movement); return(new PlayerUpdateData(currentUpdateData.Id, gravity.y, transform.localPosition, input.LookDirection)); }
void FixedUpdate() { if (IsOwn) { bool[] inputs = new bool[6]; inputs[0] = Input.GetKey(KeyCode.W); inputs[1] = Input.GetKey(KeyCode.A); inputs[2] = Input.GetKey(KeyCode.S); inputs[3] = Input.GetKey(KeyCode.D); inputs[4] = Input.GetKey(KeyCode.Space); inputs[5] = Input.GetMouseButton(0); if (inputs[5]) { GameObject go = Instantiate(ShotPrefab); go.transform.position = Interpolation.CurrentData.Position; go.transform.rotation = transform.rotation; Destroy(go, 1f); } yaw += Input.GetAxis("Mouse X") * SensitivityX; pitch += Input.GetAxis("Mouse Y") * SensitivityY; Quaternion rot = Quaternion.Euler(pitch, yaw, 0); PlayerInputData inputData = new PlayerInputData(inputs, rot, GameManager.Instance.LastRecievedServerTick - 1); transform.position = Interpolation.CurrentData.Position; PlayerUpdateData updateData = Logic.GetNextFrameData(inputData, Interpolation.CurrentData); Interpolation.SetFramePosition(updateData); using (Message m = Message.Create((ushort)Tags.GamePlayerInput, inputData)) { GlobalManager.Instance.Client.SendMessage(m, SendMode.Reliable); } reconciliationHistory.Enqueue(new ReconciliationInfo(GameManager.Instance.ClientTick, updateData, inputData)); } }
public void Initialize(Vector3 position, ClientConnection clientConnection) { ClientConnection = clientConnection; Room = clientConnection.Room; Client = clientConnection.Client; ClientConnection.Player = this; Room.ServerPlayers.Add(this); Room.UpdateDatas = new PlayerUpdateData[Room.ServerPlayers.Count]; CurrentUpdateData = new PlayerUpdateData(Client.ID, 0, Vector3.zero, Quaternion.identity); InputTick = Room.ServerTick; Health = 100; PlayerSpawnData[] datas = new PlayerSpawnData[Room.ServerPlayers.Count]; for (int i = 0; i < Room.ServerPlayers.Count; i++) { ServerPlayer p = Room.ServerPlayers[i]; datas[i] = p.GetPlayerSpawnData(); } using (Message m = Message.Create((ushort)Tags.GameStartDataResponse, new GameStartData(datas, Room.ServerTick))) { Client.SendMessage(m, SendMode.Reliable); } }
public ReconciliationInfo(uint frame, PlayerUpdateData data, PlayerInputData input) { Frame = frame; Data = data; Input = input; }
public void RefreshToPosition(PlayerUpdateData data, PlayerUpdateData prevData) { PreviousData = prevData; CurrentData = data; lastInputTime = Time.fixedTime; }
public void SetFramePosition(PlayerUpdateData data) { RefreshToPosition(data, CurrentData); }