TO SQUASH: Fucking magnets, how do they work?

This commit is contained in:
2026-08-05 05:36:38 -04:00
parent a9db378127
commit 46198d6c44
10 changed files with 472 additions and 32 deletions
+49
View File
@@ -2,13 +2,32 @@ using UnityEngine;
public class BasicBall : BallBase
{
private Bounds boardBounds;
private Collider2D selfCollider;
private bool hasBoardBounds = false;
// Faster pull keeps the ball on its current board half while magnet is active.
private float magnetBounceDuration = 0.35f;
// Prevents magnet bounces from reaching the board center line.
private float magnetCenterLeeway = 0.12f;
private MagnetBounceAxisState xMagnetBounceState;
private MagnetBounceAxisState yMagnetBounceState;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
audioSource = GetComponent<AudioSource>();
animator = GetComponent<Animator>();
selfCollider = GetComponent<Collider2D>();
speed = new Vector2(Random.Range(-0.05f, 0.05f), Random.Range(-0.05f, 0.05f));
hasBoardBounds = TryCacheGameBoardBounds(out boardBounds);
audioSource.PlayOneShot(spawnSound);
}
@@ -19,6 +38,35 @@ public class BasicBall : BallBase
if (stateInfo.IsName("BasicBallMove")) {
transform.Translate(speed);
if (affectedByMagnet && hasBoardBounds)
{
Vector3 currentPosition = transform.position;
currentPosition.x = ApplyMagnetBounceAxis(
currentPosition.x,
boardBounds.min.x,
boardBounds.max.x,
magnetActiveLeftRight,
ref xMagnetBounceState,
selfCollider != null ? selfCollider.bounds.extents.x : 0.0f,
magnetBounceDuration,
magnetCenterLeeway
);
currentPosition.y = ApplyMagnetBounceAxis(
currentPosition.y,
boardBounds.min.y,
boardBounds.max.y,
magnetActiveTopBottom,
ref yMagnetBounceState,
selfCollider != null ? selfCollider.bounds.extents.y : 0.0f,
magnetBounceDuration,
magnetCenterLeeway
);
transform.position = currentPosition;
}
}
}
@@ -32,4 +80,5 @@ public class BasicBall : BallBase
speed = Vector2.Reflect(speed, contact.normal);
}
}
}