Files
Barrack-Unity/Assets/Scripts/Game/Yummies/YummyCake.cs
T

121 lines
3.0 KiB
C#
Raw Normal View History

2026-08-06 14:43:17 -04:00
using UnityEngine;
public class YummyCake : MonoBehaviour
{
2026-08-06 20:00:41 -04:00
public AudioClip spawnSound;
public AudioClip breakSound;
public AudioClip missedSound;
public AudioClip stormSound;
public GameObject lightningPrefab;
public GameObject laserPrefab;
public GameObject magnetPrefab;
public GameObject keyPrefab;
private AudioSource audioSource;
private Animator animator;
2026-08-27 17:21:15 -04:00
private WorldFiller worldFiller;
2026-08-06 20:00:41 -04:00
private static float laserProbability = 0.4f;
private static float magnetProbability = 0.2f;
private static float keyProbability = 0.1f;
private static float manyProbability = 0.2f;
private static float stormProbability = 0.01f;
2026-08-06 14:43:17 -04:00
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
2026-08-06 20:00:41 -04:00
audioSource = GetComponent<AudioSource>();
animator = GetComponent<Animator>();
audioSource.PlayOneShot(spawnSound);
2026-08-27 17:21:15 -04:00
worldFiller = GameObject.FindWithTag("WorldFiller")?.GetComponent<WorldFiller>();
2026-08-06 20:00:41 -04:00
}
public void BreakOrMiss()
{
// TODO: Implement logic to check if the cake is in a clear area or not
2026-08-27 17:21:15 -04:00
bool inClearArea = worldFiller.IsPositionInFreeArea(transform.position);
2026-08-06 20:00:41 -04:00
if (inClearArea)
{
animator.SetTrigger("break");
audioSource.PlayOneShot(breakSound);
Break();
}
else
{
animator.SetTrigger("miss");
audioSource.PlayOneShot(missedSound);
}
}
public void OnAnimationComplete()
{
Destroy(gameObject);
2026-08-06 14:43:17 -04:00
}
2026-08-06 20:00:41 -04:00
private void SpawnItem(float randomValue)
2026-08-06 14:43:17 -04:00
{
2026-08-06 20:00:41 -04:00
if (randomValue < laserProbability)
{
Instantiate(laserPrefab, transform.position, Quaternion.identity);
}
else if (randomValue < magnetProbability)
{
Instantiate(magnetPrefab, transform.position, Quaternion.identity);
}
else if (randomValue < keyProbability)
{
Instantiate(keyPrefab, transform.position, Quaternion.identity);
}
else
{
Instantiate(lightningPrefab, transform.position, Quaternion.identity);
}
}
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.
float randomValue = 0.0f;
while (randomValue < manyProbability)
{
SpawnItem(randomValue);
// Update randomValue to determine if we should spawn another item.
randomValue = Random.value;
}
}
2026-08-06 14:43:17 -04:00
}
}