102 lines
3.4 KiB
C#
102 lines
3.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Fruitomation.Game.Items;
|
|
using Fruitomation.Global;
|
|
using UnityEngine;
|
|
using Random = UnityEngine.Random;
|
|
|
|
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 static FruitSpawner Instance;
|
|
|
|
private float TimeUntilNextSpawn;
|
|
|
|
private int CurrentItemCount => FruitSpawnParent.childCount;
|
|
|
|
private void Awake()
|
|
=> Instance = this;
|
|
|
|
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> unlocked = new() { ItemType.Apple };
|
|
|
|
if (UpgradeManager.Is(BasicUpgrade.Grapes))
|
|
unlocked.Add(ItemType.Grape);
|
|
|
|
if (UpgradeManager.Is(BasicUpgrade.Bananas))
|
|
unlocked.Add(ItemType.Banana);
|
|
|
|
if (UpgradeManager.Is(BasicUpgrade.Kiwi))
|
|
unlocked.Add(ItemType.Kiwi);
|
|
|
|
if (UpgradeManager.Is(BasicUpgrade.Mangoes))
|
|
unlocked.Add(ItemType.Mango);
|
|
|
|
if (UpgradeManager.Is(BasicUpgrade.Durian))
|
|
unlocked.Add(ItemType.Durian);
|
|
|
|
if (UpgradeManager.Is(BasicUpgrade.BuddhasHand))
|
|
unlocked.Add(ItemType.BuddhasHand);
|
|
|
|
if (UpgradeManager.Is(BasicUpgrade.Pitayas))
|
|
unlocked.Add(ItemType.Pitaya);
|
|
|
|
SpawnItem(unlocked[Random.Range(0, unlocked.Count)]);
|
|
}
|
|
|
|
public static ItemBehaviour SpawnItem(ItemType item, Vector3? pos = null)
|
|
=> Instance.SpawnItemInternal(item, pos);
|
|
|
|
private ItemBehaviour SpawnItemInternal(ItemType item, Vector3? pos = null)
|
|
{
|
|
GameObject parent = Instantiate(BaseItemPrefab, FruitSpawnParent);
|
|
ItemBehaviour behaviour = parent.GetComponent<ItemBehaviour>();
|
|
Debug.Assert(behaviour is not null, "Could not find ItemBehaviour");
|
|
|
|
if (pos != null)
|
|
{
|
|
parent.transform.position = (Vector3)pos;
|
|
}
|
|
|
|
behaviour.InitBehaviour(GameCanvas, item);
|
|
return behaviour;
|
|
}
|
|
}
|
|
}
|