Building.cs 4.3 KB

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