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.meta
/[Aa]ssets/[Ss]treamingAssets/aa/* /[Aa]ssets/[Ss]treamingAssets/aa/*
.vscode/

View File

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

View File

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