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

77 lines
1.7 KiB
C#
Raw Normal View History

2026-08-06 14:43:17 -04:00
using UnityEngine;
2026-08-25 22:24:07 -04:00
public class CakeSpawner : Spawner
2026-08-06 14:43:17 -04:00
{
2026-08-06 20:00:41 -04:00
public GameObject cakePrefab;
public SpriteRenderer gameBoardSpriteRenderer;
2026-08-27 17:14:18 -04:00
public WorldFiller worldFiller;
2026-08-27 21:51:28 -04:00
public Canvas canvas;
2026-08-06 20:00:41 -04:00
private Bounds cakeBounds;
private Bounds gameBoardBounds;
private float spawnTimer = 0f;
private float spawnInterval = 10f;
private Vector2 spawnPosition;
private bool spawned = false;
private static float spawnProbability = 0.9f;
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
gameBoardBounds = gameBoardSpriteRenderer.bounds;
cakeBounds = cakePrefab.GetComponent<SpriteRenderer>().bounds;
Reset();
2026-08-06 14:43:17 -04:00
}
// Update is called once per frame
2026-08-06 20:00:41 -04:00
void FixedUpdate()
{
2026-08-25 22:24:07 -04:00
if (!isSpawning)
{
return;
}
if (!spawned )
2026-08-06 20:00:41 -04:00
{
spawnTimer += Time.fixedDeltaTime;
if (spawnTimer >= 1f)
{
spawnTimer = 0f;
if (Random.value < spawnProbability)
{
GameObject cake = Instantiate(cakePrefab, spawnPosition, Quaternion.identity);
2026-08-27 21:51:28 -04:00
cake.GetComponent<YummyCake>().canvas = canvas;
2026-08-06 20:00:41 -04:00
}
spawned = true;
}
}
else
{
spawnTimer += Time.fixedDeltaTime;
if (spawnTimer >= spawnInterval)
{
Reset();
}
}
}
public void Reset()
2026-08-06 14:43:17 -04:00
{
2026-08-06 20:00:41 -04:00
spawned = false;
spawnTimer = 0f;
2026-08-27 17:14:18 -04:00
spawnPosition = worldFiller.GetRandomPositionInFreeAreas(cakeBounds);
2026-08-06 14:43:17 -04:00
}
}