using UnityEngine; public class ConstructionSite : MonoBehaviour { [Header("Construction Info")] public string buildingName; public BuildingTemplate buildingTemplate; public float constructionTime = 10f; public float constructionProgress = 0f; public bool isComplete = false; [Header("References")] public Villager assignedBuilder; private Renderer constructionRenderer; private GameObject progressBar; private GameObject progressBarBackground; private Transform progressBarFill; void Start() { constructionRenderer = GetComponent(); CreateProgressBar(); UpdateVisualState(); Debug.Log($"Construction site created for {buildingName}"); } public void AdvanceConstruction(float deltaTime) { if (isComplete || assignedBuilder == null) return; constructionProgress += deltaTime; if (constructionProgress >= constructionTime) { CompleteConstruction(); } UpdateVisualState(); } public void CompleteConstruction() { isComplete = true; constructionProgress = constructionTime; // Create the actual building GameObject completedBuilding = CreateCompletedBuilding(); Debug.Log($"Construction of {buildingName} completed!"); // Destroy this construction site Destroy(gameObject); } void CreateProgressBar() { // Create progress bar background progressBarBackground = GameObject.CreatePrimitive(PrimitiveType.Cube); progressBarBackground.name = "ProgressBarBG"; progressBarBackground.transform.SetParent(transform); progressBarBackground.transform.localPosition = new Vector3(0, 2f, 0); progressBarBackground.transform.localScale = new Vector3(2f, 0.2f, 0.1f); progressBarBackground.GetComponent().material.color = Color.gray; // Create progress bar fill GameObject progressBarFillObj = GameObject.CreatePrimitive(PrimitiveType.Cube); progressBarFillObj.name = "ProgressBarFill"; progressBarFillObj.transform.SetParent(progressBarBackground.transform); progressBarFillObj.transform.localPosition = new Vector3(-0.5f, 0, 0.1f); progressBarFillObj.transform.localScale = new Vector3(0f, 1.2f, 1.2f); progressBarFillObj.GetComponent().material.color = Color.green; progressBarFill = progressBarFillObj.transform; // Remove colliders from progress bar elements Destroy(progressBarBackground.GetComponent()); Destroy(progressBarFillObj.GetComponent()); } GameObject CreateCompletedBuilding() { GameObject building = null; if (buildingTemplate.prefab != null) { building = Instantiate(buildingTemplate.prefab, transform.position, transform.rotation); } else { // Create basic building if no prefab exists building = CreateBasicBuilding(); } // Add building component Building buildingComponent = building.GetComponent(); if (buildingComponent == null) { buildingComponent = building.AddComponent(); } buildingComponent.buildingName = buildingTemplate.name; buildingComponent.category = buildingTemplate.category; // Special setup for different building types if (buildingTemplate.name == "Farmhouse") { SetupFarmhouse(building); } return building; } GameObject CreateBasicBuilding() { GameObject building = GameObject.CreatePrimitive(PrimitiveType.Cube); building.name = buildingTemplate.name; building.transform.position = transform.position; building.transform.localScale = new Vector3(buildingTemplate.size.x, 1.5f, buildingTemplate.size.y); // Color based on building type Renderer renderer = building.GetComponent(); if (buildingTemplate.name == "Farmhouse") { renderer.material.color = new Color(0.8f, 0.6f, 0.4f); // Light brown } else if (buildingTemplate.category == BuildingCategory.Working) { renderer.material.color = new Color(0.6f, 0.4f, 0.2f); // Dark brown } else { renderer.material.color = Color.gray; } return building; } void SetupFarmhouse(GameObject farmhouse) { // Add farmhouse-specific component Farmhouse farmhouseComponent = farmhouse.GetComponent(); if (farmhouseComponent == null) { farmhouseComponent = farmhouse.AddComponent(); } Debug.Log("Farmhouse setup completed with field building capabilities"); } void UpdateVisualState() { if (constructionRenderer == null) return; float progress = constructionProgress / constructionTime; // Change color based on progress (gray foundation -> green when complete) Color baseColor = new Color(0.7f, 0.7f, 0.7f, 0.8f); // Gray foundation Color progressColor = Color.Lerp(Color.yellow, Color.green, progress); constructionRenderer.material.color = Color.Lerp(baseColor, progressColor, progress * 0.7f); // Change height based on progress (building grows upward) Vector3 scale = transform.localScale; scale.y = 0.3f + (progress * 1.2f); // Grows from 0.3 to 1.5 height transform.localScale = scale; // Update progress bar if (progressBarFill != null) { Vector3 fillScale = progressBarFill.localScale; fillScale.x = progress; // Fill bar from 0 to 1 progressBarFill.localScale = fillScale; Vector3 fillPos = progressBarFill.localPosition; fillPos.x = -0.5f + (progress * 0.5f); // Center the fill progressBarFill.localPosition = fillPos; } } public string GetConstructionInfo() { float progressPercent = (constructionProgress / constructionTime) * 100f; string builderStatus = assignedBuilder != null ? assignedBuilder.villagerName : "⚠️ BUILDER NEEDED"; return $"🏗️ {buildingName}\nProgress: {progressPercent:F0}%\nBuilder: {builderStatus}"; } public bool NeedsBuilder() { return !isComplete && assignedBuilder == null; } public void AssignBuilder(Villager builder) { assignedBuilder = builder; Debug.Log($"Builder {builder.villagerName} assigned to {buildingName} construction"); } public void RemoveBuilder() { if (assignedBuilder != null) { Debug.Log($"Builder {assignedBuilder.villagerName} removed from {buildingName} construction"); assignedBuilder = null; } } }