| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- 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<Renderer>();
- 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<Renderer>().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<Renderer>().material.color = Color.green;
- progressBarFill = progressBarFillObj.transform;
- // Remove colliders from progress bar elements
- Destroy(progressBarBackground.GetComponent<Collider>());
- Destroy(progressBarFillObj.GetComponent<Collider>());
- }
- 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<Building>();
- if (buildingComponent == null)
- {
- buildingComponent = building.AddComponent<Building>();
- }
- 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<Renderer>();
- 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<Farmhouse>();
- if (farmhouseComponent == null)
- {
- farmhouseComponent = farmhouse.AddComponent<Farmhouse>();
- }
- 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;
- }
- }
- }
|