From ae60d7b1b47d2e8d9e6eac3713c14beab5098f72 Mon Sep 17 00:00:00 2001 From: Sveske_Juice Date: Wed, 14 Feb 2024 14:37:29 +0100 Subject: [PATCH] Build rope from code --- Assets/Scripts/Rope/RopeSimulator.cs | 36 ++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/Assets/Scripts/Rope/RopeSimulator.cs b/Assets/Scripts/Rope/RopeSimulator.cs index cb3fca0..dcf9315 100644 --- a/Assets/Scripts/Rope/RopeSimulator.cs +++ b/Assets/Scripts/Rope/RopeSimulator.cs @@ -72,6 +72,7 @@ public class RopeSimulator : MonoBehaviour private void Start() { + // Example of using rope builder //rope = new RopeBuilder() // .AddPoint(new Point(testPos, locked: true)) // .AddPoint(new Point(testPos.Add(x:5f))) @@ -83,13 +84,36 @@ public class RopeSimulator : MonoBehaviour // .ConnectPoints(2, 3) // .ConnectPoints(3, 4) // .Build(); + + // Build rope if rope joints specified in inspector + if (start != null && end != null) + BuildRope(start, end); + } + + public void BuildRope(RopeJoint start, RopeJoint end) + { + // Sanity check if rope simulator was initialized before + if (this.start != null) + { + start.playerInput.ropeLengthExtend -= ExtendRope; + start.playerInput.ropeLengthShrinken -= ShrinkenRope; + } + if (this.end != null) + { + end.playerInput.ropeLengthExtend -= ExtendRope; + end.playerInput.ropeLengthShrinken -= ShrinkenRope; + } + + this.start = start; + this.end = end; + Rebuild(); - start.playerInput.ropeLengthShrinken += ShrinkenRope; - end.playerInput.ropeLengthShrinken += ShrinkenRope; + this.start.playerInput.ropeLengthShrinken += ShrinkenRope; + this.end.playerInput.ropeLengthShrinken += ShrinkenRope; - start.playerInput.ropeLengthExtend += ExtendRope; - end.playerInput.ropeLengthExtend += ExtendRope; + this.start.playerInput.ropeLengthExtend += ExtendRope; + this.end.playerInput.ropeLengthExtend += ExtendRope; } void ShrinkenRope(int playerNumber) @@ -215,6 +239,10 @@ public class RopeSimulator : MonoBehaviour private void Update() { + // Dont update if no rope is initialized + if (start == null || end == null) + return; + //Debug.Log($"overshoot: {rope.CalculateLengthOvershoot()}"); //ShrinkenRope(1); //ExtendRope(0);