using UnityEngine; using UnityEngine.InputSystem; [System.Serializable] public enum ResourceNodeType { Tree, Stone, Farm } public class ResourceNode : MonoBehaviour, ISelectable { [Header("Resource Node Info")] public ResourceNodeType nodeType; public ResourceType resourceType; public int maxResources = 100; public int currentResources = 100; public float regenerationRate = 1f; // Resources per second when not being harvested [Header("Harvesting")] public bool isBeingHarvested = false; public Villager currentHarvester; public float harvestTime = 3f; [Header("Visual")] public Renderer nodeRenderer; public GameObject selectionIndicator; public GameObject depletedVisual; private float lastRegenerationTime; public bool IsAvailable => currentResources > 0; public bool IsOccupied => isBeingHarvested && currentHarvester != null; public float ResourcePercentage => (float)currentResources / maxResources; void Start() { lastRegenerationTime = Time.time; if (selectionIndicator != null) selectionIndicator.SetActive(false); if (depletedVisual != null) depletedVisual.SetActive(false); SetAppropriateTag(); UpdateVisualState(); } void SetAppropriateTag() { gameObject.tag = nodeType switch { ResourceNodeType.Tree => "Tree", ResourceNodeType.Stone => "Stone", ResourceNodeType.Farm => "Farm", _ => "Untagged" }; } void Update() { // Only farms regenerate - trees and stones are finite if (!isBeingHarvested && currentResources < maxResources && nodeType == ResourceNodeType.Farm) { float timeSinceLastRegen = Time.time - lastRegenerationTime; if (timeSinceLastRegen >= 1f) // Regenerate every second { int regenAmount = Mathf.FloorToInt(regenerationRate); AddResources(regenAmount); lastRegenerationTime = Time.time; } } // Destroy depleted resource nodes (except farms) if (currentResources <= 0 && nodeType != ResourceNodeType.Farm) { if (currentHarvester != null) { currentHarvester.targetWorkplace = null; // Clear the workplace reference } Debug.Log($"{nodeType} depleted and removed"); Destroy(gameObject); } } public bool StartHarvesting(Villager harvester) { if (IsOccupied || !IsAvailable) return false; isBeingHarvested = true; currentHarvester = harvester; Debug.Log($"{harvester.villagerName} started harvesting {nodeType}"); return true; } public void StopHarvesting() { isBeingHarvested = false; currentHarvester = null; Debug.Log($"Stopped harvesting {nodeType}"); } public int HarvestResources(int requestedAmount, float harvesterEfficiency) { if (!IsAvailable || !isBeingHarvested) return 0; // Calculate actual harvest amount based on efficiency and available resources int baseAmount = Mathf.Min(requestedAmount, currentResources); int harvestedAmount = Mathf.RoundToInt(baseAmount * harvesterEfficiency); currentResources -= harvestedAmount; currentResources = Mathf.Max(0, currentResources); UpdateVisualState(); Debug.Log($"Harvested {harvestedAmount} {resourceType} from {nodeType}. Remaining: {currentResources}"); return harvestedAmount; } public void AddResources(int amount) { currentResources = Mathf.Min(maxResources, currentResources + amount); UpdateVisualState(); } void UpdateVisualState() { if (nodeRenderer != null) { // Change color based on resource percentage Color baseColor = nodeType switch { ResourceNodeType.Tree => Color.green, ResourceNodeType.Stone => Color.gray, ResourceNodeType.Farm => Color.yellow, _ => Color.white }; // Fade color based on resources remaining float intensity = Mathf.Lerp(0.3f, 1f, ResourcePercentage); nodeRenderer.material.color = baseColor * intensity; } // Show depleted visual if empty if (depletedVisual != null) { depletedVisual.SetActive(currentResources == 0); } } public void OnSelected() { if (selectionIndicator != null) selectionIndicator.SetActive(true); } public void OnDeselected() { if (selectionIndicator != null) selectionIndicator.SetActive(false); } public string GetNodeInfo() { string icon = nodeType switch { ResourceNodeType.Tree => "🌲", ResourceNodeType.Stone => "🪨", ResourceNodeType.Farm => "🌾", _ => "📦" }; string status = isBeingHarvested ? "⚙️ Being Harvested" : "✓ Available"; if (currentHarvester != null) status += $" by {currentHarvester.villagerName}"; float percentage = (float)currentResources / maxResources * 100f; return $"{icon} {nodeType}\nResources: {currentResources}/{maxResources} ({percentage:F0}%)\n{status}"; } }