Added autospawning
This commit is contained in:
@@ -6,9 +6,8 @@ namespace Fruitomation
|
||||
{
|
||||
public class FruitBehaviour : MonoBehaviour
|
||||
{
|
||||
[Header("References")] [SerializeField]
|
||||
private RectTransform RectTransform;
|
||||
|
||||
[Header("References")]
|
||||
[SerializeField] private RectTransform RectTransform;
|
||||
[SerializeField] private Rigidbody2D Body2D;
|
||||
[SerializeField] private Button Button;
|
||||
|
||||
@@ -43,6 +42,7 @@ namespace Fruitomation
|
||||
|
||||
private void TriggerDestruction()
|
||||
{
|
||||
Spawner.RemoveFruit(this);
|
||||
Destroy(gameObject);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using PashaBibko.Pacore.Attributes;
|
||||
using System.Collections.Generic;
|
||||
using JetBrains.Annotations;
|
||||
using UnityEngine;
|
||||
|
||||
@@ -6,22 +7,48 @@ 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[UsedImplicitly, InspectorCallable("Spawn Fruit")]
|
||||
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