using UnityEngine; public class YummyCake : MonoBehaviour { public AudioClip spawnSound; public AudioClip breakSound; public AudioClip missedSound; public AudioClip stormSound; public GameObject lightningPrefab; public GameObject laserPrefab; public GameObject magnetPrefab; public GameObject keyPrefab; public Canvas canvas; private AudioSource audioSource; private Animator animator; private WorldFiller worldFiller; private static float laserProbability = 0.25f; private static float magnetProbability = 0.2f; private static float keyProbability = 0.1f; private static float manyProbability = 0.2f; private static float stormProbability = 0.01f; // Start is called once before the first execution of Update after the MonoBehaviour is created void Start() { audioSource = GetComponent(); animator = GetComponent(); audioSource.PlayOneShot(spawnSound); worldFiller = GameObject.FindWithTag("WorldFiller")?.GetComponent(); } public void BreakOrMiss() { // TODO: Implement logic to check if the cake is in a clear area or not bool inClearArea = worldFiller.IsPositionInFreeRegion(transform.position); if (inClearArea) { animator.SetTrigger("break"); audioSource.PlayOneShot(breakSound); Break(); } else { animator.SetTrigger("miss"); audioSource.PlayOneShot(missedSound); } } public void OnAnimationComplete() { Destroy(gameObject); } private void SpawnItem(float randomValue) { float laserThreshold = laserProbability; float magnetThreshold = laserThreshold + magnetProbability; float keyThreshold = magnetThreshold + keyProbability; GameObject item = null; if (randomValue < laserThreshold) { item = Instantiate(laserPrefab, transform.position, Quaternion.identity); } else if (randomValue < magnetThreshold) { item = Instantiate(magnetPrefab, transform.position, Quaternion.identity); } else if (randomValue < keyThreshold) { item = Instantiate(keyPrefab, transform.position, Quaternion.identity); } else { item = Instantiate(lightningPrefab, transform.position, Quaternion.identity); } if (item != null) { item.GetComponent().canvas = canvas; } } private void Break() { float isStorm = Random.value; if (isStorm < stormProbability) { // Trigger a storm event, ie. spawn multiple items at once. audioSource.PlayOneShot(stormSound); for (int i = 0; i < Random.Range(8, 12); i++) { float randomValue = Random.value; SpawnItem(randomValue); } } else { // The first yummy is guaranteed. SpawnItem(Random.value); while (Random.value < manyProbability) { SpawnItem(Random.value); } } } }