From e1a44344fef1068ab4007c76c4e6030ddb942dca Mon Sep 17 00:00:00 2001 From: Sveske Juice Date: Sat, 3 Feb 2024 07:58:14 -0800 Subject: [PATCH] rebuild rope on shrink extend WIP --- Assets/Scenes/EnemyRope.unity | 16 +++------ Assets/Scripts/Player/PlayerInput.cs | 12 ++++--- Assets/Scripts/Rope/RopeBuilder.cs | 7 ++++ Assets/Scripts/Rope/RopeSimulator.cs | 54 ++++++++++++++++++++++++---- 4 files changed, 66 insertions(+), 23 deletions(-) diff --git a/Assets/Scenes/EnemyRope.unity b/Assets/Scenes/EnemyRope.unity index a62fe33..76982f4 100644 --- a/Assets/Scenes/EnemyRope.unity +++ b/Assets/Scenes/EnemyRope.unity @@ -868,6 +868,7 @@ MonoBehaviour: anchor: {fileID: 625885769} body: {fileID: 0} locked: 0 + playerInput: {fileID: 0} --- !u!1 &646449334 GameObject: m_ObjectHideFlags: 0 @@ -1103,17 +1104,6 @@ MonoBehaviour: m_Area: 0 m_IgnoreFromBuild: 0 m_AffectedAgents: ffffffff ---- !u!114 &1081435768 stripped -MonoBehaviour: - m_CorrespondingSourceObject: {fileID: 1949941092232239315, guid: 99a6ff8b9591949439b620b13bd249a4, type: 3} - m_PrefabInstance: {fileID: 1273044612} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 0} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: de340bb967770a7499e140a52a287f84, type: 3} - m_Name: - m_EditorClassIdentifier: --- !u!1 &1156361827 GameObject: m_ObjectHideFlags: 0 @@ -2075,7 +2065,6 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 27ac133d9e10e544ba603e07122e3359, type: 3} m_Name: m_EditorClassIdentifier: - playerInput: {fileID: 1081435768} gravity: 15 solveIterations: 10 constrainStickMinLength: 0 @@ -2092,6 +2081,9 @@ MonoBehaviour: m_Bits: 1 pullForce: 65 xyGravityDampScalor: 1 + ropeExtendSpeed: 15 + ropeShrinkSpeed: 15 + ropeMaxLength: 0 lineRenderer: {fileID: 1647138191} --- !u!4 &1647138193 Transform: diff --git a/Assets/Scripts/Player/PlayerInput.cs b/Assets/Scripts/Player/PlayerInput.cs index 37831f8..fc63bad 100644 --- a/Assets/Scripts/Player/PlayerInput.cs +++ b/Assets/Scripts/Player/PlayerInput.cs @@ -10,11 +10,12 @@ public class PlayerInput : MonoBehaviour [SerializeField] private int playerNumber; public Vector2 movement; + public Vector2 look; public Gamepad controller { get; private set; } public bool whipAttack; - public event Action ropeLengthShrinken; - public event Action ropeLengthExtend; + public event Action ropeLengthShrinken; + public event Action ropeLengthExtend; public int PlayerNum => playerNumber; @@ -34,10 +35,13 @@ public class PlayerInput : MonoBehaviour movement.x = controller.leftStick.x.ReadValue(); movement.y = controller.leftStick.y.ReadValue(); + look.x = controller.rightStick.x.ReadValue(); + look.y = controller.rightStick.y.ReadValue(); + whipAttack = controller.buttonWest.IsPressed(); - if (controller.rightShoulder.IsPressed()) ropeLengthShrinken?.Invoke(); - if (controller.leftShoulder.IsPressed()) ropeLengthExtend?.Invoke(); + if (controller.rightShoulder.IsPressed()) ropeLengthShrinken?.Invoke(playerNumber); + if (controller.leftShoulder.IsPressed()) ropeLengthExtend?.Invoke(playerNumber); } else { diff --git a/Assets/Scripts/Rope/RopeBuilder.cs b/Assets/Scripts/Rope/RopeBuilder.cs index 6c57470..87ebc3a 100644 --- a/Assets/Scripts/Rope/RopeBuilder.cs +++ b/Assets/Scripts/Rope/RopeBuilder.cs @@ -7,6 +7,13 @@ public class RopeBuilder List points = new(); List sticks = new(); + public RopeBuilder() { } + public RopeBuilder(List points, List sticks) + { + this.points = points; + this.sticks = sticks; + } + public RopeBuilder AddPoint(Point point) { points.Add(point); diff --git a/Assets/Scripts/Rope/RopeSimulator.cs b/Assets/Scripts/Rope/RopeSimulator.cs index 8c6a8f4..a8d0777 100644 --- a/Assets/Scripts/Rope/RopeSimulator.cs +++ b/Assets/Scripts/Rope/RopeSimulator.cs @@ -50,11 +50,11 @@ public class RopeSimulator : MonoBehaviour [SerializeField] float xyGravityDampScalor = 1f; - [SerializeField, Range(0f, 5f)] + [SerializeField, Range(0f, 20f)] public float ropeExtendSpeed, ropeShrinkSpeed; [SerializeField] - public float ropeMaxLength; + public float ropeMaxLength, ropeMinLength; [Header("Rendering")] [SerializeField] LineRenderer lineRenderer; @@ -87,15 +87,54 @@ public class RopeSimulator : MonoBehaviour end.playerInput.ropeLengthExtend += ExtendRope; } - void ShrinkenRope() + void ShrinkenRope(int playerNumber) { - subDivision += ropeShrinkSpeed * Time.deltaTime; - Rebuild(); + int prevSubDivision = (int) subDivision; + subDivision -= ropeShrinkSpeed * Time.deltaTime; + if (subDivision < ropeMinLength) + subDivision = ropeMinLength; + + if (prevSubDivision - (int)subDivision > 0) return; + + // Shrink from start + if (playerNumber == start.playerInput.PlayerNum) + { + rope.sticks.Clear(); + rope.points.RemoveAt(0); + + var builder = new RopeBuilder(rope.points, new List()); + + // Re-gen sticks + for (int i = 0; i < (int) subDivision; i++) + { + builder.ConnectPoints(i, i + 1); + } + rope = builder.Build(); + } + // Shrink from end + else if (playerNumber == end.playerInput.PlayerNum) + { + rope.points.RemoveAt(rope.points.Count - 2); + rope.sticks.Clear(); + + var builder = new RopeBuilder(rope.points, new List()); + + // Re-gen sticks + for (int i = 0; i < (int)subDivision; i++) + { + builder.ConnectPoints(i, i + 1); + } + rope = builder.Build(); + } + CreateOrderArray(); } - void ExtendRope() + void ExtendRope(int playerNumber) { - subDivision -= ropeExtendSpeed * Time.deltaTime; + subDivision += ropeExtendSpeed * Time.deltaTime; + if (subDivision > ropeMaxLength) + subDivision = ropeMaxLength; + Rebuild(); } @@ -150,6 +189,7 @@ public class RopeSimulator : MonoBehaviour private void Update() { + ShrinkenRope(1); colliderToSquezeForce.Clear(); rope.points.First().position = start.position;