Organised and added fan art
This commit is contained in:
54
Assets/Scripts/Game/FruitSpawner.cs
Normal file
54
Assets/Scripts/Game/FruitSpawner.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using PashaBibko.Pacore.Attributes;
|
||||
using System.Collections.Generic;
|
||||
using Fruitomation.Global;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Fruitomation.Game
|
||||
{
|
||||
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 && GameStateController.Is(GameState.Simulation))
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user