ResourceNode.cs 5.4 KB

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