Building.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. [System.Serializable]
  4. public enum BuildingType
  5. {
  6. TownHall,
  7. Storage,
  8. LumberMill,
  9. Quarry,
  10. Farm,
  11. House
  12. }
  13. public class Building : MonoBehaviour, ISelectable
  14. {
  15. [Header("Building Info")]
  16. public BuildingType buildingType;
  17. public string buildingName;
  18. public int maxWorkers = 1;
  19. public float workRadius = 5f;
  20. [Header("Storage (if applicable)")]
  21. public int storageCapacity = 100;
  22. public Dictionary<ResourceType, int> storedResources = new Dictionary<ResourceType, int>();
  23. [Header("Production (if applicable)")]
  24. public ResourceType producedResource;
  25. public float baseProductionTime = 5f;
  26. public int baseProductionAmount = 1;
  27. [Header("Visual")]
  28. public Renderer buildingRenderer;
  29. public GameObject selectionIndicator;
  30. private List<Villager> assignedWorkers = new List<Villager>();
  31. private bool isSelected = false;
  32. public bool IsSelected => isSelected;
  33. public int CurrentWorkers => assignedWorkers.Count;
  34. public bool HasCapacity => CurrentWorkers < maxWorkers;
  35. public void Start()
  36. {
  37. // Initialize storage
  38. foreach (ResourceType resourceType in System.Enum.GetValues(typeof(ResourceType)))
  39. {
  40. storedResources[resourceType] = 0;
  41. }
  42. if (selectionIndicator != null)
  43. selectionIndicator.SetActive(false);
  44. }
  45. public void OnSelected()
  46. {
  47. isSelected = true;
  48. if (selectionIndicator != null)
  49. selectionIndicator.SetActive(true);
  50. }
  51. public void OnDeselected()
  52. {
  53. isSelected = false;
  54. if (selectionIndicator != null)
  55. selectionIndicator.SetActive(false);
  56. }
  57. public bool CanStoreResource(ResourceType resourceType, int amount)
  58. {
  59. int currentTotal = GetTotalStoredResources();
  60. return currentTotal + amount <= storageCapacity;
  61. }
  62. public virtual bool StoreResource(ResourceType resourceType, int amount)
  63. {
  64. if (CanStoreResource(resourceType, amount))
  65. {
  66. storedResources[resourceType] += amount;
  67. return true;
  68. }
  69. return false;
  70. }
  71. public virtual bool WithdrawResource(ResourceType resourceType, int amount)
  72. {
  73. if (storedResources[resourceType] >= amount)
  74. {
  75. storedResources[resourceType] -= amount;
  76. return true;
  77. }
  78. return false;
  79. }
  80. public int GetStoredResource(ResourceType resourceType)
  81. {
  82. return storedResources.ContainsKey(resourceType) ? storedResources[resourceType] : 0;
  83. }
  84. public int GetTotalStoredResources()
  85. {
  86. int total = 0;
  87. foreach (var kvp in storedResources)
  88. {
  89. total += kvp.Value;
  90. }
  91. return total;
  92. }
  93. public bool AssignWorker(Villager villager)
  94. {
  95. if (HasCapacity && !assignedWorkers.Contains(villager))
  96. {
  97. assignedWorkers.Add(villager);
  98. return true;
  99. }
  100. return false;
  101. }
  102. public void RemoveWorker(Villager villager)
  103. {
  104. assignedWorkers.Remove(villager);
  105. }
  106. public List<Villager> GetAssignedWorkers()
  107. {
  108. return new List<Villager>(assignedWorkers);
  109. }
  110. public Vector3 GetWorkPosition()
  111. {
  112. // Return a position near the building where villagers can work
  113. Vector3 basePos = transform.position;
  114. Vector2 randomCircle = Random.insideUnitCircle * workRadius;
  115. return basePos + new Vector3(randomCircle.x, 0, randomCircle.y);
  116. }
  117. public virtual string GetBuildingInfo()
  118. {
  119. string info = $"{buildingName}\nType: {buildingType}\nWorkers: {CurrentWorkers}/{maxWorkers}";
  120. if (buildingType == BuildingType.TownHall || buildingType == BuildingType.Storage)
  121. {
  122. info += $"\nStorage: {GetTotalStoredResources()}/{storageCapacity}";
  123. foreach (var resource in storedResources)
  124. {
  125. if (resource.Value > 0)
  126. info += $"\n{resource.Key}: {resource.Value}";
  127. }
  128. }
  129. return info;
  130. }
  131. void OnDrawGizmosSelected()
  132. {
  133. // Draw work radius
  134. Gizmos.color = Color.yellow;
  135. Gizmos.DrawWireSphere(transform.position, workRadius);
  136. }
  137. }