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

117 lines
2.8 KiB
C#

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;
private AudioSource audioSource;
private Animator animator;
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;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
audioSource = GetComponent<AudioSource>();
animator = GetComponent<Animator>();
audioSource.PlayOneShot(spawnSound);
}
public void BreakOrMiss()
{
// TODO: Implement logic to check if the cake is in a clear area or not
bool inClearArea = true;
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)
{
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;
}
}
}
}