From 007e762a47c3e731b495d28b5fcd467db29369e6 Mon Sep 17 00:00:00 2001 From: BOTAlex Date: Sun, 26 May 2024 03:29:03 +0200 Subject: [PATCH] Fixed rope startup errors --- Assets/Scripts/Rope/RopeSimulator.cs | 35 ++++++++++++++++------------ 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/Assets/Scripts/Rope/RopeSimulator.cs b/Assets/Scripts/Rope/RopeSimulator.cs index d4ec2af..23dedca 100644 --- a/Assets/Scripts/Rope/RopeSimulator.cs +++ b/Assets/Scripts/Rope/RopeSimulator.cs @@ -108,13 +108,17 @@ public class RopeSimulator : NetworkBehaviour private void OnEnable() { GameManager.OnPlayersReady += PlayersReady; - NetworkManager.Singleton.NetworkTickSystem.Tick += NetworkTick; + + if (NetworkManager.Singleton != null) + NetworkManager.Singleton.NetworkTickSystem.Tick += NetworkTick; } private void OnDisable() { GameManager.OnPlayersReady -= PlayersReady; - NetworkManager.Singleton.NetworkTickSystem.Tick -= NetworkTick; + + if (NetworkManager.Singleton != null) + NetworkManager.Singleton.NetworkTickSystem.Tick -= NetworkTick; } public void PlayersReady(GameObject[] players) @@ -153,12 +157,12 @@ public class RopeSimulator : NetworkBehaviour void ShrinkenRope(int playerNumber) { - int prevSubDivision = (int) subDivision; + int prevSubDivision = (int)subDivision; subDivision -= ropeShrinkSpeed * Time.deltaTime; subDivision = Mathf.Clamp(subDivision, ropeMinLength, ropeMaxLength); // Only shrinken if the numeric value has changed - if (prevSubDivision - (int) subDivision <= 0) return; + if (prevSubDivision - (int)subDivision <= 0) return; // Shrink from rope point after start rope joint List newPoints = new(rope.points.Length - 1); @@ -170,7 +174,7 @@ public class RopeSimulator : NetworkBehaviour var builder = new RopeBuilder(newPoints, new List()); // Re-gen sticks - for (int i = 0; i < (int) subDivision; i++) + for (int i = 0; i < (int)subDivision; i++) { builder.ConnectPointsWithDesiredLength(i, i + 1, distBetweenRopePoints); } @@ -187,10 +191,10 @@ public class RopeSimulator : NetworkBehaviour subDivision = Mathf.Clamp(subDivision, ropeMinLength, ropeMaxLength); // Only extend if the numeric value has changed - if (prevSubDivision - (int) subDivision >= 0) return; + if (prevSubDivision - (int)subDivision >= 0) return; // Extend from rope point after start rope point - List newPoints = new (rope.points.Length + 1); + List newPoints = new(rope.points.Length + 1); newPoints.Add(new Point(rope.points[1].position)); for (int i = 1; i < rope.points.Length; i++) { @@ -233,7 +237,7 @@ public class RopeSimulator : NetworkBehaviour builder.AddPoint(new Point(start.position, locked: true)); // Build rope points - for (int i = 1; i < (int) subDivision; i++) + for (int i = 1; i < (int)subDivision; i++) { Vector3 pointPos = Vector3.Lerp(start.position, end.position, (float)i / Mathf.Floor(subDivision)); Debug.DrawRay(pointPos, (end.position - start.position).normalized); @@ -243,7 +247,7 @@ public class RopeSimulator : NetworkBehaviour builder.AddPoint(new Point(end.position, locked: true)); // Connect rope points - for (int i = 0; i < (int) subDivision; i++) + for (int i = 0; i < (int)subDivision; i++) { builder.ConnectPointsWithDesiredLength(i, i + 1, desiredLength: distBetweenRopePoints); } @@ -348,7 +352,7 @@ public class RopeSimulator : NetworkBehaviour foreach (var enemyPos in intermediateState.enemyPositions) { // Find corresponding client enemy with id (z-component) - ulong enemyID = (ulong) enemyPos.z; + ulong enemyID = (ulong)enemyPos.z; GameObject enemy = enemyByIds.GetValueOrDefault(enemyID); Assert.IsNotNull(enemy, $"Server enemy with id: {enemyID} could not be found on client!"); @@ -358,7 +362,7 @@ public class RopeSimulator : NetworkBehaviour foreach (var playerPos in intermediateState.playerPositions) { // Find corresponding client player with id (z-component) - ulong playerID = (ulong) playerPos.z; + ulong playerID = (ulong)playerPos.z; GameObject player = playerByIds.GetValueOrDefault(playerID); Assert.IsNotNull(player, $"Server player with id: {playerID} could not be found on client!"); @@ -384,7 +388,8 @@ public class RopeSimulator : NetworkBehaviour private GameState ProcessGame() { - GameState localState = new() { + GameState localState = new() + { tick = currentTick, nrope = Rope.ToNetworkRope(this.rope), enemyPositions = GameObject.FindObjectsByType(FindObjectsSortMode.None).Select(e => new Vector3(e.transform.position.x, e.transform.position.y, e.GetComponent().NetworkObjectId)).ToArray(), @@ -608,12 +613,12 @@ public class RopeSimulator : NetworkBehaviour private void TryMovePointToPosition(Point point, Vector3 position) { 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(); Vector2 initialPos = new Vector2(point.position.x, point.position.y); bool shouldBreak = false; - for (int i = 0 ; i < stepsRequired; i++) + for (int i = 0; i < stepsRequired; i++) { Vector2 newPos = Vector2.MoveTowards(new Vector2(point.position.x, point.position.y), new Vector2(position.x, position.y), collisionCheckDist); point.position.Set(newPos.x, newPos.y, point.position.z); @@ -645,7 +650,7 @@ public class RopeSimulator : NetworkBehaviour private void HandleStaticCollidersOfPoint(Point p) { - 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.isTrigger) continue;