VoxelBuildingSystem.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using KairoEngine.Chunks;
  5. using KairoEngine.Core;
  6. using KairoEngine.UI;
  7. using Utils = KairoEngine.Utility.Utilities;
  8. using Sirenix.OdinInspector;
  9. namespace KairoEngine.VoxelBuildingSystem
  10. {
  11. [HideMonoScript]
  12. public class VoxelBuildingSystem : MonoBehaviour
  13. {
  14. #region Variables
  15. public static VoxelBuildingSystem instance;
  16. public Vector3Int chunkSize = new Vector3Int(16, 16, 16);
  17. public Vector3Int chunkCount = new Vector3Int(3, 3, 3);
  18. public Vector3 voxelSize = new Vector3(1f, 1f, 1f);
  19. public LayerMask layerMask;
  20. public List<BuildingTemplate> buildingTemplates = new List<BuildingTemplate>();
  21. public bool debugBlocks = false;
  22. public GameObject blockVisualPrefab;
  23. public Transform blockContainer;
  24. [ReadOnly] public int currentBuildingTemplate;
  25. [ReadOnly] public int currentRotationIndex;
  26. [ReadOnly] public VoxelBuildingTool tool = VoxelBuildingTool.Build;
  27. [ReadOnly] public List<Vector3Int> rotationList = new List<Vector3Int>();
  28. public ChunkSystem<VoxelBuildData> chunkSystem;
  29. [HideInInspector] public Dictionary<Vector3Int, BuildingBehaviour> buildings = new Dictionary<Vector3Int, BuildingBehaviour>();
  30. [HideInInspector] public List<BuildingBehaviour> buildingList = new List<BuildingBehaviour>();
  31. private Vector3 pointerPosition;
  32. #endregion
  33. #region Lifecycle
  34. public void CreateVoxelSystem()
  35. {
  36. SetupRotationList();
  37. chunkSystem = new ChunkSystem<VoxelBuildData>(chunkSize, false);
  38. for (int x = 0; x < chunkCount.x; x++) {
  39. for (int z = 0; z < chunkCount.z; z++) {
  40. for (int y = 0; y < chunkCount.y; y++) {
  41. Vector3Int chunkPos = new Vector3Int(x * chunkSize.x, y * chunkSize.y, z * chunkSize.z);
  42. chunkSystem.CreateChunk(chunkPos, (Chunk<VoxelBuildData> c, Vector3Int pos) => new VoxelBuildData(pos, -1));
  43. }
  44. }
  45. }
  46. }
  47. void Start() => CreateVoxelSystem();
  48. void Update()
  49. {
  50. pointerPosition = Utils.GetMouseWorldPosition(layerMask.value);
  51. if(!MouseInputUIBlocker.BlockedByUI)
  52. {
  53. switch (tool)
  54. {
  55. case VoxelBuildingTool.Build:
  56. if(Input.GetKeyDown(KeyCode.R)) RotateBuilding();
  57. if(Input.GetMouseButtonDown(0)) PlaceBuilding(pointerPosition);
  58. break;
  59. case VoxelBuildingTool.Remove:
  60. if(Input.GetMouseButtonDown(0)) RemoveBuilding(pointerPosition);
  61. break;
  62. default:
  63. break;
  64. }
  65. }
  66. if(Input.GetKeyDown(KeyCode.Alpha1)) CycleBuildingTemplate();
  67. }
  68. #endregion
  69. #region Actions
  70. public void RotateBuilding()
  71. {
  72. if(currentRotationIndex < rotationList.Count - 1) currentRotationIndex += 1;
  73. else currentRotationIndex = 0;
  74. }
  75. public void PlaceBuilding(Vector3 pos)
  76. {
  77. Vector3Int buildingPos = GetMouseBuildingPosition();
  78. Vector3Int voxelPos = GetMouseVoxelPosition();
  79. Vector3Int buildingRotation = rotationList[currentRotationIndex];
  80. if(!CanPlaceBuilding(voxelPos, currentBuildingTemplate, currentRotationIndex, true)) return;
  81. Quaternion rotation = Quaternion.Euler(buildingRotation.x, buildingRotation.y, buildingRotation.z);
  82. Transform newBuilding = Instantiate(buildingTemplates[currentBuildingTemplate].prefab, voxelPos, rotation, this.transform);
  83. BuildingBehaviour buildingBehaviour = newBuilding.GetComponent<BuildingBehaviour>();
  84. if(buildingBehaviour == null)
  85. {
  86. Debug.LogError($"Building prefab has no BuildingBehaviour component! Cannot place building", newBuilding);
  87. Destroy(newBuilding);
  88. return;
  89. }
  90. buildingBehaviour.origin = voxelPos;
  91. buildings.Add(voxelPos, buildingBehaviour);
  92. buildingList.Add(buildingBehaviour);
  93. List<Vector3Int> voxelPositions = GetBuildingVoxelList(voxelPos, currentBuildingTemplate, currentRotationIndex);
  94. for (int i = 0; i < voxelPositions.Count; i++)
  95. {
  96. VoxelBuildData block = chunkSystem.GetBlock(voxelPositions[i]);
  97. block.buildingIndex = buildingList.Count - 1;
  98. block.buildingTemplateIndex = currentBuildingTemplate;
  99. chunkSystem.SetBlock(voxelPositions[i], block);
  100. if(debugBlocks) Instantiate(blockVisualPrefab, voxelPositions[i], new Quaternion(), blockContainer);
  101. }
  102. }
  103. public void RemoveBuilding(Vector3 pos) {}
  104. public void CycleBuildingTemplate()
  105. {
  106. if(currentBuildingTemplate < buildingTemplates.Count - 1) currentBuildingTemplate += 1;
  107. else currentBuildingTemplate = 0;
  108. GenericEvents.Trigger("OnBuildingTemplateChanged");
  109. //Debug.Log($"Changing current building template to {currentBuildingTemplate}");
  110. }
  111. #endregion
  112. #region Queries
  113. public bool CanPlaceBuilding(Vector3Int voxelPos, int buildingTemplateIndex, int buildingRotationIndex = 0, bool debug = false)
  114. {
  115. List<Vector3Int> voxelPositions = GetBuildingVoxelList(voxelPos, buildingTemplateIndex, buildingRotationIndex);
  116. for (int i = 0; i < voxelPositions.Count; i++)
  117. {
  118. VoxelBuildData block = chunkSystem.GetBlock(voxelPositions[i]);
  119. if(block.buildingIndex != -1)
  120. {
  121. if(debug) Debug.Log("Cannot place building, place is occupied.");
  122. return false;
  123. }
  124. }
  125. return true;
  126. }
  127. public Vector3Int FindVoxelPosition(Vector3 pos)
  128. {
  129. return new Vector3Int(Mathf.FloorToInt(pos.x/voxelSize.x), Mathf.FloorToInt(pos.y/voxelSize.y), Mathf.FloorToInt(pos.z/voxelSize.z));
  130. }
  131. public Vector3 GetMouseWorldSnappedPosition() => (Vector3)FindVoxelPosition(pointerPosition);
  132. public Vector3Int GetMouseVoxelPosition() => FindVoxelPosition(pointerPosition);
  133. public Vector3Int GetMouseBuildingPosition()
  134. {
  135. Vector3Int voxelPos = FindVoxelPosition(pointerPosition);
  136. Vector3Int rotationOffset = GetRotationOffset(currentRotationIndex, buildingTemplates[currentBuildingTemplate].size);
  137. return voxelPos + rotationOffset;
  138. }
  139. public List<Vector3Int> GetBuildingVoxelList(Vector3Int offset, int buildingTemplateId, int buildingRotationIndex = 0)
  140. {
  141. BuildingTemplate template = buildingTemplates[buildingTemplateId];
  142. List<Vector3Int> positionList = new List<Vector3Int>();
  143. Vector3Int rotationOffset = GetRotationOffset(buildingRotationIndex, template.size);
  144. switch (buildingRotationIndex)
  145. {
  146. default:
  147. case 0:
  148. case 2:
  149. for(int x = 0; x < template.size.x; x++)
  150. {
  151. for (int y = 0; y < template.size.y; y++)
  152. {
  153. for (int z = 0; z < template.size.z; z++)
  154. {
  155. Vector3Int pos = new Vector3Int(offset.x + rotationOffset.x + x, offset.y + rotationOffset.y + y, offset.z + rotationOffset.z + z);
  156. positionList.Add(pos);
  157. }
  158. }
  159. }
  160. break;
  161. case 1:
  162. case 3:
  163. for(int x = 0; x < template.size.z; x++)
  164. {
  165. for (int y = 0; y < template.size.y; y++)
  166. {
  167. for (int z = 0; z < template.size.x; z++)
  168. {
  169. Vector3Int pos = new Vector3Int(offset.x + rotationOffset.x + x, offset.y + rotationOffset.y + y, offset.z + rotationOffset.z + z);
  170. positionList.Add(pos);
  171. }
  172. }
  173. }
  174. break;
  175. }
  176. //Debug.Log($"{positionList.Count} voxel positions found for building \"{buildingTemplates[buildingTemplateId].title}\"");
  177. return positionList;
  178. }
  179. #endregion
  180. #region Rotation
  181. private void SetupRotationList()
  182. {
  183. rotationList = new List<Vector3Int>
  184. {
  185. new Vector3Int(0, 0, 0),
  186. new Vector3Int(0, 90, 0),
  187. new Vector3Int(0, 180, 0),
  188. new Vector3Int(0, 270, 0)
  189. };
  190. }
  191. public Vector3Int GetRotationOffset(int rotationIndex, Vector3Int size)
  192. {
  193. switch (rotationIndex)
  194. {
  195. default:
  196. case 0: return new Vector3Int(0, 0, 0);
  197. case 1: return new Vector3Int(0, 0, -size.x);
  198. case 2: return new Vector3Int(-size.x, 0, -size.z);
  199. case 3: return new Vector3Int(-size.z, 0, 0);
  200. }
  201. }
  202. #endregion
  203. #region Connectors
  204. /// <summary>Get an offset value for calculating a connectors position, accounting for the buildings rotation and size.
  205. /// For now it only works in the Y axis with rotation indexes ranging from 0 to 3.</summary>
  206. /// <param name="connector">A reference for the Voxel Building Connector</param>
  207. /// <param name="size">The size of the building that the connector belongs to</param>
  208. /// <param name="rotationIndex">The rotation index for the building</param>
  209. /// <returns>A position offset that can be added to a connector position to get its coordinates</returns>
  210. // TODO: Add rotations to the X and Z axis
  211. public Vector3Int GetConnectorPositionOffset(VoxelConnectorData connector, Vector3Int size, int rotationIndex)
  212. {
  213. Vector3Int pos = connector.position;
  214. switch (rotationIndex)
  215. {
  216. default:
  217. case 0: return connector.position;
  218. case 1: return new Vector3Int(pos.z, pos.y, (pos.x * -1) + size.x - 1);
  219. case 2: return new Vector3Int((pos.x * -1) + size.x - 1, pos.y, (pos.z * -1) + size.z - 1);
  220. case 3: return new Vector3Int((pos.z * -1) + size.z - 1, pos.y, pos.x);
  221. }
  222. }
  223. /// <summary>Returns an Offset from a connector's voxel to its target voxel accounting for the building rotation and the direction of the connector.
  224. /// For now it only works in the Y axis with rotation indexes ranging from 0 to 3.</summary>
  225. /// <param name="rotationIndex">The rotation index for calculating the offset </param>
  226. /// <param name="connector">A reference for the Voxel Building Connector</param>
  227. /// <returns>A position offset</returns>
  228. // TODO: Add rotations to the X and Z axis
  229. public Vector3Int GetPositionOffsetFromDirection(int rotationIndex, VoxelConnectorData connector)
  230. {
  231. switch (rotationIndex)
  232. {
  233. default:
  234. case 0:
  235. switch (connector.direction)
  236. {
  237. default:
  238. case TargetDirection.Back: return new Vector3Int(0, 0, -1);
  239. case TargetDirection.Left: return new Vector3Int(-1, 0, 0);
  240. case TargetDirection.Front: return new Vector3Int(0, 0, 1);
  241. case TargetDirection.Right: return new Vector3Int(1, 0, 0);
  242. case TargetDirection.Up: return new Vector3Int(0, 1, 0);
  243. case TargetDirection.Down: return new Vector3Int(0, -1, 0);
  244. }
  245. case 1:
  246. switch (connector.direction)
  247. {
  248. default:
  249. case TargetDirection.Back: return new Vector3Int(-1, 0, 0);
  250. case TargetDirection.Left: return new Vector3Int(0, 0, 1);
  251. case TargetDirection.Front: return new Vector3Int(1, 0, 0);
  252. case TargetDirection.Right: return new Vector3Int(0, 0, -1);
  253. case TargetDirection.Up: return new Vector3Int(0, 1, 0);
  254. case TargetDirection.Down: return new Vector3Int(0, -1, 0);
  255. }
  256. case 2:
  257. switch (connector.direction)
  258. {
  259. default:
  260. case TargetDirection.Back: return new Vector3Int(0, 0, 1);
  261. case TargetDirection.Left: return new Vector3Int(1, 0, 0);
  262. case TargetDirection.Front: return new Vector3Int(0, 0, -1);
  263. case TargetDirection.Right: return new Vector3Int(-1, 0, 0);
  264. case TargetDirection.Up: return new Vector3Int(0, 1, 0);
  265. case TargetDirection.Down: return new Vector3Int(0, -1, 0);
  266. }
  267. case 3:
  268. switch (connector.direction)
  269. {
  270. default:
  271. case TargetDirection.Back: return new Vector3Int(1, 0, 0);
  272. case TargetDirection.Left: return new Vector3Int(0, 0, -1);
  273. case TargetDirection.Front: return new Vector3Int(-1, 0, 0);
  274. case TargetDirection.Right: return new Vector3Int(0, 0, 1);
  275. case TargetDirection.Up: return new Vector3Int(0, 1, 0);
  276. case TargetDirection.Down: return new Vector3Int(0, -1, 0);
  277. }
  278. }
  279. }
  280. #endregion
  281. }
  282. public enum VoxelBuildingTool
  283. {
  284. None,
  285. Build,
  286. Remove
  287. }
  288. }