ConstructionSite.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using UnityEngine;
  2. public class ConstructionSite : MonoBehaviour
  3. {
  4. [Header("Construction Info")]
  5. public string buildingName;
  6. public BuildingTemplate buildingTemplate;
  7. public float constructionTime = 10f;
  8. public float constructionProgress = 0f;
  9. public bool isComplete = false;
  10. [Header("References")]
  11. public Villager assignedBuilder;
  12. private Renderer constructionRenderer;
  13. private GameObject progressBar;
  14. private GameObject progressBarBackground;
  15. private Transform progressBarFill;
  16. void Start()
  17. {
  18. constructionRenderer = GetComponent<Renderer>();
  19. CreateProgressBar();
  20. UpdateVisualState();
  21. Debug.Log($"Construction site created for {buildingName}");
  22. }
  23. public void AdvanceConstruction(float deltaTime)
  24. {
  25. if (isComplete || assignedBuilder == null) return;
  26. constructionProgress += deltaTime;
  27. if (constructionProgress >= constructionTime)
  28. {
  29. CompleteConstruction();
  30. }
  31. UpdateVisualState();
  32. }
  33. public void CompleteConstruction()
  34. {
  35. isComplete = true;
  36. constructionProgress = constructionTime;
  37. // Create the actual building
  38. GameObject completedBuilding = CreateCompletedBuilding();
  39. Debug.Log($"Construction of {buildingName} completed!");
  40. // Destroy this construction site
  41. Destroy(gameObject);
  42. }
  43. void CreateProgressBar()
  44. {
  45. // Create progress bar background
  46. progressBarBackground = GameObject.CreatePrimitive(PrimitiveType.Cube);
  47. progressBarBackground.name = "ProgressBarBG";
  48. progressBarBackground.transform.SetParent(transform);
  49. progressBarBackground.transform.localPosition = new Vector3(0, 2f, 0);
  50. progressBarBackground.transform.localScale = new Vector3(2f, 0.2f, 0.1f);
  51. progressBarBackground.GetComponent<Renderer>().material.color = Color.gray;
  52. // Create progress bar fill
  53. GameObject progressBarFillObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
  54. progressBarFillObj.name = "ProgressBarFill";
  55. progressBarFillObj.transform.SetParent(progressBarBackground.transform);
  56. progressBarFillObj.transform.localPosition = new Vector3(-0.5f, 0, 0.1f);
  57. progressBarFillObj.transform.localScale = new Vector3(0f, 1.2f, 1.2f);
  58. progressBarFillObj.GetComponent<Renderer>().material.color = Color.green;
  59. progressBarFill = progressBarFillObj.transform;
  60. // Remove colliders from progress bar elements
  61. Destroy(progressBarBackground.GetComponent<Collider>());
  62. Destroy(progressBarFillObj.GetComponent<Collider>());
  63. }
  64. GameObject CreateCompletedBuilding()
  65. {
  66. GameObject building = null;
  67. if (buildingTemplate.prefab != null)
  68. {
  69. building = Instantiate(buildingTemplate.prefab, transform.position, transform.rotation);
  70. }
  71. else
  72. {
  73. // Create basic building if no prefab exists
  74. building = CreateBasicBuilding();
  75. }
  76. // Add building component
  77. Building buildingComponent = building.GetComponent<Building>();
  78. if (buildingComponent == null)
  79. {
  80. buildingComponent = building.AddComponent<Building>();
  81. }
  82. buildingComponent.buildingName = buildingTemplate.name;
  83. buildingComponent.category = buildingTemplate.category;
  84. // Special setup for different building types
  85. if (buildingTemplate.name == "Farmhouse")
  86. {
  87. SetupFarmhouse(building);
  88. }
  89. return building;
  90. }
  91. GameObject CreateBasicBuilding()
  92. {
  93. GameObject building = GameObject.CreatePrimitive(PrimitiveType.Cube);
  94. building.name = buildingTemplate.name;
  95. building.transform.position = transform.position;
  96. building.transform.localScale = new Vector3(buildingTemplate.size.x, 1.5f, buildingTemplate.size.y);
  97. // Color based on building type
  98. Renderer renderer = building.GetComponent<Renderer>();
  99. if (buildingTemplate.name == "Farmhouse")
  100. {
  101. renderer.material.color = new Color(0.8f, 0.6f, 0.4f); // Light brown
  102. }
  103. else if (buildingTemplate.category == BuildingCategory.Working)
  104. {
  105. renderer.material.color = new Color(0.6f, 0.4f, 0.2f); // Dark brown
  106. }
  107. else
  108. {
  109. renderer.material.color = Color.gray;
  110. }
  111. return building;
  112. }
  113. void SetupFarmhouse(GameObject farmhouse)
  114. {
  115. // Add farmhouse-specific component
  116. Farmhouse farmhouseComponent = farmhouse.GetComponent<Farmhouse>();
  117. if (farmhouseComponent == null)
  118. {
  119. farmhouseComponent = farmhouse.AddComponent<Farmhouse>();
  120. }
  121. Debug.Log("Farmhouse setup completed with field building capabilities");
  122. }
  123. void UpdateVisualState()
  124. {
  125. if (constructionRenderer == null) return;
  126. float progress = constructionProgress / constructionTime;
  127. // Change color based on progress (gray foundation -> green when complete)
  128. Color baseColor = new Color(0.7f, 0.7f, 0.7f, 0.8f); // Gray foundation
  129. Color progressColor = Color.Lerp(Color.yellow, Color.green, progress);
  130. constructionRenderer.material.color = Color.Lerp(baseColor, progressColor, progress * 0.7f);
  131. // Change height based on progress (building grows upward)
  132. Vector3 scale = transform.localScale;
  133. scale.y = 0.3f + (progress * 1.2f); // Grows from 0.3 to 1.5 height
  134. transform.localScale = scale;
  135. // Update progress bar
  136. if (progressBarFill != null)
  137. {
  138. Vector3 fillScale = progressBarFill.localScale;
  139. fillScale.x = progress; // Fill bar from 0 to 1
  140. progressBarFill.localScale = fillScale;
  141. Vector3 fillPos = progressBarFill.localPosition;
  142. fillPos.x = -0.5f + (progress * 0.5f); // Center the fill
  143. progressBarFill.localPosition = fillPos;
  144. }
  145. }
  146. public string GetConstructionInfo()
  147. {
  148. float progressPercent = (constructionProgress / constructionTime) * 100f;
  149. string builderStatus = assignedBuilder != null ? assignedBuilder.villagerName : "⚠️ BUILDER NEEDED";
  150. return $"🏗️ {buildingName}\nProgress: {progressPercent:F0}%\nBuilder: {builderStatus}";
  151. }
  152. public bool NeedsBuilder()
  153. {
  154. return !isComplete && assignedBuilder == null;
  155. }
  156. public void AssignBuilder(Villager builder)
  157. {
  158. assignedBuilder = builder;
  159. Debug.Log($"Builder {builder.villagerName} assigned to {buildingName} construction");
  160. }
  161. public void RemoveBuilder()
  162. {
  163. if (assignedBuilder != null)
  164. {
  165. Debug.Log($"Builder {assignedBuilder.villagerName} removed from {buildingName} construction");
  166. assignedBuilder = null;
  167. }
  168. }
  169. }