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

130 lines
3.2 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;
2026-08-27 21:51:28 -04:00
public Canvas canvas;
2026-08-06 20:00:41 -04:00
private AudioSource audioSource;
private Animator animator;
2026-08-27 17:21:15 -04:00
private WorldFiller worldFiller;
2026-08-27 21:51:28 -04:00
private static float laserProbability = 0.25f;
2026-08-06 20:00:41 -04:00
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-27 21:51:28 -04:00
float laserThreshold = laserProbability;
float magnetThreshold = laserThreshold + magnetProbability;
float keyThreshold = magnetThreshold + keyProbability;
GameObject item = null;
if (randomValue < laserThreshold)
2026-08-06 20:00:41 -04:00
{
2026-08-27 21:51:28 -04:00
item = Instantiate(laserPrefab, transform.position, Quaternion.identity);
2026-08-06 20:00:41 -04:00
}
2026-08-27 21:51:28 -04:00
else if (randomValue < magnetThreshold)
2026-08-06 20:00:41 -04:00
{
2026-08-27 21:51:28 -04:00
item = Instantiate(magnetPrefab, transform.position, Quaternion.identity);
2026-08-06 20:00:41 -04:00
}
2026-08-27 21:51:28 -04:00
else if (randomValue < keyThreshold)
2026-08-06 20:00:41 -04:00
{
2026-08-27 21:51:28 -04:00
item = Instantiate(keyPrefab, transform.position, Quaternion.identity);
2026-08-06 20:00:41 -04:00
}
else
{
2026-08-27 21:51:28 -04:00
item = Instantiate(lightningPrefab, transform.position, Quaternion.identity);
}
if (item != null)
{
item.GetComponent<YummyBase>().canvas = canvas;
2026-08-06 20:00:41 -04:00
}
}
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.
2026-08-27 21:51:28 -04:00
SpawnItem(Random.value);
2026-08-06 20:00:41 -04:00
2026-08-27 21:51:28 -04:00
while (Random.value < manyProbability)
2026-08-06 20:00:41 -04:00
{
2026-08-27 21:51:28 -04:00
SpawnItem(Random.value);
2026-08-06 20:00:41 -04:00
}
}
2026-08-06 14:43:17 -04:00
}
}