| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- 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<ResourceType, int> storedResources = new Dictionary<ResourceType, int>();
- [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<Villager> assignedWorkers = new List<Villager>();
- 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<Villager> GetAssignedWorkers()
- {
- return new List<Villager>(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);
- }
- }
|