using UnityEngine; using System; using System.Collections; using System.Collections.Generic; [System.Serializable] public class StorageConfig { public Resource.ResourceType resourceType; public int capacity; } public class Building : MonoBehaviour { public enum BuildingType { House, Farm, Barracks, TownHall } public enum BuildingState { Unbuilt, Constructing, Active, Destroyed } [SerializeField] float constructionTime; [SerializeField] private Resource.ResourceType[] constructionResources; [SerializeField] private int[] constructionCost; private BuildingState buildingState; [SerializeField] private BuildingType buildingType; [SerializeField] private GameObject unbuiltModel, constructingModel, activeModel, destroyedModel; [SerializeField] private StorageConfig[] storageConfig = new StorageConfig[0]; public Dictionary resourcesToStore = new Dictionary(); // Start is called once before the first execution of Update after the MonoBehaviour is created void Start() { if (buildingType == BuildingType.TownHall) { buildingState = BuildingState.Active; } else { buildingState = BuildingState.Unbuilt; } SetBuildingModel(buildingState); InitializeStorage(); } private void InitializeStorage() { resourcesToStore.Clear(); foreach (StorageConfig config in storageConfig) { resourcesToStore[config.resourceType] = config.capacity; Debug.Log($"Storage capacity for {config.resourceType} set to {config.capacity}"); } } // Update is called once per frame void Update() { } public BuildingType GetBuildingType() { return buildingType; } public BuildingState GetBuildingState() { return buildingState; } private void SetBuildingModel(BuildingState state) { switch (state) { case BuildingState.Unbuilt: unbuiltModel.SetActive(true); constructingModel.SetActive(false); activeModel.SetActive(false); destroyedModel.SetActive(false); break; case BuildingState.Constructing: unbuiltModel.SetActive(false); constructingModel.SetActive(true); activeModel.SetActive(false); destroyedModel.SetActive(false); StartCoroutine(ConstructionTimer(constructionTime)); break; case BuildingState.Active: unbuiltModel.SetActive(false); constructingModel.SetActive(false); activeModel.SetActive(true); destroyedModel.SetActive(false); break; case BuildingState.Destroyed: unbuiltModel.SetActive(false); constructingModel.SetActive(false); activeModel.SetActive(false); destroyedModel.SetActive(true); break; } } public void StartBuilding() { if (buildingState != BuildingState.Unbuilt) return; for (int i = 0; i < constructionResources.Length && i < constructionCost.Length; i++) { if (GameManager.Instance.GetResource(constructionResources[i]) < constructionCost[i]) { Debug.Log("Not enough resources"); return; } GameManager.Instance.AddResource(constructionResources[i], -constructionCost[i]); } Debug.Log("Building Started"); buildingState = BuildingState.Constructing; SetBuildingModel(buildingState); } public void FinishBuilding() { if (buildingState != BuildingState.Constructing) return; buildingState = BuildingState.Active; SetBuildingModel(buildingState); if(buildingType == BuildingType.House) UnitManagement.Instance.CreateUnit("Citizen", storageConfig.Length, transform.position); } public void DestroyBuilding() { if (buildingState != BuildingState.Active) return; buildingState = BuildingState.Destroyed; SetBuildingModel(buildingState); } public void CancelBuilding() { if (buildingState != BuildingState.Constructing) return; buildingState = BuildingState.Unbuilt; SetBuildingModel(buildingState); } private IEnumerator ConstructionTimer(float constructionTime) { while (constructionTime > 0) { yield return Time.deltaTime; constructionTime -= Time.deltaTime; } FinishBuilding(); } public void Interact(NPC npcInteracting) { switch (buildingState) { case BuildingState.Unbuilt: StartBuilding(); break; case BuildingState.Constructing: break; case BuildingState.Active: //DestroyBuilding(); var npcInventory = npcInteracting.GetInventory(); var entries = new List>(npcInventory); foreach (KeyValuePair entry in entries) { GameManager.Instance.AddResource(entry.Key, entry.Value); UIController.Instance.UpdateStats(); npcInteracting.inventory.Remove(entry.Key); } break; case BuildingState.Destroyed: CancelBuilding(); break; } } public void SetUpStore(StorageConfig[] configs) { if (configs == null) { Debug.LogWarning("SetUpStore received null config array."); return; } resourcesToStore.Clear(); foreach (StorageConfig config in configs) { resourcesToStore[config.resourceType] = config.capacity; Debug.Log($"Storage capacity for {config.resourceType} set to {config.capacity}"); } } }