| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- 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 bool isSelected = false;
- 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()
- {
- // Regenerate resources over time if not being harvested
- if (!isBeingHarvested && currentResources < maxResources)
- {
- float timeSinceLastRegen = Time.time - lastRegenerationTime;
- if (timeSinceLastRegen >= 1f) // Regenerate every second
- {
- int regenAmount = Mathf.FloorToInt(regenerationRate);
- AddResources(regenAmount);
- lastRegenerationTime = Time.time;
- }
- }
- }
- 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()
- {
- isSelected = true;
- if (selectionIndicator != null)
- selectionIndicator.SetActive(true);
- }
- public void OnDeselected()
- {
- isSelected = false;
- if (selectionIndicator != null)
- selectionIndicator.SetActive(false);
- }
- public string GetNodeInfo()
- {
- string status = isBeingHarvested ? "Being Harvested" : "Available";
- if (currentHarvester != null)
- status += $" by {currentHarvester.villagerName}";
- return $"{nodeType} Node\nResources: {currentResources}/{maxResources}\nStatus: {status}";
- }
- void OnMouseDown()
- {
- if (Mouse.current != null && Mouse.current.leftButton.wasPressedThisFrame)
- {
- if (GameManager.Instance?.selectionManager != null)
- {
- GameManager.Instance.selectionManager.SelectObject(gameObject);
- }
- }
- }
- }
|