Advertisement
Guest User

Untitled

a guest
Oct 1st, 2023
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.60 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using Obi;
  4. using FishNet.Object;
  5. using FishNet.Object.Synchronizing;
  6. using FishNet.Object.Synchronizing.Internal;
  7.  
  8. public class RopeSync : NetworkBehaviour
  9. {
  10.     ObiRope rope;
  11.     public ObiPathSmoother pathsmooth;
  12.     public ObiRopeExtrudedRenderer obiRopeExtrudedRenderer;
  13.  
  14.     [SyncObject]
  15.     private readonly SyncList<Vector4> Net_position = new SyncList<Vector4>();
  16.     [SyncObject]
  17.     private readonly SyncList<Vector4> Net_velocity = new SyncList<Vector4>();
  18.     [SyncObject]
  19.     private readonly SyncList<Vector4> Net_startPosition = new SyncList<Vector4>();
  20.     [SyncObject]
  21.     private readonly SyncList<Vector4> Net_prevPosition = new SyncList<Vector4>();
  22.  
  23.     public void OnEnable()
  24.     {
  25.  
  26.         GetComponent<ObiActor>().OnEndStep += EndStep;
  27.  
  28.     }
  29.  
  30.     public void OnDisable()
  31.     {
  32.  
  33.         GetComponent<ObiActor>().OnEndStep -= EndStep;
  34.  
  35.     }
  36.  
  37.     void EndStep(ObiActor actor, float stepTime)
  38.     {
  39.  
  40.         if (IsServer)
  41.         {
  42.  
  43.             // Server sends rope states (positions, velocities, startPos, and prevPos) to clients.
  44.             for (int i = 0; i < rope.solverIndices.Length; i++)
  45.             {
  46.                 Vector4 position = rope.solver.positions[rope.solverIndices[i]];
  47.                 Vector4 velocity = rope.solver.velocities[rope.solverIndices[i]];
  48.                 Vector4 startPositions = rope.solver.startPositions[rope.solverIndices[i]];
  49.                 Vector4 prevPositions = rope.solver.prevPositions[rope.solverIndices[i]];
  50.  
  51.                 Net_position[i] = position;
  52.                 Net_velocity[i] = velocity;
  53.                 Net_startPosition[i] = startPositions;
  54.                 Net_prevPosition[i] = prevPositions;
  55.  
  56.             }
  57.             Net_position.DirtyAll();
  58.             Net_velocity.DirtyAll();
  59.             Net_startPosition.DirtyAll();
  60.             Net_prevPosition.DirtyAll();
  61.             // Call a client RPC to sync the rope states.
  62.         }
  63.         if (IsClientOnly)
  64.         {
  65.  
  66.             // Client receives rope states (positions, velocities, startPos, and prevPos) from the server.
  67.             for (int i = 0; i < rope.solverIndices.Length; i++)
  68.             {
  69.                 rope.solver.positions[i] = Net_position[i];
  70.                 rope.solver.velocities[i] = Net_velocity[i];
  71.                 rope.solver.startPositions[i] = Net_startPosition[i];
  72.                 rope.solver.prevPositions[i] = Net_prevPosition[i];
  73.             }
  74.  
  75.         }
  76.  
  77.  
  78.     }
  79.     private void FixedUpdate()
  80.     {
  81.         if (IsClientOnly)
  82.         {
  83.             pathsmooth.GenerateSmoothChunks(rope, pathsmooth.smoothing);
  84.             rope.UpdateParticleProperties();
  85.             obiRopeExtrudedRenderer.UpdateRenderer(rope);
  86.         }
  87.     }
  88.     private void Start()
  89.     {
  90.         rope = GetComponent<ObiRope>();
  91.         obiRopeExtrudedRenderer = GetComponent<ObiRopeExtrudedRenderer>();
  92.         pathsmooth = GetComponent<ObiPathSmoother>();
  93.  
  94.         if (IsServer)
  95.         {
  96.             for (int i = 0; i < rope.solverIndices.Length; i++)
  97.             {
  98.                 //init the length of the rope
  99.                 Net_position.Add(Vector4.zero); //initialize to 0,0,0,0
  100.                 Net_velocity.Add(Vector4.zero);
  101.                 Net_startPosition.Add(Vector4.zero);
  102.                 Net_prevPosition.Add(Vector4.zero);
  103.             }
  104.         }
  105.  
  106.         if (IsClientOnly)
  107.         {
  108.  
  109.             for (int i = 0; i < rope.solverIndices.Length; i++)
  110.             {
  111.                 rope.solver.invMasses[i] = 0f; //remove all mass on client
  112.             }
  113.  
  114.         }
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement