From 35117a5d8b464dfddc76e2eceec16b4ba15baaa2 Mon Sep 17 00:00:00 2001 From: Sveske_Juice Date: Sun, 17 Mar 2024 17:55:35 +0100 Subject: [PATCH] Send game state to client rpc with tick info. Still WIP on fields in game state (should include player pos, enemies in future) --- Assets/Scripts/Rope/GameState.cs | 14 ++++++++++++++ Assets/Scripts/Rope/GameState.cs.meta | 2 ++ Assets/Scripts/Rope/RopeSimulator.cs | 27 +++++++++++++++++++++++++-- 3 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 Assets/Scripts/Rope/GameState.cs create mode 100644 Assets/Scripts/Rope/GameState.cs.meta diff --git a/Assets/Scripts/Rope/GameState.cs b/Assets/Scripts/Rope/GameState.cs new file mode 100644 index 0000000..d174b65 --- /dev/null +++ b/Assets/Scripts/Rope/GameState.cs @@ -0,0 +1,14 @@ +using Unity.Netcode; + +[System.Serializable] +public struct GameState : INetworkSerializable +{ + public int tick; + public NetworkRope nrope; + + public void NetworkSerialize(BufferSerializer serializer) where T : IReaderWriter + { + serializer.SerializeValue(ref tick); + nrope.NetworkSerialize(serializer); + } +} diff --git a/Assets/Scripts/Rope/GameState.cs.meta b/Assets/Scripts/Rope/GameState.cs.meta new file mode 100644 index 0000000..6a12a68 --- /dev/null +++ b/Assets/Scripts/Rope/GameState.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 1cdca75ffd9bfc5b9b1c9ffe1a501dcf \ No newline at end of file diff --git a/Assets/Scripts/Rope/RopeSimulator.cs b/Assets/Scripts/Rope/RopeSimulator.cs index 0951d2f..0634624 100644 --- a/Assets/Scripts/Rope/RopeSimulator.cs +++ b/Assets/Scripts/Rope/RopeSimulator.cs @@ -66,11 +66,13 @@ public class RopeSimulator : NetworkBehaviour [SerializeField] float pullAnimationOvershootThreshold = 0.2f; [Header("Netcode")] + private const float k_serverTickRate = 60f; private const int k_rngSeed = 6969; private const float k_sendRopeDataDelay = 8f; public float k_ropeReconciliateThreshold = 10f; private System.Random rng = new System.Random(k_rngSeed); private CountdownTimer ropeSendTimer; + private int currentTick => NetworkManager.Singleton.NetworkTickSystem.LocalTime.Tick; private int[] order; @@ -97,18 +99,22 @@ public class RopeSimulator : NetworkBehaviour } ropeSendTimer = new(k_sendRopeDataDelay); + // ropeSendTimer.OnTimerStop += SendRopeData; ropeSendTimer.Start(); + } private void OnEnable() { GameManager.OnPlayersReady += PlayersReady; + NetworkManager.Singleton.NetworkTickSystem.Tick += SendGameState; } private void OnDisable() { GameManager.OnPlayersReady -= PlayersReady; + NetworkManager.Singleton.NetworkTickSystem.Tick -= SendGameState; } public void PlayersReady(GameObject[] players) @@ -293,6 +299,24 @@ public class RopeSimulator : NetworkBehaviour // } } + [Rpc(SendTo.NotServer)] + private void ServerToClientGameStateRpc(GameState serverState) + { + Debug.Log($"Received server state. Server tick: {serverState.tick}, client: {currentTick}"); + } + + private void SendGameState() + { + if (!IsServer) return; + + GameState serverState = new() { + tick = currentTick, + nrope = Rope.ToNetworkRope(this.rope) + }; + + ServerToClientGameStateRpc(serverState); + } + private void SendRopeData() { if (!IsServer) return; @@ -319,7 +343,6 @@ public class RopeSimulator : NetworkBehaviour float ropeDiff = Simulate(Time.fixedDeltaTime); ropeSendTimer.Tick(Time.deltaTime); - Debug.Log(ropeSendTimer.Progress); // Update the rope collider positions for (int i = 0; i < rope.points.Length; i++) @@ -363,7 +386,7 @@ public class RopeSimulator : NetworkBehaviour } DrawRope(); - SendRopeData(); + // SendRopeData(); } private void PlayerPullAnimation(float overshoot)