Send game state to client rpc with tick info.

Still WIP on fields in game state (should include player pos, enemies in future)
This commit is contained in:
Sveske_Juice 2024-03-17 17:55:35 +01:00
parent 2d7d7f2d4e
commit 35117a5d8b
3 changed files with 41 additions and 2 deletions

View File

@ -0,0 +1,14 @@
using Unity.Netcode;
[System.Serializable]
public struct GameState : INetworkSerializable
{
public int tick;
public NetworkRope nrope;
public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
{
serializer.SerializeValue(ref tick);
nrope.NetworkSerialize(serializer);
}
}

View File

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 1cdca75ffd9bfc5b9b1c9ffe1a501dcf

View File

@ -66,11 +66,13 @@ public class RopeSimulator : NetworkBehaviour
[SerializeField] float pullAnimationOvershootThreshold = 0.2f; [SerializeField] float pullAnimationOvershootThreshold = 0.2f;
[Header("Netcode")] [Header("Netcode")]
private const float k_serverTickRate = 60f;
private const int k_rngSeed = 6969; private const int k_rngSeed = 6969;
private const float k_sendRopeDataDelay = 8f; private const float k_sendRopeDataDelay = 8f;
public float k_ropeReconciliateThreshold = 10f; public float k_ropeReconciliateThreshold = 10f;
private System.Random rng = new System.Random(k_rngSeed); private System.Random rng = new System.Random(k_rngSeed);
private CountdownTimer ropeSendTimer; private CountdownTimer ropeSendTimer;
private int currentTick => NetworkManager.Singleton.NetworkTickSystem.LocalTime.Tick;
private int[] order; private int[] order;
@ -97,18 +99,22 @@ public class RopeSimulator : NetworkBehaviour
} }
ropeSendTimer = new(k_sendRopeDataDelay); ropeSendTimer = new(k_sendRopeDataDelay);
// ropeSendTimer.OnTimerStop += SendRopeData; // ropeSendTimer.OnTimerStop += SendRopeData;
ropeSendTimer.Start(); ropeSendTimer.Start();
} }
private void OnEnable() private void OnEnable()
{ {
GameManager.OnPlayersReady += PlayersReady; GameManager.OnPlayersReady += PlayersReady;
NetworkManager.Singleton.NetworkTickSystem.Tick += SendGameState;
} }
private void OnDisable() private void OnDisable()
{ {
GameManager.OnPlayersReady -= PlayersReady; GameManager.OnPlayersReady -= PlayersReady;
NetworkManager.Singleton.NetworkTickSystem.Tick -= SendGameState;
} }
public void PlayersReady(GameObject[] players) 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() private void SendRopeData()
{ {
if (!IsServer) return; if (!IsServer) return;
@ -319,7 +343,6 @@ public class RopeSimulator : NetworkBehaviour
float ropeDiff = Simulate(Time.fixedDeltaTime); float ropeDiff = Simulate(Time.fixedDeltaTime);
ropeSendTimer.Tick(Time.deltaTime); ropeSendTimer.Tick(Time.deltaTime);
Debug.Log(ropeSendTimer.Progress);
// Update the rope collider positions // Update the rope collider positions
for (int i = 0; i < rope.points.Length; i++) for (int i = 0; i < rope.points.Length; i++)
@ -363,7 +386,7 @@ public class RopeSimulator : NetworkBehaviour
} }
DrawRope(); DrawRope();
SendRopeData(); // SendRopeData();
} }
private void PlayerPullAnimation(float overshoot) private void PlayerPullAnimation(float overshoot)