TO SQUASH: better ball collisions.

This commit is contained in:
2026-08-03 01:47:14 -04:00
parent 378efa16ef
commit 17051821b5
8 changed files with 50 additions and 10 deletions
+35 -3
View File
@@ -22,6 +22,8 @@ public class BallSpawner : MonoBehaviour
private Queue<System.Action> ballQueue = new Queue<System.Action>();
private Queue<GameObject> balls = new Queue<GameObject>();
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
@@ -46,7 +48,9 @@ public class BallSpawner : MonoBehaviour
}
Vector3 spawnPosition = GetRandomPositionInsideGameBoard();
Instantiate(ballPrefab, spawnPosition, Quaternion.identity);
var newBall = Instantiate(ballPrefab, spawnPosition, Quaternion.identity);
balls.Enqueue(newBall);
}
private Vector3 GetRandomPositionInsideGameBoard()
@@ -70,8 +74,9 @@ public class BallSpawner : MonoBehaviour
bounds = boardRenderer.bounds;
}
float randomX = Random.Range(bounds.min.x, bounds.max.x);
float randomY = Random.Range(bounds.min.y, bounds.max.y);
const float edgeInset = 0.05f;
float randomX = Random.Range(bounds.min.x + edgeInset, bounds.max.x - edgeInset);
float randomY = Random.Range(bounds.min.y + edgeInset, bounds.max.y - edgeInset);
return new Vector3(randomX, randomY, gameBoard.transform.position.z);
}
@@ -84,5 +89,32 @@ public class BallSpawner : MonoBehaviour
spawnAction.Invoke();
yield return _waitForSeconds0_5;
}
// Disable collision between existing balls of certain types.
// Only basic balls and fruit balls will collide with each other, while ooze,
// nuclear, glass, and eye balls will not collide with any other balls.
// As an exception, glass balls will collide with other glass balls.
foreach (var ball in balls)
{
foreach (var otherBall in balls)
{
if (ball == otherBall) continue;
bool ballIsBasicOrFruit = ball.CompareTag("BasicBall") || ball.CompareTag("FruitBall");
bool otherIsBasicOrFruit = otherBall.CompareTag("BasicBall") || otherBall.CompareTag("FruitBall");
bool bothAreGlass = ball.CompareTag("GlassBall") && otherBall.CompareTag("GlassBall");
bool shouldCollide = (ballIsBasicOrFruit && otherIsBasicOrFruit) || bothAreGlass;
if (!shouldCollide)
{
Physics2D.IgnoreCollision(
ball.GetComponent<Collider2D>(),
otherBall.GetComponent<Collider2D>(),
true
);
}
}
}
}
}