Advertisement
evelynshilosky

Plant - Part 38

Apr 18th, 2024
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.92 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.     [SerializeField] 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.  
  40.  
  41.     private void DayPass() // Will be called daily at midnight
  42.     {
  43.         if (isWatered)
  44.         {
  45.             plantAge++;
  46.         }
  47.  
  48.         CheckGrowth();
  49.  
  50.         CheckProduce();
  51.     }
  52.  
  53.  
  54.  
  55.     private void CheckGrowth()
  56.     {
  57.         seedModel.SetActive(plantAge < ageForYoungModel);
  58.         youngPlantModel.SetActive(plantAge >= ageForYoungModel && plantAge < ageForMatureModel);
  59.         maturePlantModel.SetActive(plantAge >= ageForMatureModel);
  60.     }
  61.  
  62.     private void CheckProduce()
  63.     {
  64.         if (plantAge == ageForFirstProduceBatch)
  65.         {
  66.             GenerateProduceForEmptySpawns();
  67.         }
  68.  
  69.         if (plantAge > ageForFirstProduceBatch)
  70.         {
  71.             if (daysRemainingForNewProduceCounter == 0)
  72.             {
  73.                 GenerateProduceForEmptySpawns();
  74.  
  75.                 daysRemainingForNewProduceCounter = daysForNewProduce;
  76.             }
  77.             else
  78.             {
  79.                 daysRemainingForNewProduceCounter--;
  80.             }
  81.         }
  82.     }
  83.  
  84.     private void GenerateProduceForEmptySpawns()
  85.     {
  86.         foreach(GameObject spawn in plantProduceSpawns)
  87.         {
  88.             if (spawn.transform.childCount == 0)
  89.             {
  90.                 // Instantiate the produce from the prefab
  91.                 GameObject produce = Instantiate(producePrefab);
  92.  
  93.                 // Set the produce to be a child of the current spawn in the list
  94.                 produce.transform.parent = spawn.transform;
  95.  
  96.                 // Position the produce in the middle of the spawn
  97.                 Vector3 producePosition = Vector3.zero;
  98.                 producePosition.y = 0f;
  99.                 produce.transform.localPosition = producePosition;
  100.             }
  101.         }
  102.     }
  103. }
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement