ResourceNode.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using UnityEngine;
  2. using UnityEngine.InputSystem;
  3. [System.Serializable]
  4. public enum ResourceNodeType
  5. {
  6. Tree,
  7. Stone,
  8. Farm
  9. }
  10. public class ResourceNode : MonoBehaviour, ISelectable
  11. {
  12. [Header("Resource Node Info")]
  13. public ResourceNodeType nodeType;
  14. public ResourceType resourceType;
  15. public int maxResources = 100;
  16. public int currentResources = 100;
  17. public float regenerationRate = 1f; // Resources per second when not being harvested
  18. [Header("Harvesting")]
  19. public bool isBeingHarvested = false;
  20. public Villager currentHarvester;
  21. public float harvestTime = 3f;
  22. [Header("Visual")]
  23. public Renderer nodeRenderer;
  24. public GameObject selectionIndicator;
  25. public GameObject depletedVisual;
  26. private bool isSelected = false;
  27. private float lastRegenerationTime;
  28. public bool IsAvailable => currentResources > 0;
  29. public bool IsOccupied => isBeingHarvested && currentHarvester != null;
  30. public float ResourcePercentage => (float)currentResources / maxResources;
  31. void Start()
  32. {
  33. lastRegenerationTime = Time.time;
  34. if (selectionIndicator != null)
  35. selectionIndicator.SetActive(false);
  36. if (depletedVisual != null)
  37. depletedVisual.SetActive(false);
  38. SetAppropriateTag();
  39. UpdateVisualState();
  40. }
  41. void SetAppropriateTag()
  42. {
  43. gameObject.tag = nodeType switch
  44. {
  45. ResourceNodeType.Tree => "Tree",
  46. ResourceNodeType.Stone => "Stone",
  47. ResourceNodeType.Farm => "Farm",
  48. _ => "Untagged"
  49. };
  50. }
  51. void Update()
  52. {
  53. // Only farms regenerate - trees and stones are finite
  54. if (!isBeingHarvested && currentResources < maxResources && nodeType == ResourceNodeType.Farm)
  55. {
  56. float timeSinceLastRegen = Time.time - lastRegenerationTime;
  57. if (timeSinceLastRegen >= 1f) // Regenerate every second
  58. {
  59. int regenAmount = Mathf.FloorToInt(regenerationRate);
  60. AddResources(regenAmount);
  61. lastRegenerationTime = Time.time;
  62. }
  63. }
  64. // Destroy depleted resource nodes (except farms)
  65. if (currentResources <= 0 && nodeType != ResourceNodeType.Farm)
  66. {
  67. if (currentHarvester != null)
  68. {
  69. currentHarvester.targetWorkplace = null; // Clear the workplace reference
  70. }
  71. Debug.Log($"{nodeType} depleted and removed");
  72. Destroy(gameObject);
  73. }
  74. }
  75. public bool StartHarvesting(Villager harvester)
  76. {
  77. if (IsOccupied || !IsAvailable)
  78. return false;
  79. isBeingHarvested = true;
  80. currentHarvester = harvester;
  81. Debug.Log($"{harvester.villagerName} started harvesting {nodeType}");
  82. return true;
  83. }
  84. public void StopHarvesting()
  85. {
  86. isBeingHarvested = false;
  87. currentHarvester = null;
  88. Debug.Log($"Stopped harvesting {nodeType}");
  89. }
  90. public int HarvestResources(int requestedAmount, float harvesterEfficiency)
  91. {
  92. if (!IsAvailable || !isBeingHarvested)
  93. return 0;
  94. // Calculate actual harvest amount based on efficiency and available resources
  95. int baseAmount = Mathf.Min(requestedAmount, currentResources);
  96. int harvestedAmount = Mathf.RoundToInt(baseAmount * harvesterEfficiency);
  97. currentResources -= harvestedAmount;
  98. currentResources = Mathf.Max(0, currentResources);
  99. UpdateVisualState();
  100. Debug.Log($"Harvested {harvestedAmount} {resourceType} from {nodeType}. Remaining: {currentResources}");
  101. return harvestedAmount;
  102. }
  103. public void AddResources(int amount)
  104. {
  105. currentResources = Mathf.Min(maxResources, currentResources + amount);
  106. UpdateVisualState();
  107. }
  108. void UpdateVisualState()
  109. {
  110. if (nodeRenderer != null)
  111. {
  112. // Change color based on resource percentage
  113. Color baseColor = nodeType switch
  114. {
  115. ResourceNodeType.Tree => Color.green,
  116. ResourceNodeType.Stone => Color.gray,
  117. ResourceNodeType.Farm => Color.yellow,
  118. _ => Color.white
  119. };
  120. // Fade color based on resources remaining
  121. float intensity = Mathf.Lerp(0.3f, 1f, ResourcePercentage);
  122. nodeRenderer.material.color = baseColor * intensity;
  123. }
  124. // Show depleted visual if empty
  125. if (depletedVisual != null)
  126. {
  127. depletedVisual.SetActive(currentResources == 0);
  128. }
  129. }
  130. public void OnSelected()
  131. {
  132. isSelected = true;
  133. if (selectionIndicator != null)
  134. selectionIndicator.SetActive(true);
  135. }
  136. public void OnDeselected()
  137. {
  138. isSelected = false;
  139. if (selectionIndicator != null)
  140. selectionIndicator.SetActive(false);
  141. }
  142. public string GetNodeInfo()
  143. {
  144. string icon = nodeType switch
  145. {
  146. ResourceNodeType.Tree => "🌲",
  147. ResourceNodeType.Stone => "🪨",
  148. ResourceNodeType.Farm => "🌾",
  149. _ => "📦"
  150. };
  151. string status = isBeingHarvested ? "⚙️ Being Harvested" : "✓ Available";
  152. if (currentHarvester != null)
  153. status += $" by {currentHarvester.villagerName}";
  154. float percentage = (float)currentResources / maxResources * 100f;
  155. return $"{icon} {nodeType}\nResources: {currentResources}/{maxResources} ({percentage:F0}%)\n{status}";
  156. }
  157. }