Field.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using UnityEngine;
  2. using UnityEngine.InputSystem;
  3. public enum FieldSize
  4. {
  5. Small = 1, // 1x1, fast to work, low yield
  6. Medium = 2, // 2x2, medium time, medium yield
  7. Large = 3 // 3x3, slow to work, high yield
  8. }
  9. public class Field : MonoBehaviour, ISelectable
  10. {
  11. [Header("Field Properties")]
  12. public FieldSize fieldSize = FieldSize.Small;
  13. public int maxFood = 30;
  14. public int currentFood = 0;
  15. public int totalHarvests = 0; // Track how many times it's been harvested
  16. public int maxHarvests = 5; // Field depletes after this many harvests
  17. public float growthTime = 20f; // Base time to fully grow
  18. public bool isFullyGrown = false;
  19. [Header("Construction")]
  20. public bool isUnderConstruction = true;
  21. public float constructionProgress = 0f;
  22. public float constructionTime = 8f; // Time for farmer to build the field
  23. [Header("References")]
  24. public GameObject farmhouse; // The farmhouse that owns this field
  25. public Villager assignedFarmer; // The farmer working this field
  26. private float growthTimer = 0f;
  27. private Renderer fieldRenderer;
  28. void Start()
  29. {
  30. fieldRenderer = GetComponent<Renderer>();
  31. SetupFieldProperties();
  32. UpdateVisualState();
  33. }
  34. void Update()
  35. {
  36. if (isUnderConstruction)
  37. {
  38. // Construction is handled by the farmer - this just updates visuals
  39. UpdateVisualState();
  40. }
  41. else if (!isFullyGrown && currentFood < maxFood)
  42. {
  43. // Grow food over time
  44. growthTimer += Time.deltaTime;
  45. if (growthTimer >= growthTime)
  46. {
  47. GrowFood();
  48. growthTimer = 0f;
  49. }
  50. UpdateVisualState();
  51. }
  52. }
  53. void SetupFieldProperties()
  54. {
  55. // Adjust properties based on field size
  56. int sizeMultiplier = (int)fieldSize;
  57. maxFood = 20 * sizeMultiplier * sizeMultiplier; // Small=20, Medium=80, Large=180
  58. constructionTime = 5f + (sizeMultiplier * 3f); // Small=8s, Medium=11s, Large=14s
  59. growthTime = 15f + (sizeMultiplier * 5f); // Small=20s, Medium=25s, Large=30s
  60. // Scale the visual size
  61. transform.localScale = new Vector3(sizeMultiplier, 0.1f, sizeMultiplier);
  62. Debug.Log($"Field created - Size: {fieldSize}, MaxFood: {maxFood}, ConstructionTime: {constructionTime}s");
  63. }
  64. public void AdvanceConstruction(float deltaTime)
  65. {
  66. if (!isUnderConstruction) return;
  67. constructionProgress += deltaTime;
  68. if (constructionProgress >= constructionTime)
  69. {
  70. CompleteConstruction();
  71. }
  72. UpdateVisualState();
  73. }
  74. public void CompleteConstruction()
  75. {
  76. isUnderConstruction = false;
  77. constructionProgress = constructionTime;
  78. currentFood = maxFood / 4; // Start with some food
  79. Debug.Log($"Field construction completed! Starting with {currentFood}/{maxFood} food");
  80. UpdateVisualState();
  81. }
  82. void GrowFood()
  83. {
  84. int growthAmount = Random.Range(5, 15);
  85. currentFood = Mathf.Min(currentFood + growthAmount, maxFood);
  86. if (currentFood >= maxFood)
  87. {
  88. isFullyGrown = true;
  89. }
  90. Debug.Log($"Field grew {growthAmount} food. Now has {currentFood}/{maxFood}");
  91. }
  92. public int HarvestFood(int requestedAmount)
  93. {
  94. if (isUnderConstruction || currentFood <= 0) return 0;
  95. int harvestedAmount = Mathf.Min(requestedAmount, currentFood);
  96. currentFood -= harvestedAmount;
  97. if (currentFood < maxFood)
  98. {
  99. isFullyGrown = false;
  100. growthTimer = 0f; // Reset growth timer when harvested
  101. totalHarvests++;
  102. }
  103. // Check if field is depleted
  104. if (totalHarvests >= maxHarvests)
  105. {
  106. Debug.Log($"🌾 Field depleted after {totalHarvests} harvests. Field will be removed.");
  107. if (assignedFarmer != null)
  108. {
  109. assignedFarmer.targetWorkplace = null;
  110. }
  111. Destroy(gameObject, 1f); // Destroy after a short delay
  112. return harvestedAmount;
  113. }
  114. Debug.Log($"Harvested {harvestedAmount} food from field. Remaining: {currentFood}/{maxFood}. Uses: {totalHarvests}/{maxHarvests}");
  115. UpdateVisualState();
  116. return harvestedAmount;
  117. }
  118. void UpdateVisualState()
  119. {
  120. if (fieldRenderer == null) return;
  121. if (isUnderConstruction)
  122. {
  123. // Brown/dirt color during construction, with progress indication
  124. float progress = constructionProgress / constructionTime;
  125. Color constructionColor = Color.Lerp(new Color(0.4f, 0.3f, 0.2f), new Color(0.6f, 0.4f, 0.2f), progress);
  126. fieldRenderer.material.color = constructionColor;
  127. }
  128. else
  129. {
  130. // Green shades based on food content
  131. float foodPercent = (float)currentFood / maxFood;
  132. if (foodPercent > 0.8f)
  133. {
  134. fieldRenderer.material.color = new Color(0.2f, 0.8f, 0.2f); // Bright green - ready to harvest
  135. }
  136. else if (foodPercent > 0.4f)
  137. {
  138. fieldRenderer.material.color = new Color(0.4f, 0.7f, 0.3f); // Medium green - growing
  139. }
  140. else
  141. {
  142. fieldRenderer.material.color = new Color(0.6f, 0.5f, 0.3f); // Brownish - recently harvested
  143. }
  144. }
  145. }
  146. public string GetFieldInfo()
  147. {
  148. if (isUnderConstruction)
  149. {
  150. float progressPercent = (constructionProgress / constructionTime) * 100f;
  151. return $"🌾 Field ({fieldSize})\\nUnder Construction: {progressPercent:F0}%\\nBuilder: {(assignedFarmer != null ? assignedFarmer.villagerName : "Needed")}";
  152. }
  153. else
  154. {
  155. float depletionPercent = ((float)totalHarvests / maxHarvests) * 100f;
  156. return $"🌾 Field ({fieldSize})\\nFood: {currentFood}/{maxFood}\\nGrown: {(isFullyGrown ? "Yes" : "Growing...")}\\nUses: {totalHarvests}/{maxHarvests} ({depletionPercent:F0}% depleted)";
  157. }
  158. }
  159. // ISelectable implementation
  160. public void OnSelected()
  161. {
  162. // Visual indication could be added here
  163. }
  164. public void OnDeselected()
  165. {
  166. // Clear visual indication
  167. }
  168. void OnMouseDown()
  169. {
  170. if (Mouse.current != null && Mouse.current.leftButton.wasPressedThisFrame)
  171. {
  172. if (GameManager.Instance?.selectionManager != null)
  173. {
  174. GameManager.Instance.selectionManager.SelectObject(gameObject);
  175. }
  176. }
  177. }
  178. public float GetWorkTime()
  179. {
  180. // Return how long it takes to work this field (affects farmer efficiency)
  181. return 2f + ((int)fieldSize * 1.5f); // Small=3.5s, Medium=5s, Large=6.5s
  182. }
  183. }