evershade/Assets/Scripts/Managers/CropManager.cs

57 lines
1.3 KiB
C#

using UnityEngine;
// 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.
public class CropManager : MonoBehaviour
{
public Crop[] crops;
public static CropManager Instance { get; private set; }
private void Awake()
{
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
LoadCrops();
}
else
{
Destroy(gameObject);
}
}
public Crop GetCropById(int id)
{
return crops[id];
}
public Crop GetCropByName(string name)
{
foreach (var crop in crops)
if (crop.cropItem.name == name)
return crop;
return null;
}
// Get a random crop
public Crop GetRandomCrop()
{
return crops[Random.Range(0, crops.Length - 1)];
}
// Get all the crops from the resource folder
public void LoadCrops()
{
Debug.Log("Loading crops from Resources/Crops");
crops = Resources.LoadAll<Crop>("Crops");
foreach (var crop in crops) Debug.Log("Loaded crop: " + crop.cropItem.name);
}
}