From 15d0079a2dbf52ca9ae01624866d44e4df3a289d Mon Sep 17 00:00:00 2001 From: Sveske_Juice Date: Mon, 27 May 2024 12:19:16 +0200 Subject: [PATCH] Save ropestate and send RPC --- Assets/Scenes/RopeOverhaulGame.unity | 24 ++++++++++- Assets/Scripts/HeathSystem/HealthComponent.cs | 1 + Assets/Scripts/Rope/RopeSimulator.cs | 42 ++++++++++++++++++- 3 files changed, 63 insertions(+), 4 deletions(-) diff --git a/Assets/Scenes/RopeOverhaulGame.unity b/Assets/Scenes/RopeOverhaulGame.unity index efb6844..76318a0 100644 --- a/Assets/Scenes/RopeOverhaulGame.unity +++ b/Assets/Scenes/RopeOverhaulGame.unity @@ -2240,6 +2240,7 @@ GameObject: - component: {fileID: 34894283} - component: {fileID: 34894284} - component: {fileID: 34894285} + - component: {fileID: 34894286} m_Layer: 0 m_Name: Rope m_TagString: Untagged @@ -2275,8 +2276,6 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: solveIterations: 10 - ropeStart: {fileID: 0} - ropeEnd: {fileID: 0} subDivision: 20 distBetweenRopePoints: 1.5 lineRenderer: {fileID: 34894285} @@ -2390,6 +2389,27 @@ LineRenderer: m_UseWorldSpace: 1 m_Loop: 0 m_ApplyActiveColorSpace: 1 +--- !u!114 &34894286 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 34894282} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: d5a57f767e5e46a458fc5d3c628d0cbb, type: 3} + m_Name: + m_EditorClassIdentifier: + GlobalObjectIdHash: 1869519831 + InScenePlacedSourceGlobalObjectIdHash: 0 + AlwaysReplicateAsRoot: 0 + SynchronizeTransform: 1 + ActiveSceneSynchronization: 0 + SceneMigrationSynchronization: 1 + SpawnWithObservers: 1 + DontDestroyWithOwner: 0 + AutoObjectParentSync: 1 --- !u!1001 &37294573 PrefabInstance: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/HeathSystem/HealthComponent.cs b/Assets/Scripts/HeathSystem/HealthComponent.cs index 72459e6..4baeeee 100644 --- a/Assets/Scripts/HeathSystem/HealthComponent.cs +++ b/Assets/Scripts/HeathSystem/HealthComponent.cs @@ -87,6 +87,7 @@ public class HealthComponent : MonoBehaviour, ISquezeDamageReceiver void Update() { + return; // blod regen if (bloodRegen) { diff --git a/Assets/Scripts/Rope/RopeSimulator.cs b/Assets/Scripts/Rope/RopeSimulator.cs index f988adc..1795af7 100644 --- a/Assets/Scripts/Rope/RopeSimulator.cs +++ b/Assets/Scripts/Rope/RopeSimulator.cs @@ -3,7 +3,7 @@ using UnityEngine.Assertions; using System.Linq; using Unity.Netcode; -public class RopeSimulator : MonoBehaviour +public class RopeSimulator : NetworkBehaviour { [Header("Solver")] [SerializeField] int solveIterations = 10; @@ -20,13 +20,14 @@ public class RopeSimulator : MonoBehaviour [Header("Networking")] [SerializeField] CircularBuffer stateBuffer; + [SerializeField] float reconciliateThreshold = 1f; private const int k_stateBufferSize = 128; private int currentTick => NetworkManager.Singleton.NetworkTickSystem.LocalTime.Tick; private void Awake() { + this.rope = null; stateBuffer = new CircularBuffer(k_stateBufferSize); - } private void OnEnable() @@ -37,10 +38,15 @@ public class RopeSimulator : MonoBehaviour private void OnDisable() { GameManager.OnPlayersReady += Init; + + if (NetworkManager.Singleton != null) + NetworkManager.Singleton.NetworkTickSystem.Tick -= NetworkTick; } private void Update() { + if (rope == null) return; + rope.points.First().position = ropeStart.position; rope.points.Last().position = ropeEnd.position; Simulate(this.rope, Time.deltaTime, this.distBetweenRopePoints, this.solveIterations); @@ -54,6 +60,38 @@ public class RopeSimulator : MonoBehaviour this.ropeEnd = players[1].transform; this.rope = RopeSimulator.BuildRope(this.ropeStart, this.ropeEnd, this.subDivision, this.distBetweenRopePoints); + NetworkManager.Singleton.NetworkTickSystem.Tick += NetworkTick; + } + + private void NetworkTick() + { + RopeState ropeState = new() + { + tick = currentTick, + nrope = Rope.ToNetworkRope(this.rope) + }; + + stateBuffer.Add(ropeState, currentTick); + + if (IsServer) + { + ServerToClientRopeStateRpc(ropeState); + } + } + + [Rpc(SendTo.NotServer)] + private void ServerToClientRopeStateRpc(RopeState serverState) + { + RopeState clientState = this.stateBuffer.Get(serverState.tick); + Rope serverRope = Rope.FromNetworkRope(serverState.nrope, this.distBetweenRopePoints); + Rope previousClientRope = Rope.FromNetworkRope(clientState.nrope, this.distBetweenRopePoints); + float diff = Rope.CalcDiff(previousClientRope, serverRope); + Debug.Log($"Diff: {diff}, ({serverState.tick})/({clientState.tick})"); + + if (diff > reconciliateThreshold) + { + this.rope = serverRope; + } } private void DisplayRope()