77 lines
1.7 KiB
C#
77 lines
1.7 KiB
C#
using UnityEngine;
|
|
|
|
public class CakeSpawner : Spawner
|
|
{
|
|
public GameObject cakePrefab;
|
|
|
|
public SpriteRenderer gameBoardSpriteRenderer;
|
|
|
|
public WorldFiller worldFiller;
|
|
|
|
public Canvas canvas;
|
|
|
|
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;
|
|
|
|
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
|
void Start()
|
|
{
|
|
gameBoardBounds = gameBoardSpriteRenderer.bounds;
|
|
cakeBounds = cakePrefab.GetComponent<SpriteRenderer>().bounds;
|
|
Reset();
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void FixedUpdate()
|
|
{
|
|
if (!isSpawning)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (!spawned )
|
|
{
|
|
spawnTimer += Time.fixedDeltaTime;
|
|
|
|
if (spawnTimer >= 1f)
|
|
{
|
|
spawnTimer = 0f;
|
|
if (Random.value < spawnProbability)
|
|
{
|
|
GameObject cake = Instantiate(cakePrefab, spawnPosition, Quaternion.identity);
|
|
cake.GetComponent<YummyCake>().canvas = canvas;
|
|
}
|
|
|
|
spawned = true;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
spawnTimer += Time.fixedDeltaTime;
|
|
|
|
if (spawnTimer >= spawnInterval)
|
|
{
|
|
Reset();
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Reset()
|
|
{
|
|
spawned = false;
|
|
spawnTimer = 0f;
|
|
spawnPosition = worldFiller.GetRandomPositionInFreeAreas(cakeBounds);
|
|
}
|
|
}
|