Save ropestate and send RPC

This commit is contained in:
Sveske_Juice 2024-05-27 12:19:16 +02:00
parent 99aa0647f2
commit 15d0079a2d
3 changed files with 63 additions and 4 deletions

View File

@ -2240,6 +2240,7 @@ GameObject:
- component: {fileID: 34894283} - component: {fileID: 34894283}
- component: {fileID: 34894284} - component: {fileID: 34894284}
- component: {fileID: 34894285} - component: {fileID: 34894285}
- component: {fileID: 34894286}
m_Layer: 0 m_Layer: 0
m_Name: Rope m_Name: Rope
m_TagString: Untagged m_TagString: Untagged
@ -2275,8 +2276,6 @@ MonoBehaviour:
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
solveIterations: 10 solveIterations: 10
ropeStart: {fileID: 0}
ropeEnd: {fileID: 0}
subDivision: 20 subDivision: 20
distBetweenRopePoints: 1.5 distBetweenRopePoints: 1.5
lineRenderer: {fileID: 34894285} lineRenderer: {fileID: 34894285}
@ -2390,6 +2389,27 @@ LineRenderer:
m_UseWorldSpace: 1 m_UseWorldSpace: 1
m_Loop: 0 m_Loop: 0
m_ApplyActiveColorSpace: 1 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 --- !u!1001 &37294573
PrefabInstance: PrefabInstance:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

View File

@ -87,6 +87,7 @@ public class HealthComponent : MonoBehaviour, ISquezeDamageReceiver
void Update() void Update()
{ {
return;
// blod regen // blod regen
if (bloodRegen) if (bloodRegen)
{ {

View File

@ -3,7 +3,7 @@ using UnityEngine.Assertions;
using System.Linq; using System.Linq;
using Unity.Netcode; using Unity.Netcode;
public class RopeSimulator : MonoBehaviour public class RopeSimulator : NetworkBehaviour
{ {
[Header("Solver")] [Header("Solver")]
[SerializeField] int solveIterations = 10; [SerializeField] int solveIterations = 10;
@ -20,13 +20,14 @@ public class RopeSimulator : MonoBehaviour
[Header("Networking")] [Header("Networking")]
[SerializeField] CircularBuffer<RopeState> stateBuffer; [SerializeField] CircularBuffer<RopeState> stateBuffer;
[SerializeField] float reconciliateThreshold = 1f;
private const int k_stateBufferSize = 128; private const int k_stateBufferSize = 128;
private int currentTick => NetworkManager.Singleton.NetworkTickSystem.LocalTime.Tick; private int currentTick => NetworkManager.Singleton.NetworkTickSystem.LocalTime.Tick;
private void Awake() private void Awake()
{ {
this.rope = null;
stateBuffer = new CircularBuffer<RopeState>(k_stateBufferSize); stateBuffer = new CircularBuffer<RopeState>(k_stateBufferSize);
} }
private void OnEnable() private void OnEnable()
@ -37,10 +38,15 @@ public class RopeSimulator : MonoBehaviour
private void OnDisable() private void OnDisable()
{ {
GameManager.OnPlayersReady += Init; GameManager.OnPlayersReady += Init;
if (NetworkManager.Singleton != null)
NetworkManager.Singleton.NetworkTickSystem.Tick -= NetworkTick;
} }
private void Update() private void Update()
{ {
if (rope == null) return;
rope.points.First().position = ropeStart.position; rope.points.First().position = ropeStart.position;
rope.points.Last().position = ropeEnd.position; rope.points.Last().position = ropeEnd.position;
Simulate(this.rope, Time.deltaTime, this.distBetweenRopePoints, this.solveIterations); Simulate(this.rope, Time.deltaTime, this.distBetweenRopePoints, this.solveIterations);
@ -54,6 +60,38 @@ public class RopeSimulator : MonoBehaviour
this.ropeEnd = players[1].transform; this.ropeEnd = players[1].transform;
this.rope = RopeSimulator.BuildRope(this.ropeStart, this.ropeEnd, this.subDivision, this.distBetweenRopePoints); 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() private void DisplayRope()