maus234

Wheat

Apr 14th, 2024
1,654
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.71 KB | Gaming | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEditor.Timeline.Actions;
  4. using UnityEngine;
  5. using WorldTimeSystem;
  6.  
  7. namespace WorldTimeSystem
  8. {
  9.  
  10.     public class Wheat : MonoBehaviour
  11.     {
  12.         [SerializeField]
  13.         private List<GameObject> growthStagePrefabs; // Prefabs for each growth stage
  14.         [SerializeField]
  15.         private GameObject deterioratedPrefab; // Prefab for the deteriorated crop
  16.         [SerializeField]
  17.         private GameObject prefabCoin; // Prefab to drop when harvested
  18.         [SerializeField]
  19.         private int numberOfCoins = 5; // Number of coins to spawn
  20.         [SerializeField]
  21.         private float spawnRadius = 1.0f; // Radius within which coins will spawn
  22.  
  23.         private int currentStage = -1; // Start at -1 to indicate not growing
  24.         private const int GROWTH_DAYS = 3; // Total days for full growth
  25.         private const int NUM_STAGES = 7; // Number of growth stages
  26.         private GameObject currentCropInstance; // The current instance of the crop
  27.         private TimeSpan lastGrowthTime; // Last time the crop grew
  28.         private bool isFarmerInteracted = false; // Flag to check if farmer has interacted
  29.         private TimeSpan timeWhenFullyGrown; // Time when the crop reached full growth
  30.  
  31.         private void Start()
  32.         {
  33.             WorldTime.Instance.WorldTimeChanged += OnWorldTimeChanged;
  34.             lastGrowthTime = WorldTime.Instance.GetCurrentTime();
  35.         }
  36.  
  37.         private void OnDestroy()
  38.         {
  39.             WorldTime.Instance.WorldTimeChanged -= OnWorldTimeChanged;
  40.         }
  41.  
  42.         private void OnWorldTimeChanged(object sender, TimeSpan currentTime)
  43.         {
  44.             if (!isFarmerInteracted) return;
  45.  
  46.             // Calculate the interval in real-time seconds for each growth stage
  47.             float totalGrowthTimeInSeconds = GROWTH_DAYS * WorldTime.Instance._dayLength;
  48.             float growthIntervalInSeconds = totalGrowthTimeInSeconds / NUM_STAGES;
  49.  
  50.             float secondsSinceLastGrowth = (float)(currentTime - lastGrowthTime).TotalMinutes;
  51.  
  52.             if (currentStage < NUM_STAGES - 1 && secondsSinceLastGrowth >= growthIntervalInSeconds)
  53.             {
  54.                 lastGrowthTime = currentTime;
  55.                 GrowCrop();
  56.             }
  57.         }
  58.  
  59.         private void OnTriggerEnter2D(Collider2D other)
  60.         {
  61.             if (other.CompareTag("Farmer") && !isFarmerInteracted)
  62.             {
  63.                 isFarmerInteracted = true;
  64.                 Invoke("InitialGrow", 7f); // Start growing after a 7-second delay
  65.             }
  66.         }
  67.  
  68.         private void OnTriggerStay2D(Collider2D other)
  69.         {
  70.             if (other.CompareTag("Farmer") && currentStage == NUM_STAGES - 1)
  71.             {
  72.                 Harvest();
  73.             }
  74.         }
  75.  
  76.         private void InitialGrow()
  77.         {
  78.             lastGrowthTime = WorldTime.Instance.GetCurrentTime();
  79.             GrowCrop();
  80.         }
  81.  
  82.         private void GrowCrop()
  83.         {
  84.             if (currentStage < NUM_STAGES - 1)
  85.             {
  86.                 currentStage++;
  87.                 if (currentCropInstance != null)
  88.                 {
  89.                     Destroy(currentCropInstance);
  90.                 }
  91.                 currentCropInstance = Instantiate(growthStagePrefabs[currentStage], transform.position, Quaternion.identity, transform);
  92.  
  93.                 if (currentStage == NUM_STAGES - 1)
  94.                 {
  95.                     timeWhenFullyGrown = WorldTime.Instance.GetCurrentTime();
  96.                 }
  97.             }
  98.         }
  99.  
  100.         private void DeteriorateCrop()
  101.         {
  102.             currentStage = NUM_STAGES;
  103.             if (currentCropInstance != null)
  104.             {
  105.                 Destroy(currentCropInstance);
  106.             }
  107.             currentCropInstance = Instantiate(deterioratedPrefab, transform.position, Quaternion.identity, transform);
  108.         }
  109.  
  110.         private void Harvest()
  111.         {
  112.             if (currentCropInstance != null)
  113.             {
  114.                 Destroy(currentCropInstance);
  115.             }
  116.  
  117.             // Calculate the top edge of the collider
  118.             var collider = GetComponent<Collider2D>();
  119.             float topEdge = collider.bounds.max.y;
  120.  
  121.             // Spawn coins along the top edge of the collider
  122.             for (int i = 0; i < numberOfCoins; i++)
  123.             {
  124.                 float spawnX = UnityEngine.Random.Range(collider.bounds.min.x, collider.bounds.max.x);
  125.                 Vector3 spawnPosition = new Vector3(spawnX, topEdge, 0);
  126.                 Instantiate(prefabCoin, spawnPosition, Quaternion.identity);
  127.             }
  128.  
  129.             Destroy(gameObject); // Consider not destroying if you want the crop to regrow
  130.         }
  131.     }
  132. }
  133.  
Advertisement
Add Comment
Please, Sign In to add comment