rebuild rope on shrink extend WIP

This commit is contained in:
Sveske Juice 2024-02-03 07:58:14 -08:00
parent b2e26ce8d2
commit e1a44344fe
4 changed files with 66 additions and 23 deletions

View File

@ -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:

View File

@ -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<int> ropeLengthShrinken;
public event Action<int> 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
{

View File

@ -7,6 +7,13 @@ public class RopeBuilder
List<Point> points = new();
List<Stick> sticks = new();
public RopeBuilder() { }
public RopeBuilder(List<Point> points, List<Stick> sticks)
{
this.points = points;
this.sticks = sticks;
}
public RopeBuilder AddPoint(Point point)
{
points.Add(point);

View File

@ -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<Stick>());
// 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<Stick>());
// 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;