Field.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using UnityEngine;
  2. public enum FieldSize
  3. {
  4. Small = 1, // 1x1, fast to work, low yield
  5. Medium = 2, // 2x2, medium time, medium yield
  6. Large = 3 // 3x3, slow to work, high yield
  7. }
  8. public class Field : MonoBehaviour
  9. {
  10. [Header("Field Properties")]
  11. public FieldSize fieldSize = FieldSize.Small;
  12. public int maxFood = 30;
  13. public int currentFood = 0;
  14. public float growthTime = 20f; // Base time to fully grow
  15. public bool isFullyGrown = false;
  16. [Header("Construction")]
  17. public bool isUnderConstruction = true;
  18. public float constructionProgress = 0f;
  19. public float constructionTime = 8f; // Time for farmer to build the field
  20. [Header("References")]
  21. public GameObject farmhouse; // The farmhouse that owns this field
  22. public Villager assignedFarmer; // The farmer working this field
  23. private float growthTimer = 0f;
  24. private Renderer fieldRenderer;
  25. void Start()
  26. {
  27. fieldRenderer = GetComponent<Renderer>();
  28. SetupFieldProperties();
  29. UpdateVisualState();
  30. }
  31. void Update()
  32. {
  33. if (isUnderConstruction)
  34. {
  35. // Construction is handled by the farmer - this just updates visuals
  36. UpdateVisualState();
  37. }
  38. else if (!isFullyGrown && currentFood < maxFood)
  39. {
  40. // Grow food over time
  41. growthTimer += Time.deltaTime;
  42. if (growthTimer >= growthTime)
  43. {
  44. GrowFood();
  45. growthTimer = 0f;
  46. }
  47. UpdateVisualState();
  48. }
  49. }
  50. void SetupFieldProperties()
  51. {
  52. // Adjust properties based on field size
  53. int sizeMultiplier = (int)fieldSize;
  54. maxFood = 20 * sizeMultiplier * sizeMultiplier; // Small=20, Medium=80, Large=180
  55. constructionTime = 5f + (sizeMultiplier * 3f); // Small=8s, Medium=11s, Large=14s
  56. growthTime = 15f + (sizeMultiplier * 5f); // Small=20s, Medium=25s, Large=30s
  57. // Scale the visual size
  58. transform.localScale = new Vector3(sizeMultiplier, 0.1f, sizeMultiplier);
  59. Debug.Log($"Field created - Size: {fieldSize}, MaxFood: {maxFood}, ConstructionTime: {constructionTime}s");
  60. }
  61. public void AdvanceConstruction(float deltaTime)
  62. {
  63. if (!isUnderConstruction) return;
  64. constructionProgress += deltaTime;
  65. if (constructionProgress >= constructionTime)
  66. {
  67. CompleteConstruction();
  68. }
  69. UpdateVisualState();
  70. }
  71. public void CompleteConstruction()
  72. {
  73. isUnderConstruction = false;
  74. constructionProgress = constructionTime;
  75. currentFood = maxFood / 4; // Start with some food
  76. Debug.Log($"Field construction completed! Starting with {currentFood}/{maxFood} food");
  77. UpdateVisualState();
  78. }
  79. void GrowFood()
  80. {
  81. int growthAmount = Random.Range(5, 15);
  82. currentFood = Mathf.Min(currentFood + growthAmount, maxFood);
  83. if (currentFood >= maxFood)
  84. {
  85. isFullyGrown = true;
  86. }
  87. Debug.Log($"Field grew {growthAmount} food. Now has {currentFood}/{maxFood}");
  88. }
  89. public int HarvestFood(int requestedAmount)
  90. {
  91. if (isUnderConstruction || currentFood <= 0) return 0;
  92. int harvestedAmount = Mathf.Min(requestedAmount, currentFood);
  93. currentFood -= harvestedAmount;
  94. if (currentFood < maxFood)
  95. {
  96. isFullyGrown = false;
  97. growthTimer = 0f; // Reset growth timer when harvested
  98. }
  99. Debug.Log($"Harvested {harvestedAmount} food from field. Remaining: {currentFood}/{maxFood}");
  100. UpdateVisualState();
  101. return harvestedAmount;
  102. }
  103. void UpdateVisualState()
  104. {
  105. if (fieldRenderer == null) return;
  106. if (isUnderConstruction)
  107. {
  108. // Brown/dirt color during construction, with progress indication
  109. float progress = constructionProgress / constructionTime;
  110. Color constructionColor = Color.Lerp(new Color(0.4f, 0.3f, 0.2f), new Color(0.6f, 0.4f, 0.2f), progress);
  111. fieldRenderer.material.color = constructionColor;
  112. }
  113. else
  114. {
  115. // Green shades based on food content
  116. float foodPercent = (float)currentFood / maxFood;
  117. if (foodPercent > 0.8f)
  118. {
  119. fieldRenderer.material.color = new Color(0.2f, 0.8f, 0.2f); // Bright green - ready to harvest
  120. }
  121. else if (foodPercent > 0.4f)
  122. {
  123. fieldRenderer.material.color = new Color(0.4f, 0.7f, 0.3f); // Medium green - growing
  124. }
  125. else
  126. {
  127. fieldRenderer.material.color = new Color(0.6f, 0.5f, 0.3f); // Brownish - recently harvested
  128. }
  129. }
  130. }
  131. public string GetFieldInfo()
  132. {
  133. if (isUnderConstruction)
  134. {
  135. float progressPercent = (constructionProgress / constructionTime) * 100f;
  136. return $"Field ({fieldSize})\nUnder Construction: {progressPercent:F0}%";
  137. }
  138. else
  139. {
  140. return $"Field ({fieldSize})\nFood: {currentFood}/{maxFood}\nGrowth: {isFullyGrown}";
  141. }
  142. }
  143. public float GetWorkTime()
  144. {
  145. // Return how long it takes to work this field (affects farmer efficiency)
  146. return 2f + ((int)fieldSize * 1.5f); // Small=3.5s, Medium=5s, Large=6.5s
  147. }
  148. }