Advertisement
evelynshilosky

Plant - Part 39

Apr 18th, 2024
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.63 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class Plant : MonoBehaviour
  7. {
  8.     [SerializeField] GameObject seedModel;
  9.     [SerializeField] GameObject youngPlantModel;
  10.     [SerializeField] GameObject maturePlantModel;
  11.  
  12.     [SerializeField] List<GameObject> plantProduceSpawns;
  13.  
  14.     [SerializeField] GameObject producePrefab;
  15.  
  16.     public int dayOfPlanting; //Day of "Birth"
  17.     [SerializeField] int plantAge = 0; //depends on the watering frequency
  18.  
  19.     [SerializeField] int ageForYoungModel; //3
  20.     [SerializeField] int ageForMatureModel; //10
  21.     [SerializeField] int ageForFirstProduceBatch; //13
  22.  
  23.     [SerializeField] int daysForNewProduce; //Days it takes for new fruit to grow after the initial batch
  24.     [SerializeField] int daysRemainingForNewProduceCounter;
  25.  
  26.     [SerializeField] bool isOneTimeHarvest;
  27.     public bool isWatered; //Only is the plant is watered at the end of the day, it will "age";
  28.  
  29.     private void OnEnable()
  30.     {
  31.         TimeManager.Instance.OnDayPass.AddListener(DayPass);
  32.     }
  33.  
  34.     private void OnDisable()
  35.     {
  36.         TimeManager.Instance.OnDayPass.RemoveListener(DayPass);
  37.     }
  38.  
  39.     private void OnDestroy()
  40.     {
  41.         GetComponentInParent<Soil>().isEmpty = true;
  42.         GetComponentInParent<Soil>().plantName = "";
  43.         GetComponentInParent<Soil>().currentPlant = null;
  44.     }
  45.  
  46.     private void DayPass() // Will be called daily at midnight
  47.     {
  48.         if (isWatered)
  49.         {
  50.             plantAge++;
  51.  
  52.             isWatered = false;
  53.             GetComponentInParent<Soil>().MakeSoilNotWatered();
  54.             //GetComponent<MeshCollider>().enabled = false;
  55.         }
  56.  
  57.         CheckGrowth();
  58.  
  59.         if (!isOneTimeHarvest)
  60.         {
  61.             CheckProduce();
  62.         }
  63.  
  64.  
  65.     }
  66.  
  67.  
  68.     private void CheckGrowth()
  69.     {
  70.         seedModel.SetActive(plantAge < ageForYoungModel);
  71.         youngPlantModel.SetActive(plantAge >= ageForYoungModel && plantAge < ageForMatureModel);
  72.         maturePlantModel.SetActive(plantAge >= ageForMatureModel);
  73.        
  74.         if (plantAge >= ageForMatureModel && isOneTimeHarvest)
  75.         {
  76.             MakePlantPickable();
  77.         }
  78.  
  79.     }
  80.  
  81.     private void MakePlantPickable()
  82.     {
  83.         GetComponent<InteractableObject>().enabled = true;
  84.         //GetComponent<MeshCollider>().enabled = true;
  85.     }
  86.  
  87.     private void CheckProduce()
  88.     {
  89.         if (plantAge == ageForFirstProduceBatch)
  90.         {
  91.             GenerateProduceForEmptySpawns();
  92.         }
  93.  
  94.         if (plantAge > ageForFirstProduceBatch)
  95.         {
  96.             if (daysRemainingForNewProduceCounter == 0)
  97.             {
  98.                 GenerateProduceForEmptySpawns();
  99.  
  100.                 daysRemainingForNewProduceCounter = daysForNewProduce;
  101.             }
  102.             else
  103.             {
  104.                 daysRemainingForNewProduceCounter--;
  105.             }
  106.         }
  107.     }
  108.  
  109.     private void GenerateProduceForEmptySpawns()
  110.     {
  111.         foreach(GameObject spawn in plantProduceSpawns)
  112.         {
  113.             if (spawn.transform.childCount == 0)
  114.             {
  115.                 // Instantiate the produce from the prefab
  116.                 GameObject produce = Instantiate(producePrefab);
  117.  
  118.                 // Set the produce to be a child of the current spawn in the list
  119.                 produce.transform.parent = spawn.transform;
  120.  
  121.                 // Position the produce in the middle of the spawn
  122.                 Vector3 producePosition = Vector3.zero;
  123.                 producePosition.y = 0f;
  124.                 produce.transform.localPosition = producePosition;
  125.             }
  126.         }
  127.     }
  128. }
  129.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement