Fixed rope startup errors

This commit is contained in:
BOTAlex 2024-05-26 03:29:03 +02:00
parent 9017315bfe
commit 007e762a47
1 changed files with 20 additions and 15 deletions

View File

@ -108,13 +108,17 @@ public class RopeSimulator : NetworkBehaviour
private void OnEnable() private void OnEnable()
{ {
GameManager.OnPlayersReady += PlayersReady; GameManager.OnPlayersReady += PlayersReady;
NetworkManager.Singleton.NetworkTickSystem.Tick += NetworkTick;
if (NetworkManager.Singleton != null)
NetworkManager.Singleton.NetworkTickSystem.Tick += NetworkTick;
} }
private void OnDisable() private void OnDisable()
{ {
GameManager.OnPlayersReady -= PlayersReady; GameManager.OnPlayersReady -= PlayersReady;
NetworkManager.Singleton.NetworkTickSystem.Tick -= NetworkTick;
if (NetworkManager.Singleton != null)
NetworkManager.Singleton.NetworkTickSystem.Tick -= NetworkTick;
} }
public void PlayersReady(GameObject[] players) public void PlayersReady(GameObject[] players)
@ -153,12 +157,12 @@ public class RopeSimulator : NetworkBehaviour
void ShrinkenRope(int playerNumber) void ShrinkenRope(int playerNumber)
{ {
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);
// Only shrinken if the numeric value has changed // 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 // Shrink from rope point after start rope joint
List<Point> newPoints = new(rope.points.Length - 1); List<Point> newPoints = new(rope.points.Length - 1);
@ -170,7 +174,7 @@ public class RopeSimulator : NetworkBehaviour
var builder = new RopeBuilder(newPoints, new List<Stick>()); var builder = new RopeBuilder(newPoints, new List<Stick>());
// Re-gen sticks // 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); builder.ConnectPointsWithDesiredLength(i, i + 1, distBetweenRopePoints);
} }
@ -187,10 +191,10 @@ public class RopeSimulator : NetworkBehaviour
subDivision = Mathf.Clamp(subDivision, ropeMinLength, ropeMaxLength); subDivision = Mathf.Clamp(subDivision, ropeMinLength, ropeMaxLength);
// Only extend if the numeric value has changed // 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 // Extend from rope point after start rope point
List<Point> newPoints = new (rope.points.Length + 1); List<Point> newPoints = new(rope.points.Length + 1);
newPoints.Add(new Point(rope.points[1].position)); newPoints.Add(new Point(rope.points[1].position));
for (int i = 1; i < rope.points.Length; i++) 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)); builder.AddPoint(new Point(start.position, locked: true));
// Build rope points // 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)); Vector3 pointPos = Vector3.Lerp(start.position, end.position, (float)i / Mathf.Floor(subDivision));
Debug.DrawRay(pointPos, (end.position - start.position).normalized); Debug.DrawRay(pointPos, (end.position - start.position).normalized);
@ -243,7 +247,7 @@ public class RopeSimulator : NetworkBehaviour
builder.AddPoint(new Point(end.position, locked: true)); builder.AddPoint(new Point(end.position, locked: true));
// Connect rope points // 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); builder.ConnectPointsWithDesiredLength(i, i + 1, desiredLength: distBetweenRopePoints);
} }
@ -348,7 +352,7 @@ public class RopeSimulator : NetworkBehaviour
foreach (var enemyPos in intermediateState.enemyPositions) foreach (var enemyPos in intermediateState.enemyPositions)
{ {
// Find corresponding client enemy with id (z-component) // Find corresponding client enemy with id (z-component)
ulong enemyID = (ulong) enemyPos.z; ulong enemyID = (ulong)enemyPos.z;
GameObject enemy = enemyByIds.GetValueOrDefault(enemyID); GameObject enemy = enemyByIds.GetValueOrDefault(enemyID);
Assert.IsNotNull(enemy, $"Server enemy with id: {enemyID} could not be found on client!"); 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) foreach (var playerPos in intermediateState.playerPositions)
{ {
// Find corresponding client player with id (z-component) // Find corresponding client player with id (z-component)
ulong playerID = (ulong) playerPos.z; ulong playerID = (ulong)playerPos.z;
GameObject player = playerByIds.GetValueOrDefault(playerID); GameObject player = playerByIds.GetValueOrDefault(playerID);
Assert.IsNotNull(player, $"Server player with id: {playerID} could not be found on client!"); 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() private GameState ProcessGame()
{ {
GameState localState = new() { GameState localState = new()
{
tick = currentTick, tick = currentTick,
nrope = Rope.ToNetworkRope(this.rope), nrope = Rope.ToNetworkRope(this.rope),
enemyPositions = GameObject.FindObjectsByType<EnemyPathFinding>(FindObjectsSortMode.None).Select(e => new Vector3(e.transform.position.x, e.transform.position.y, e.GetComponent<NetworkObject>().NetworkObjectId)).ToArray(), enemyPositions = GameObject.FindObjectsByType<EnemyPathFinding>(FindObjectsSortMode.None).Select(e => new Vector3(e.transform.position.x, e.transform.position.y, e.GetComponent<NetworkObject>().NetworkObjectId)).ToArray(),
@ -608,12 +613,12 @@ public class RopeSimulator : NetworkBehaviour
private void TryMovePointToPosition(Point point, Vector3 position) private void TryMovePointToPosition(Point point, Vector3 position)
{ {
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();
Vector2 initialPos = new Vector2(point.position.x, point.position.y); Vector2 initialPos = new Vector2(point.position.x, point.position.y);
bool shouldBreak = false; 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); 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); point.position.Set(newPos.x, newPos.y, point.position.z);
@ -645,7 +650,7 @@ public class RopeSimulator : NetworkBehaviour
private void HandleStaticCollidersOfPoint(Point p) 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 == null) continue;
if (hitCollider.isTrigger) continue; if (hitCollider.isTrigger) continue;