85 lines
3.1 KiB
C#
85 lines
3.1 KiB
C#
using System.Collections.Generic;
|
|
using Fruitomation.Game.Items;
|
|
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 Canvas GameCanvas;
|
|
|
|
[Header("Prefabs")]
|
|
[SerializeField] private GameObject BaseItemPrefab;
|
|
[SerializeField] private GameObject ApplePrefab;
|
|
[SerializeField] private GameObject GrapePrefab;
|
|
[SerializeField] private GameObject BananaPrefab;
|
|
[SerializeField] private GameObject MangoPrefab;
|
|
[SerializeField] private GameObject DurianPrefab;
|
|
[SerializeField] private GameObject PitayaPrefab;
|
|
[SerializeField] private GameObject KiwiPrefab;
|
|
[SerializeField] private GameObject BuddhasHandPrefab;
|
|
|
|
private float TimeUntilNextSpawn;
|
|
|
|
private int CurrentItemCount => FruitSpawnParent.childCount;
|
|
|
|
private void Update()
|
|
{
|
|
if (CurrentItemCount <= MaxSpawned && GameStateController.Is(GameState.Simulation))
|
|
{
|
|
TimeUntilNextSpawn -= Time.deltaTime;
|
|
|
|
if (TimeUntilNextSpawn <= 0f)
|
|
{
|
|
TimeUntilNextSpawn = Random.Range(MinSpawnTime, MaxSpawnTime);
|
|
SpawnFruit();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void SpawnFruit()
|
|
{
|
|
List<(ItemType, GameObject)> unlocked = new() { (ItemType.Apple, ApplePrefab) };
|
|
|
|
if (UpgradeManager.Is(BasicUpgrade.Grapes))
|
|
unlocked.Add((ItemType.Grape, GrapePrefab));
|
|
|
|
if (UpgradeManager.Is(BasicUpgrade.Bananas))
|
|
unlocked.Add((ItemType.Banana, BananaPrefab));
|
|
|
|
if (UpgradeManager.Is(BasicUpgrade.Kiwi))
|
|
unlocked.Add((ItemType.Kiwi, KiwiPrefab));
|
|
|
|
if (UpgradeManager.Is(BasicUpgrade.Mangoes))
|
|
unlocked.Add((ItemType.Mango, MangoPrefab));
|
|
|
|
if (UpgradeManager.Is(BasicUpgrade.Durian))
|
|
unlocked.Add((ItemType.Durian, DurianPrefab));
|
|
|
|
if (UpgradeManager.Is(BasicUpgrade.BuddhasHand))
|
|
unlocked.Add((ItemType.BuddhasHand, BuddhasHandPrefab));
|
|
|
|
if (UpgradeManager.Is(BasicUpgrade.Pitayas))
|
|
unlocked.Add((ItemType.Pitaya, PitayaPrefab));
|
|
|
|
GameObject parent = Instantiate(BaseItemPrefab, FruitSpawnParent);
|
|
|
|
(ItemType type, GameObject prefab) = unlocked[Random.Range(0, unlocked.Count)];
|
|
GameObject go = Instantiate(prefab, parent.transform);
|
|
|
|
ItemBehaviour behaviour = parent.GetComponent<ItemBehaviour>();
|
|
Debug.Assert(behaviour is not null, "Could not find ItemBehaviour");
|
|
|
|
behaviour.InitBehaviour(GameCanvas, type);
|
|
}
|
|
}
|
|
}
|