using UnityEngine; using System.Collections.Generic; [System.Serializable] public enum BuildingType { TownHall, Storage, LumberMill, Quarry, Farm, House, BuildersWorkshop } public class Building : MonoBehaviour, ISelectable { [Header("Building Info")] public BuildingType buildingType; public string buildingName; public BuildingCategory category; public int maxWorkers = 1; public float workRadius = 5f; [Header("Storage (if applicable)")] public int storageCapacity = 100; public Dictionary storedResources = new Dictionary(); [Header("Production (if applicable)")] public ResourceType producedResource; public float baseProductionTime = 5f; public int baseProductionAmount = 1; [Header("Visual")] public Renderer buildingRenderer; public GameObject selectionIndicator; private List assignedWorkers = new List(); private bool isSelected = false; public bool IsSelected => isSelected; public int CurrentWorkers => assignedWorkers.Count; public bool HasCapacity => CurrentWorkers < maxWorkers; public void Start() { // Initialize storage foreach (ResourceType resourceType in System.Enum.GetValues(typeof(ResourceType))) { storedResources[resourceType] = 0; } if (selectionIndicator != null) selectionIndicator.SetActive(false); } public void OnSelected() { isSelected = true; if (selectionIndicator != null) selectionIndicator.SetActive(true); } public void OnDeselected() { isSelected = false; if (selectionIndicator != null) selectionIndicator.SetActive(false); } public bool CanStoreResource(ResourceType resourceType, int amount) { int currentTotal = GetTotalStoredResources(); return currentTotal + amount <= storageCapacity; } public virtual bool StoreResource(ResourceType resourceType, int amount) { if (CanStoreResource(resourceType, amount)) { storedResources[resourceType] += amount; return true; } return false; } public virtual bool WithdrawResource(ResourceType resourceType, int amount) { if (storedResources[resourceType] >= amount) { storedResources[resourceType] -= amount; return true; } return false; } public int GetStoredResource(ResourceType resourceType) { return storedResources.ContainsKey(resourceType) ? storedResources[resourceType] : 0; } public int GetTotalStoredResources() { int total = 0; foreach (var kvp in storedResources) { total += kvp.Value; } return total; } public bool AssignWorker(Villager villager) { if (HasCapacity && !assignedWorkers.Contains(villager)) { assignedWorkers.Add(villager); return true; } return false; } public void RemoveWorker(Villager villager) { assignedWorkers.Remove(villager); } public List GetAssignedWorkers() { return new List(assignedWorkers); } public Vector3 GetWorkPosition() { // Return a position near the building where villagers can work Vector3 basePos = transform.position; Vector2 randomCircle = Random.insideUnitCircle * workRadius; return basePos + new Vector3(randomCircle.x, 0, randomCircle.y); } public virtual string GetBuildingInfo() { string info = $"{buildingName}\nType: {buildingType}\nWorkers: {CurrentWorkers}/{maxWorkers}"; if (buildingType == BuildingType.TownHall || buildingType == BuildingType.Storage) { info += $"\nStorage: {GetTotalStoredResources()}/{storageCapacity}"; foreach (var resource in storedResources) { if (resource.Value > 0) info += $"\n{resource.Key}: {resource.Value}"; } } return info; } void OnDrawGizmosSelected() { // Draw work radius Gizmos.color = Color.yellow; Gizmos.DrawWireSphere(transform.position, workRadius); } }