TO SQUASH: added basic balls.

This commit is contained in:
2026-07-26 00:59:17 -04:00
parent 433bbd88e0
commit 787d5394c7
25 changed files with 2186 additions and 403 deletions
+86
View File
@@ -0,0 +1,86 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class BallSpawner : MonoBehaviour
{
public GameObject basicBallPrefab;
public GameObject nuclearBallPrefab;
public GameObject glassBallPrefab;
public GameObject eyeBallPrefab;
public GameObject oozeBallPrefab;
public GameObject fruitBallPrefab;
public GameObject gameBoard;
private static WaitForSeconds _waitForSeconds0_5 = new WaitForSeconds(0.5f);
private Queue<System.Action> ballQueue = new Queue<System.Action>();
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
int currentLevel = PlayerPrefs.GetInt("CurrentLevel");
ballQueue.Enqueue(() => SpawnBasicBall());
ballQueue.Enqueue(() => SpawnBasicBall());
ballQueue.Enqueue(() => SpawnBasicBall());
ballQueue.Enqueue(() => SpawnBasicBall());
StartCoroutine(SpawnBalls());
}
private void SpawnBasicBall()
{
if (basicBallPrefab == null || gameBoard == null)
{
Debug.LogWarning("BallSpawner: Missing basicBallPrefab or gameBoard reference.");
return;
}
Vector3 spawnPosition = GetRandomPositionInsideGameBoard();
Instantiate(basicBallPrefab, spawnPosition, Quaternion.identity);
}
private Vector3 GetRandomPositionInsideGameBoard()
{
Bounds bounds;
Collider2D collider2D = gameBoard.GetComponent<Collider2D>();
if (collider2D != null)
{
bounds = collider2D.bounds;
}
else
{
Renderer boardRenderer = gameBoard.GetComponent<Renderer>();
if (boardRenderer == null)
{
Debug.LogWarning("BallSpawner: gameBoard needs a Collider2D or Renderer to read bounds.");
return gameBoard.transform.position;
}
bounds = boardRenderer.bounds;
}
float randomX = Random.Range(bounds.min.x, bounds.max.x);
float randomY = Random.Range(bounds.min.y, bounds.max.y);
return new Vector3(randomX, randomY, gameBoard.transform.position.z);
}
private IEnumerator SpawnBalls()
{
while (ballQueue.Count > 0)
{
System.Action spawnAction = ballQueue.Dequeue();
spawnAction.Invoke();
yield return _waitForSeconds0_5;
}
}
}
+2
View File
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: b45a86a9500699daf8af02a61c756b2b
+8
View File
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 1b9d11fbe5aebe016b8940bc88822ac2
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
+45
View File
@@ -0,0 +1,45 @@
using UnityEngine;
public class BasicBall : MonoBehaviour
{
private Animator animator;
public AudioClip spawnSound;
public AudioClip bounceSound;
public AudioClip hitSound;
private AudioSource audioSource;
private Vector2 speed;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
audioSource = GetComponent<AudioSource>();
animator = GetComponent<Animator>();
speed = new Vector2(Random.Range(-0.05f, 0.05f), Random.Range(-0.05f, 0.05f));
audioSource.PlayOneShot(spawnSound);
}
// Update is called once per frame
void FixedUpdate()
{
AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(0);
if (stateInfo.IsName("BasicBallMove")) {
transform.Translate(speed);
}
}
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Wall") || collision.gameObject.CompareTag("Ball"))
{
ContactPoint2D contact = collision.GetContact(0);
speed = Vector2.Reflect(speed, contact.normal);
}
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 5788fcee29b49660abecc58bc2095706
+1
View File
@@ -1,6 +1,7 @@
using UnityEngine;
using System.Collections.Generic;
// Idea from https://discussions.unity.com/t/play-audio-in-queue/239402/2
public class MusicManager : MonoBehaviour
{
public AudioClip intro;