BuildingBlueprintController.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using KairoEngine.Core;
  5. using Sirenix.OdinInspector;
  6. namespace KairoEngine.VoxelBuildingSystem
  7. {
  8. [HideMonoScript]
  9. public class BuildingBlueprintController : MonoBehaviour
  10. {
  11. public VoxelBuildingSystem buildingSystem;
  12. public Transform currentBlueprint;
  13. [BoxGroup("Materials")] public Material blueprintMain;
  14. [BoxGroup("Materials")] public Material blueprintDetail;
  15. [BoxGroup("Materials")] public Material invalidMain;
  16. [BoxGroup("Materials")] public Material invalidDetail;
  17. private void Start() {
  18. RefreshVisual();
  19. GenericEvents.StartListening("OnBuildingTemplateChanged", OnBuildingTemplateChanged);
  20. }
  21. private void OnDisable()
  22. {
  23. GenericEvents.StopListening("OnBuildingTemplateChanged", OnBuildingTemplateChanged);
  24. }
  25. private void OnBuildingTemplateChanged() {
  26. RefreshVisual();
  27. }
  28. private void LateUpdate() {
  29. if(currentBlueprint == null) return;
  30. var template = buildingSystem.buildingTemplates[buildingSystem.currentBuildingTemplate];
  31. Vector3Int targetPosition = buildingSystem.GetMouseBuildingPosition();
  32. Vector3Int dir = buildingSystem.rotationList[buildingSystem.currentRotationIndex];
  33. Quaternion rotation = Quaternion.Euler(dir.x, dir.y, dir.z);
  34. currentBlueprint.transform.position = Vector3.Lerp(currentBlueprint.transform.position, targetPosition, Time.deltaTime * 15f);
  35. currentBlueprint.transform.rotation = Quaternion.Lerp(currentBlueprint.transform.rotation, rotation, Time.deltaTime * 15f);
  36. //currentBlueprint.transform.localEulerAngles = Vector3.Lerp(transform.localEulerAngles, dir, Time.deltaTime * 15f);
  37. //currentBlueprint.transform.localEulerAngles = dir;
  38. if(buildingSystem.tool == VoxelBuildingTool.Build) currentBlueprint.gameObject.SetActive(true);
  39. else currentBlueprint.gameObject.SetActive(false);
  40. CanBuild();
  41. }
  42. private void RefreshVisual() {
  43. Quaternion currentRotation = currentBlueprint.transform.rotation;
  44. Vector3 currentPosition = currentBlueprint.transform.position;
  45. if (currentBlueprint != null) {
  46. Destroy(currentBlueprint.gameObject);
  47. currentBlueprint = null;
  48. }
  49. BuildingTemplate buildingTemplate = buildingSystem.buildingTemplates[buildingSystem.currentBuildingTemplate];
  50. if (buildingTemplate != null) {
  51. Vector3 targetPosition = buildingSystem.GetMouseWorldSnappedPosition();
  52. currentBlueprint = Instantiate(buildingTemplate.visual, currentPosition, currentRotation);
  53. currentBlueprint.parent = transform;
  54. // currentBlueprint.localPosition = Vector3.zero;
  55. // currentBlueprint.localEulerAngles = Vector3.zero;
  56. }
  57. }
  58. private bool canBuild;
  59. private Vector3Int lastPosition;
  60. private int lastTemplateIndex;
  61. private int lastRotationIndex;
  62. private void CanBuild()
  63. {
  64. Vector3Int pos = buildingSystem.GetMouseBuildingPosition();
  65. int templateIndex = buildingSystem.currentBuildingTemplate;
  66. int rotationIndex = buildingSystem.currentRotationIndex;
  67. if(lastPosition != pos || lastTemplateIndex != templateIndex || lastRotationIndex != rotationIndex)
  68. {
  69. //Debug.Log("Position changed, updating blueprint materials");
  70. canBuild = buildingSystem.CanPlaceBuilding(pos, templateIndex, rotationIndex);
  71. lastPosition = pos;
  72. lastTemplateIndex = templateIndex;
  73. lastRotationIndex = rotationIndex;
  74. ChangeMaterials();
  75. }
  76. }
  77. private void ChangeMaterials()
  78. {
  79. MeshRenderer[] renderers = currentBlueprint.GetComponentsInChildren<MeshRenderer>(false);
  80. //Debug.Log($"Changing materials in {renderers.Length} renderers");
  81. for (int i = 0; i < renderers.Length; i++)
  82. {
  83. Material[] materials = renderers[i].materials;
  84. //Debug.Log($"Found {renderers[i].materials.Length} materials in render {i}");
  85. for (int a = 0; a < materials.Length; a++)
  86. {
  87. if(materials[a].name.Contains("Main"))
  88. {
  89. if(canBuild) materials[a] = blueprintMain;
  90. else materials[a] = invalidMain;
  91. }
  92. else if(materials[a].name.Contains("Detail"))
  93. {
  94. if(canBuild) materials[a] = blueprintDetail;
  95. else materials[a] = invalidDetail;
  96. }
  97. }
  98. renderers[i].materials = materials;
  99. }
  100. }
  101. }
  102. }