55 lines
1.6 KiB
C#
55 lines
1.6 KiB
C#
using PashaBibko.Pacore.Attributes;
|
|
using System.Collections.Generic;
|
|
using JetBrains.Annotations;
|
|
using UnityEngine;
|
|
|
|
namespace Fruitomation
|
|
{
|
|
public class FruitSpawner : MonoBehaviour
|
|
{
|
|
[Header("Settings")]
|
|
[SerializeField] private int MaxSpawned;
|
|
[SerializeField] private float MinSpawnTime;
|
|
[SerializeField] private float MaxSpawnTime;
|
|
|
|
[Header("References")]
|
|
[SerializeField] private Transform FruitSpawnParent;
|
|
[SerializeField] private GameObject FruitPrefab;
|
|
[SerializeField] private Canvas GameCanvas;
|
|
|
|
[Header("Read only")]
|
|
[SerializeField, InspectorReadOnly] private List<FruitBehaviour> ActiveFruits;
|
|
|
|
private float TimeUntilNextSpawn;
|
|
|
|
private void Update()
|
|
{
|
|
if (ActiveFruits.Count <= MaxSpawned)
|
|
{
|
|
TimeUntilNextSpawn -= Time.deltaTime;
|
|
|
|
if (TimeUntilNextSpawn <= 0f)
|
|
{
|
|
TimeUntilNextSpawn = Random.Range(MinSpawnTime, MaxSpawnTime);
|
|
SpawnFruit();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void SpawnFruit()
|
|
{
|
|
GameObject go = Instantiate(FruitPrefab, FruitSpawnParent);
|
|
FruitBehaviour behaviour = go.GetComponent<FruitBehaviour>();
|
|
Debug.Assert(behaviour != null, "Could not find FruitBehaviour");
|
|
|
|
ActiveFruits.Add(behaviour);
|
|
behaviour.InitFruitBehaviour
|
|
(
|
|
GameCanvas, this
|
|
);
|
|
}
|
|
|
|
public void RemoveFruit(FruitBehaviour fruit) => ActiveFruits.Remove(fruit);
|
|
}
|
|
}
|