ResourceNode.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. // Regenerate resources over time if not being harvested
  54. if (!isBeingHarvested && currentResources < maxResources)
  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. }
  65. public bool StartHarvesting(Villager harvester)
  66. {
  67. if (IsOccupied || !IsAvailable)
  68. return false;
  69. isBeingHarvested = true;
  70. currentHarvester = harvester;
  71. Debug.Log($"{harvester.villagerName} started harvesting {nodeType}");
  72. return true;
  73. }
  74. public void StopHarvesting()
  75. {
  76. isBeingHarvested = false;
  77. currentHarvester = null;
  78. Debug.Log($"Stopped harvesting {nodeType}");
  79. }
  80. public int HarvestResources(int requestedAmount, float harvesterEfficiency)
  81. {
  82. if (!IsAvailable || !isBeingHarvested)
  83. return 0;
  84. // Calculate actual harvest amount based on efficiency and available resources
  85. int baseAmount = Mathf.Min(requestedAmount, currentResources);
  86. int harvestedAmount = Mathf.RoundToInt(baseAmount * harvesterEfficiency);
  87. currentResources -= harvestedAmount;
  88. currentResources = Mathf.Max(0, currentResources);
  89. UpdateVisualState();
  90. Debug.Log($"Harvested {harvestedAmount} {resourceType} from {nodeType}. Remaining: {currentResources}");
  91. return harvestedAmount;
  92. }
  93. public void AddResources(int amount)
  94. {
  95. currentResources = Mathf.Min(maxResources, currentResources + amount);
  96. UpdateVisualState();
  97. }
  98. void UpdateVisualState()
  99. {
  100. if (nodeRenderer != null)
  101. {
  102. // Change color based on resource percentage
  103. Color baseColor = nodeType switch
  104. {
  105. ResourceNodeType.Tree => Color.green,
  106. ResourceNodeType.Stone => Color.gray,
  107. ResourceNodeType.Farm => Color.yellow,
  108. _ => Color.white
  109. };
  110. // Fade color based on resources remaining
  111. float intensity = Mathf.Lerp(0.3f, 1f, ResourcePercentage);
  112. nodeRenderer.material.color = baseColor * intensity;
  113. }
  114. // Show depleted visual if empty
  115. if (depletedVisual != null)
  116. {
  117. depletedVisual.SetActive(currentResources == 0);
  118. }
  119. }
  120. public void OnSelected()
  121. {
  122. isSelected = true;
  123. if (selectionIndicator != null)
  124. selectionIndicator.SetActive(true);
  125. }
  126. public void OnDeselected()
  127. {
  128. isSelected = false;
  129. if (selectionIndicator != null)
  130. selectionIndicator.SetActive(false);
  131. }
  132. public string GetNodeInfo()
  133. {
  134. string status = isBeingHarvested ? "Being Harvested" : "Available";
  135. if (currentHarvester != null)
  136. status += $" by {currentHarvester.villagerName}";
  137. return $"{nodeType} Node\nResources: {currentResources}/{maxResources}\nStatus: {status}";
  138. }
  139. void OnMouseDown()
  140. {
  141. if (Mouse.current != null && Mouse.current.leftButton.wasPressedThisFrame)
  142. {
  143. if (GameManager.Instance?.selectionManager != null)
  144. {
  145. GameManager.Instance.selectionManager.SelectObject(gameObject);
  146. }
  147. }
  148. }
  149. }