Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using UnityEngine;
- using Obi;
- using FishNet.Object;
- using FishNet.Object.Synchronizing;
- using FishNet.Object.Synchronizing.Internal;
- public class RopeSync : NetworkBehaviour
- {
- ObiRope rope;
- public ObiPathSmoother pathsmooth;
- public ObiRopeExtrudedRenderer obiRopeExtrudedRenderer;
- [SyncObject]
- private readonly SyncList<Vector4> Net_position = new SyncList<Vector4>();
- [SyncObject]
- private readonly SyncList<Vector4> Net_velocity = new SyncList<Vector4>();
- [SyncObject]
- private readonly SyncList<Vector4> Net_startPosition = new SyncList<Vector4>();
- [SyncObject]
- private readonly SyncList<Vector4> Net_prevPosition = new SyncList<Vector4>();
- public void OnEnable()
- {
- GetComponent<ObiActor>().OnEndStep += EndStep;
- }
- public void OnDisable()
- {
- GetComponent<ObiActor>().OnEndStep -= EndStep;
- }
- void EndStep(ObiActor actor, float stepTime)
- {
- if (IsServer)
- {
- // Server sends rope states (positions, velocities, startPos, and prevPos) to clients.
- for (int i = 0; i < rope.solverIndices.Length; i++)
- {
- Vector4 position = rope.solver.positions[rope.solverIndices[i]];
- Vector4 velocity = rope.solver.velocities[rope.solverIndices[i]];
- Vector4 startPositions = rope.solver.startPositions[rope.solverIndices[i]];
- Vector4 prevPositions = rope.solver.prevPositions[rope.solverIndices[i]];
- Net_position[i] = position;
- Net_velocity[i] = velocity;
- Net_startPosition[i] = startPositions;
- Net_prevPosition[i] = prevPositions;
- }
- Net_position.DirtyAll();
- Net_velocity.DirtyAll();
- Net_startPosition.DirtyAll();
- Net_prevPosition.DirtyAll();
- // Call a client RPC to sync the rope states.
- }
- if (IsClientOnly)
- {
- // Client receives rope states (positions, velocities, startPos, and prevPos) from the server.
- for (int i = 0; i < rope.solverIndices.Length; i++)
- {
- rope.solver.positions[i] = Net_position[i];
- rope.solver.velocities[i] = Net_velocity[i];
- rope.solver.startPositions[i] = Net_startPosition[i];
- rope.solver.prevPositions[i] = Net_prevPosition[i];
- }
- }
- }
- private void FixedUpdate()
- {
- if (IsClientOnly)
- {
- pathsmooth.GenerateSmoothChunks(rope, pathsmooth.smoothing);
- rope.UpdateParticleProperties();
- obiRopeExtrudedRenderer.UpdateRenderer(rope);
- }
- }
- private void Start()
- {
- rope = GetComponent<ObiRope>();
- obiRopeExtrudedRenderer = GetComponent<ObiRopeExtrudedRenderer>();
- pathsmooth = GetComponent<ObiPathSmoother>();
- if (IsServer)
- {
- for (int i = 0; i < rope.solverIndices.Length; i++)
- {
- //init the length of the rope
- Net_position.Add(Vector4.zero); //initialize to 0,0,0,0
- Net_velocity.Add(Vector4.zero);
- Net_startPosition.Add(Vector4.zero);
- Net_prevPosition.Add(Vector4.zero);
- }
- }
- if (IsClientOnly)
- {
- for (int i = 0; i < rope.solverIndices.Length; i++)
- {
- rope.solver.invMasses[i] = 0f; //remove all mass on client
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement