private void LoadExistingDirt() { // find the dirts data that matches its x and y. data = SectionData.Current.DirtDatas.Find(dirt => X == dirt.x && Y == dirt.y); PlantableData plantData = SectionData.Current.PlantDatas.Find(plant => plant.Id == data.Id); // if there is a matching plant data if (plantData != null) { // we must create a plant game object var prefab = Resources.Load(PLANTABLE_PREFAB_FOLDER + plantData.PrefabName) as GameObject; if (prefab == null) { Debug.LogError("There is no planteable prefab at path: " + PLANTABLE_PREFAB_FOLDER + plantData.PrefabName); } var gameObject = Instantiate(prefab); gameObject.transform.position = transform.position; // assign the plant data plant = gameObject.GetComponent <Plantable>(); plant.Data = plantData; } }
private void Harvest(ToolTypes toolType) { if (plant != null) { plant.OnHarvest(toolType, () => plant = null); INodeData node = nodeGrid.GetNodeFromXY(X, Y); node.Data.IsOccupied = false; } }
public void OnInteract(ToolTypes toolType, GameObject gameObject, Action onSuccessful) { switch (toolType) { case ToolTypes.Hoe: Hoe(); onSuccessful?.Invoke(); break; case ToolTypes.WateringCan: Water(); onSuccessful?.Invoke(); break; case ToolTypes.Sickle: if (plant == null) { return; } Harvest(ToolTypes.Sickle); onSuccessful?.Invoke(); break; case ToolTypes.Axe: if (plant == null) { return; } Harvest(ToolTypes.Axe); onSuccessful?.Invoke(); break; case ToolTypes.Other: // check if the given gameObject contains a Planteable component if (data.Hoed && gameObject != null && gameObject.TryGetComponent <Plantable>(out _)) { // generate the Planteable GameObject var obj = Instantiate(gameObject); obj.transform.position = transform.position; // initialize the Dirts Plant field and give it the same Id as this Dirt instance. plant = obj.GetComponent <Plantable>(); plant.SetDataId(data.Id); onSuccessful?.Invoke(); } break; default: /*Debug.Log($"Do nothing with tooltype {toolType}");*/ break; } }