VoxelBuildingSystem.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. public static VoxelBuildingSystem instance;
  15. public Vector3Int chunkSize = new Vector3Int(16, 16, 16);
  16. public Vector3Int chunkCount = new Vector3Int(3, 3, 3);
  17. public Vector3 voxelSize = new Vector3(1f, 1f, 1f);
  18. public LayerMask layerMask;
  19. public List<BuildingTemplate> buildingTemplates = new List<BuildingTemplate>();
  20. public bool debugBlocks = false;
  21. public GameObject blockVisualPrefab;
  22. public Transform blockContainer;
  23. [ReadOnly] public int currentBuildingTemplate;
  24. [ReadOnly] public int currentRotationIndex;
  25. [ReadOnly] public VoxelBuildingTool tool = VoxelBuildingTool.Build;
  26. [ReadOnly] public List<Vector3Int> rotationList = new List<Vector3Int>();
  27. public ChunkSystem<VoxelBuildData> chunkSystem;
  28. [HideInInspector] public Dictionary<Vector3Int, BuildingBehaviour> buildings = new Dictionary<Vector3Int, BuildingBehaviour>();
  29. [HideInInspector] public List<BuildingBehaviour> buildingList = new List<BuildingBehaviour>();
  30. private Vector3 pointerPosition;
  31. public void CreateVoxelSystem()
  32. {
  33. SetupRotationList();
  34. chunkSystem = new ChunkSystem<VoxelBuildData>(chunkSize, false);
  35. for (int x = 0; x < chunkCount.x; x++) {
  36. for (int z = 0; z < chunkCount.z; z++) {
  37. for (int y = 0; y < chunkCount.y; y++) {
  38. Vector3Int chunkPos = new Vector3Int(x * chunkSize.x, y * chunkSize.y, z * chunkSize.z);
  39. chunkSystem.CreateChunk(chunkPos, (Chunk<VoxelBuildData> c, Vector3Int pos) => new VoxelBuildData(pos, -1));
  40. }
  41. }
  42. }
  43. }
  44. void Start() => CreateVoxelSystem();
  45. void Update()
  46. {
  47. pointerPosition = Utils.GetMouseWorldPosition(layerMask.value);
  48. if(!MouseInputUIBlocker.BlockedByUI)
  49. {
  50. switch (tool)
  51. {
  52. case VoxelBuildingTool.Build:
  53. if(Input.GetKeyDown(KeyCode.R)) RotateBuilding();
  54. if(Input.GetMouseButtonDown(0)) PlaceBuilding(pointerPosition);
  55. break;
  56. case VoxelBuildingTool.Remove:
  57. if(Input.GetMouseButtonDown(0)) RemoveBuilding(pointerPosition);
  58. break;
  59. default:
  60. break;
  61. }
  62. }
  63. if(Input.GetKeyDown(KeyCode.Alpha1)) CycleBuildingTemplate();
  64. }
  65. public void RotateBuilding()
  66. {
  67. if(currentRotationIndex < rotationList.Count - 1) currentRotationIndex += 1;
  68. else currentRotationIndex = 0;
  69. }
  70. public void PlaceBuilding(Vector3 pos)
  71. {
  72. Vector3Int buildingPos = GetMouseBuildingPosition();
  73. Vector3Int voxelPos = GetMouseVoxelPosition();
  74. Vector3Int buildingRotation = rotationList[currentRotationIndex];
  75. if(!CanPlaceBuilding(voxelPos, currentBuildingTemplate, currentRotationIndex, true)) return;
  76. Quaternion rotation = Quaternion.Euler(buildingRotation.x, buildingRotation.y, buildingRotation.z);
  77. Transform newBuilding = Instantiate(buildingTemplates[currentBuildingTemplate].prefab, voxelPos, rotation, this.transform);
  78. BuildingBehaviour buildingBehaviour = newBuilding.GetComponent<BuildingBehaviour>();
  79. if(buildingBehaviour == null)
  80. {
  81. Debug.LogError($"Building prefab has no BuildingBehaviour component! Cannot place building", newBuilding);
  82. Destroy(newBuilding);
  83. return;
  84. }
  85. buildingBehaviour.origin = voxelPos;
  86. buildings.Add(voxelPos, buildingBehaviour);
  87. buildingList.Add(buildingBehaviour);
  88. List<Vector3Int> voxelPositions = GetBuildingVoxelList(voxelPos, currentBuildingTemplate, currentRotationIndex);
  89. for (int i = 0; i < voxelPositions.Count; i++)
  90. {
  91. VoxelBuildData block = chunkSystem.GetBlock(voxelPositions[i]);
  92. block.buildingIndex = buildingList.Count - 1;
  93. block.buildingTemplateIndex = currentBuildingTemplate;
  94. chunkSystem.SetBlock(voxelPositions[i], block);
  95. if(debugBlocks) Instantiate(blockVisualPrefab, voxelPositions[i], new Quaternion(), blockContainer);
  96. }
  97. }
  98. public bool CanPlaceBuilding(Vector3Int voxelPos, int buildingTemplateIndex, int buildingRotationIndex = 0, bool debug = false)
  99. {
  100. List<Vector3Int> voxelPositions = GetBuildingVoxelList(voxelPos, buildingTemplateIndex, buildingRotationIndex);
  101. for (int i = 0; i < voxelPositions.Count; i++)
  102. {
  103. VoxelBuildData block = chunkSystem.GetBlock(voxelPositions[i]);
  104. if(block.buildingIndex != -1)
  105. {
  106. if(debug) Debug.Log("Cannot place building, place is occupied.");
  107. return false;
  108. }
  109. }
  110. return true;
  111. }
  112. public void RemoveBuilding(Vector3 pos) {}
  113. public Vector3Int FindVoxelPosition(Vector3 pos)
  114. {
  115. return new Vector3Int(Mathf.FloorToInt(pos.x/voxelSize.x), Mathf.FloorToInt(pos.y/voxelSize.y), Mathf.FloorToInt(pos.z/voxelSize.z));
  116. }
  117. public Vector3 GetMouseWorldSnappedPosition() => (Vector3)FindVoxelPosition(pointerPosition);
  118. public Vector3Int GetMouseVoxelPosition() => FindVoxelPosition(pointerPosition);
  119. public Vector3Int GetMouseBuildingPosition()
  120. {
  121. Vector3Int voxelPos = FindVoxelPosition(pointerPosition);
  122. Vector3Int rotationOffset = GetRotationOffset(currentRotationIndex, buildingTemplates[currentBuildingTemplate].size);
  123. return voxelPos + rotationOffset;
  124. }
  125. public void CycleBuildingTemplate()
  126. {
  127. if(currentBuildingTemplate < buildingTemplates.Count - 1) currentBuildingTemplate += 1;
  128. else currentBuildingTemplate = 0;
  129. GenericEvents.Trigger("OnBuildingTemplateChanged");
  130. //Debug.Log($"Changing current building template to {currentBuildingTemplate}");
  131. }
  132. public List<Vector3Int> GetBuildingVoxelList(Vector3Int offset, int buildingTemplateId, int buildingRotationIndex = 0)
  133. {
  134. BuildingTemplate template = buildingTemplates[buildingTemplateId];
  135. List<Vector3Int> positionList = new List<Vector3Int>();
  136. Vector3Int rotationOffset = GetRotationOffset(buildingRotationIndex, template.size);
  137. switch (buildingRotationIndex)
  138. {
  139. default:
  140. case 0:
  141. case 2:
  142. for(int x = 0; x < template.size.x; x++)
  143. {
  144. for (int y = 0; y < template.size.y; y++)
  145. {
  146. for (int z = 0; z < template.size.z; z++)
  147. {
  148. Vector3Int pos = new Vector3Int(offset.x + rotationOffset.x + x, offset.y + rotationOffset.y + y, offset.z + rotationOffset.z + z);
  149. positionList.Add(pos);
  150. }
  151. }
  152. }
  153. break;
  154. case 1:
  155. case 3:
  156. for(int x = 0; x < template.size.z; x++)
  157. {
  158. for (int y = 0; y < template.size.y; y++)
  159. {
  160. for (int z = 0; z < template.size.x; z++)
  161. {
  162. Vector3Int pos = new Vector3Int(offset.x + rotationOffset.x + x, offset.y + rotationOffset.y + y, offset.z + rotationOffset.z + z);
  163. positionList.Add(pos);
  164. }
  165. }
  166. }
  167. break;
  168. }
  169. //Debug.Log($"{positionList.Count} voxel positions found for building \"{buildingTemplates[buildingTemplateId].title}\"");
  170. return positionList;
  171. }
  172. private void SetupRotationList()
  173. {
  174. rotationList = new List<Vector3Int>
  175. {
  176. new Vector3Int(0, 0, 0),
  177. new Vector3Int(0, 90, 0),
  178. new Vector3Int(0, 180, 0),
  179. new Vector3Int(0, 270, 0)
  180. };
  181. }
  182. public Vector3Int GetRotationOffset(int rotationIndex, Vector3Int size)
  183. {
  184. switch (rotationIndex)
  185. {
  186. default:
  187. case 0: return new Vector3Int(0, 0, 0);
  188. case 1: return new Vector3Int(0, 0, -size.x);
  189. case 2: return new Vector3Int(-size.x, 0, -size.z);
  190. case 3: return new Vector3Int(-size.z, 0, 0);
  191. }
  192. }
  193. public Vector3Int GetConnectorPositionOffset(VoxelConnectorData connector, Vector3Int size, int rotationIndex)
  194. {
  195. Vector3Int pos = connector.position;
  196. switch (rotationIndex)
  197. {
  198. default:
  199. case 0: return connector.position;
  200. case 1: return new Vector3Int(pos.z, pos.y, (pos.x * -1) + size.x - 1);
  201. case 2: return new Vector3Int((pos.x * -1) + size.x - 1, pos.y, (pos.z * -1) + size.z - 1);
  202. case 3: return new Vector3Int((pos.z * -1) + size.z - 1, pos.y, pos.x);
  203. }
  204. // switch (connector.direction)
  205. // {
  206. // default:
  207. // case TargetDirection.Back: return connector.position;
  208. // case TargetDirection.Left: return new Vector3Int(pos.z, pos.y, (pos.x * -1) + size.x - 1);
  209. // case TargetDirection.Front: return new Vector3Int((pos.x * -1) + size.x - 1, pos.y, (pos.z * -1) + size.z - 1);
  210. // case TargetDirection.Right: return new Vector3Int((pos.z * -1) + size.z - 1, pos.y, pos.x);
  211. // case TargetDirection.Up: return new Vector3Int(pos.x, (pos.y * -1) + size.y - 1, pos.z);
  212. // case TargetDirection.Down: return new Vector3Int(pos.x, pos.y * -1, pos.z);
  213. // }
  214. }
  215. public Vector3Int GetPositionOffsetFromDirection(int rotationIndex, VoxelConnectorData connector)
  216. {
  217. switch (rotationIndex)
  218. {
  219. default:
  220. case 0:
  221. switch (connector.direction)
  222. {
  223. default:
  224. case TargetDirection.Down: return new Vector3Int(0, 0, -1);
  225. case TargetDirection.Left: return new Vector3Int(-1, 0, 0);
  226. case TargetDirection.Up: return new Vector3Int(0, 0, 1);
  227. case TargetDirection.Right: return new Vector3Int(1, 0, 0);
  228. }
  229. case 1:
  230. switch (connector.direction)
  231. {
  232. default:
  233. case TargetDirection.Down: return new Vector3Int(-1, 0, 0);
  234. case TargetDirection.Left: return new Vector3Int(0, 0, 1);
  235. case TargetDirection.Up: return new Vector3Int(1, 0, 0);
  236. case TargetDirection.Right: return new Vector3Int(0, 0, -1);
  237. }
  238. case 2:
  239. switch (connector.direction)
  240. {
  241. default:
  242. case TargetDirection.Down: return new Vector3Int(0, 0, 1);
  243. case TargetDirection.Left: return new Vector3Int(1, 0, 0);
  244. case TargetDirection.Up: return new Vector3Int(0, 0, -1);
  245. case TargetDirection.Right: return new Vector3Int(-1, 0, 0);
  246. }
  247. case 3:
  248. switch (connector.direction)
  249. {
  250. default:
  251. case TargetDirection.Down: return new Vector3Int(1, 0, 0);
  252. case TargetDirection.Left: return new Vector3Int(0, 0, -1);
  253. case TargetDirection.Up: return new Vector3Int(-1, 0, 0);
  254. case TargetDirection.Right: return new Vector3Int(0, 0, 1);
  255. }
  256. }
  257. }
  258. }
  259. public enum VoxelBuildingTool
  260. {
  261. None,
  262. Build,
  263. Remove
  264. }
  265. }