evershade/Assets/Scripts/Managers/CropManager.cs

62 lines
1.6 KiB
C#

using Data.ScriptableObjects.World;
using UnityEngine;
using Utils;
// Crop Manager should load all of the crops similar to the item manager.
// We should be able to get a crop by its ID and also get a random crop.
// We should also be able to get a crop by its name.
namespace Managers
{
public class CropManager : MonoBehaviour
{
public CropInteractable[] crops;
public static CropManager Instance { get; private set; }
private void Awake()
{
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
LoadCrops();
}
else
{
Destroy(gameObject);
}
}
public CropInteractable GetCropById(int id)
{
return Instantiate(crops[id]);
}
public CropInteractable GetCropByName(string newName)
{
foreach (var crop in crops)
if (crop.CropItem.ItemName == newName)
return Instantiate(crop);
return null;
}
// Get a random crop
public CropInteractable GetRandomCrop()
{
return Instantiate(crops[Random.Range(0, crops.Length - 1)]);
}
// Get all the crops from the resource folder
public void LoadCrops()
{
GameLogger.Log("Loading crops from Resources/Crops");
crops = Resources.LoadAll<CropInteractable>("Crops");
foreach (var crop in crops) GameLogger.Log("Loaded crop: " + crop.CropItem.ItemName);
}
}
}