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:
parent
2d7d7f2d4e
commit
35117a5d8b
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 1cdca75ffd9bfc5b9b1c9ffe1a501dcf
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue