Rope now only simulated on server

This commit is contained in:
Sveske_Juice 2024-02-28 20:25:01 +01:00
parent a5e033535e
commit bbbe287c1d
3 changed files with 34 additions and 31 deletions

1
.gitignore vendored
View File

@ -72,3 +72,4 @@ crashlytics-build.properties
/[Aa]ssets/[Ss]treamingAssets/aa.meta
/[Aa]ssets/[Ss]treamingAssets/aa/*
.vscode/

View File

@ -20,10 +20,6 @@ public class GameManager : MonoBehaviour
}
Instance = this;
}
void Start()
{
StartCoroutine(WaitForPlayers());
}

View File

@ -74,7 +74,10 @@ public class RopeSimulator : NetworkBehaviour
public static RopeSimulator instance;
private bool IsInitialized => !(start == null || end == null);
private bool IsInitialized => start != null || end != null;
// TODO: also true in single player mode
private bool ShouldSimulate => IsHost;
private void Awake()
{
@ -86,24 +89,11 @@ public class RopeSimulator : NetworkBehaviour
{
Destroy(instance);
}
GameManager.OnPlayersReady += PlayersReady;
}
private void OnEnable()
{
// Example of using rope builder
//rope = new RopeBuilder()
// .AddPoint(new Point(testPos, locked: true))
// .AddPoint(new Point(testPos.Add(x:5f)))
// .AddPoint(new Point(testPos.Add(x: 10f)))
// .AddPoint(new Point(testPos.Add(x: 15f)))
// .AddPoint(new Point(testPos.Add(x: 20f)))
// .ConnectPoints(0, 1)
// .ConnectPoints(1, 2)
// .ConnectPoints(2, 3)
// .ConnectPoints(3, 4)
// .Build();
GameManager.OnPlayersReady += PlayersReady;
}
private void OnDisable()
@ -113,14 +103,17 @@ public class RopeSimulator : NetworkBehaviour
public void PlayersReady(GameObject[] players)
{
//if (IsInitialized) return;
Debug.Log(players[0].name);
BuildRope(players[0].GetComponent<RopeJoint>(), players[1].GetComponent<RopeJoint>());
if (ShouldSimulate)
{
Debug.Log(players[0].name);
BuildRope(players[0].GetComponent<RopeJoint>(), players[1].GetComponent<RopeJoint>());
}
}
public void BuildRope(RopeJoint start, RopeJoint end)
{
Assert.IsTrue(ShouldSimulate, "Should not try build rope on client!");
Assert.IsNotNull(start);
Assert.IsNotNull(end);
@ -150,6 +143,8 @@ public class RopeSimulator : NetworkBehaviour
void ShrinkenRope(int playerNumber)
{
Assert.IsTrue(ShouldSimulate, "Should not shrink rope on client!");
int prevSubDivision = (int) subDivision;
subDivision -= ropeShrinkSpeed * Time.deltaTime;
subDivision = Mathf.Clamp(subDivision, ropeMinLength, ropeMaxLength);
@ -176,6 +171,8 @@ public class RopeSimulator : NetworkBehaviour
void ExtendRope(int playerNumber)
{
Assert.IsTrue(ShouldSimulate, "Should not extend rope on client!");
int prevSubDivision = (int)subDivision;
subDivision += ropeExtendSpeed * Time.deltaTime;
subDivision = Mathf.Clamp(subDivision, ropeMinLength, ropeMaxLength);
@ -201,8 +198,10 @@ public class RopeSimulator : NetworkBehaviour
CreateOrderArray();
}
private void OnDestroy()
public override void OnDestroy()
{
base.OnDestroy();
// May never have been initialized
if (!IsInitialized) return;
@ -215,6 +214,8 @@ public class RopeSimulator : NetworkBehaviour
private void Rebuild()
{
Assert.IsTrue(ShouldSimulate, "Should not re-build on clients!");
Debug.Log("rebuild");
RopeBuilder builder = new RopeBuilder();
@ -244,12 +245,7 @@ public class RopeSimulator : NetworkBehaviour
private void RebuildRopeColliders()
{
// Benjamin forgor... to assign
if (ropeCollidersParent == null)
{
var holder = new GameObject("RopeColiderHolder");
ropeCollidersParent = holder.transform;
}
Assert.IsTrue(ShouldSimulate, "Should not build rope colliders on client!");
for (int i = 0; i < ropeCollidersParent.childCount; i++)
{
@ -281,6 +277,9 @@ public class RopeSimulator : NetworkBehaviour
if (!IsInitialized)
return;
if (!ShouldSimulate)
return;
colliderToSquezeForce.Clear();
rope.points.First().position = start.position;
@ -337,6 +336,8 @@ public class RopeSimulator : NetworkBehaviour
private void PlayerPullAnimation(float overshoot)
{
Assert.IsTrue(ShouldSimulate);
if (overshoot > pullAnimationOvershootThreshold)
{
float startDot = Vector2.Dot((start.position - rope.points[1].position).normalized, start.playerInput.movement);
@ -361,6 +362,8 @@ public class RopeSimulator : NetworkBehaviour
private void PullPlayers(float overshoot)
{
Assert.IsTrue(ShouldSimulate);
if (overshoot <= 0f) return;
//start.position = prevStartPos;
@ -404,6 +407,8 @@ public class RopeSimulator : NetworkBehaviour
void Simulate()
{
Assert.IsTrue(ShouldSimulate, "Should not simulate rope on client!");
foreach (Point p in rope.points)
{
if (!p.locked)
@ -446,6 +451,7 @@ public class RopeSimulator : NetworkBehaviour
private void TryMovePointToPosition(Point point, Vector3 position)
{
Assert.IsTrue(ShouldSimulate);
Vector2 moveDir = new Vector2(position.x, position.y) - new Vector2(point.position.x, point.position.y);
int stepsRequired = (int) Mathf.Ceil(moveDir.magnitude / collisionCheckDist);
moveDir.Normalize();
@ -484,6 +490,7 @@ public class RopeSimulator : NetworkBehaviour
private void HandleStaticCollidersOfPoint(Point p)
{
Assert.IsTrue(ShouldSimulate);
foreach (var hitCollider in Physics2D.OverlapCircleAll(p.position, ropeRadius*1.1f, staticColliderMask))
{
if (hitCollider == null) continue;
@ -517,7 +524,6 @@ public class RopeSimulator : NetworkBehaviour
public static T[] ShuffleArray<T>(T[] array, System.Random prng)
{
int elementsRemainingToShuffle = array.Length;
int randomIndex = 0;