VoxelBuildingSystem.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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(buildingPos, currentBuildingTemplate, currentRotationIndex, true)) return;
  81. Quaternion rotation = Quaternion.Euler(buildingRotation.x, buildingRotation.y, buildingRotation.z);
  82. Transform newBuilding = Instantiate(buildingTemplates[currentBuildingTemplate].prefab, buildingPos, 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 = buildingPos;
  91. buildings.Add(buildingPos, buildingBehaviour);
  92. buildingList.Add(buildingBehaviour);
  93. List<Vector3Int> voxelPositions = GetBuildingVoxelList(buildingPos, 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. return EvaluateRuleset(voxelPos, buildingRotationIndex, buildingTemplateIndex);
  116. }
  117. public Vector3Int FindVoxelPosition(Vector3 pos)
  118. {
  119. return new Vector3Int(Mathf.FloorToInt(pos.x/voxelSize.x), Mathf.FloorToInt(pos.y/voxelSize.y), Mathf.FloorToInt(pos.z/voxelSize.z));
  120. }
  121. public Vector3 GetMouseWorldSnappedPosition() => (Vector3)FindVoxelPosition(pointerPosition);
  122. public Vector3Int GetMouseVoxelPosition() => FindVoxelPosition(pointerPosition);
  123. public Vector3Int GetMouseBuildingPosition()
  124. {
  125. Vector3Int voxelPos = FindVoxelPosition(pointerPosition);
  126. Vector3Int rotationOffset = GetRotationOffset(currentRotationIndex, buildingTemplates[currentBuildingTemplate].size);
  127. return voxelPos - rotationOffset;
  128. }
  129. public List<Vector3Int> GetBuildingVoxelList(Vector3Int offset, int buildingTemplateId, int buildingRotationIndex = 0)
  130. {
  131. BuildingTemplate template = buildingTemplates[buildingTemplateId];
  132. List<Vector3Int> positionList = new List<Vector3Int>();
  133. Vector3Int rotationOffset = GetRotationOffset(buildingRotationIndex, template.size);
  134. switch (buildingRotationIndex)
  135. {
  136. default:
  137. case 0:
  138. case 2:
  139. for(int x = 0; x < template.size.x; x++)
  140. {
  141. for (int y = 0; y < template.size.y; y++)
  142. {
  143. for (int z = 0; z < template.size.z; z++)
  144. {
  145. Vector3Int pos = new Vector3Int(offset.x + rotationOffset.x + x, offset.y + rotationOffset.y + y, offset.z + rotationOffset.z + z);
  146. positionList.Add(pos);
  147. }
  148. }
  149. }
  150. break;
  151. case 1:
  152. case 3:
  153. for(int x = 0; x < template.size.z; x++)
  154. {
  155. for (int y = 0; y < template.size.y; y++)
  156. {
  157. for (int z = 0; z < template.size.x; z++)
  158. {
  159. Vector3Int pos = new Vector3Int(offset.x + rotationOffset.x + x, offset.y + rotationOffset.y + y, offset.z + rotationOffset.z + z);
  160. positionList.Add(pos);
  161. }
  162. }
  163. }
  164. break;
  165. }
  166. //Debug.Log($"{positionList.Count} voxel positions found for building \"{buildingTemplates[buildingTemplateId].title}\"");
  167. return positionList;
  168. }
  169. #endregion
  170. #region Rotation
  171. private void SetupRotationList()
  172. {
  173. rotationList = new List<Vector3Int>
  174. {
  175. new Vector3Int(0, 0, 0),
  176. new Vector3Int(0, 90, 0),
  177. new Vector3Int(0, 180, 0),
  178. new Vector3Int(0, 270, 0)
  179. };
  180. }
  181. public Vector3Int GetRotationOffset(int rotationIndex, Vector3Int size)
  182. {
  183. switch (rotationIndex)
  184. {
  185. default:
  186. case 0: return new Vector3Int(0, 0, 0);
  187. case 1: return new Vector3Int(0, 0, -size.x);
  188. case 2: return new Vector3Int(-size.x, 0, -size.z);
  189. case 3: return new Vector3Int(-size.z, 0, 0);
  190. }
  191. }
  192. #endregion
  193. #region Connectors
  194. /// <summary>Get an offset value for calculating a connectors position, accounting for the buildings rotation and size.
  195. /// For now it only works in the Y axis with rotation indexes ranging from 0 to 3.</summary>
  196. /// <param name="connector">A reference for the Voxel Building Connector</param>
  197. /// <param name="size">The size of the building that the connector belongs to</param>
  198. /// <param name="rotationIndex">The rotation index for the building</param>
  199. /// <returns>A position offset that can be added to a connector position to get its coordinates</returns>
  200. // TODO: Add rotations to the X and Z axis
  201. public Vector3Int GetConnectorPositionOffset(VoxelConnectorData connector, Vector3Int size, int rotationIndex)
  202. {
  203. Vector3Int pos = connector.position;
  204. switch (rotationIndex)
  205. {
  206. default:
  207. case 0: return connector.position;
  208. case 1: return new Vector3Int(pos.z, pos.y, (pos.x * -1) + size.x - 1);
  209. case 2: return new Vector3Int((pos.x * -1) + size.x - 1, pos.y, (pos.z * -1) + size.z - 1);
  210. case 3: return new Vector3Int((pos.z * -1) + size.z - 1, pos.y, pos.x);
  211. }
  212. }
  213. /// <summary>Returns an Offset from a connector's voxel to its target voxel accounting for the building rotation and the direction of the connector.
  214. /// For now it only works in the Y axis with rotation indexes ranging from 0 to 3.</summary>
  215. /// <param name="rotationIndex">The rotation index for calculating the offset </param>
  216. /// <param name="connector">A reference for the Voxel Building Connector</param>
  217. /// <returns>A position offset</returns>
  218. // TODO: Add rotations to the X and Z axis
  219. public Vector3Int GetPositionOffsetFromDirection(int rotationIndex, VoxelConnectorData connector)
  220. {
  221. switch (rotationIndex)
  222. {
  223. default:
  224. case 0:
  225. switch (connector.direction)
  226. {
  227. default:
  228. case TargetDirection.Back: return new Vector3Int(0, 0, -1);
  229. case TargetDirection.Left: return new Vector3Int(-1, 0, 0);
  230. case TargetDirection.Front: return new Vector3Int(0, 0, 1);
  231. case TargetDirection.Right: return new Vector3Int(1, 0, 0);
  232. case TargetDirection.Up: return new Vector3Int(0, 1, 0);
  233. case TargetDirection.Down: return new Vector3Int(0, -1, 0);
  234. }
  235. case 1:
  236. switch (connector.direction)
  237. {
  238. default:
  239. case TargetDirection.Back: return new Vector3Int(-1, 0, 0);
  240. case TargetDirection.Left: return new Vector3Int(0, 0, 1);
  241. case TargetDirection.Front: return new Vector3Int(1, 0, 0);
  242. case TargetDirection.Right: return new Vector3Int(0, 0, -1);
  243. case TargetDirection.Up: return new Vector3Int(0, 1, 0);
  244. case TargetDirection.Down: return new Vector3Int(0, -1, 0);
  245. }
  246. case 2:
  247. switch (connector.direction)
  248. {
  249. default:
  250. case TargetDirection.Back: return new Vector3Int(0, 0, 1);
  251. case TargetDirection.Left: return new Vector3Int(1, 0, 0);
  252. case TargetDirection.Front: return new Vector3Int(0, 0, -1);
  253. case TargetDirection.Right: return new Vector3Int(-1, 0, 0);
  254. case TargetDirection.Up: return new Vector3Int(0, 1, 0);
  255. case TargetDirection.Down: return new Vector3Int(0, -1, 0);
  256. }
  257. case 3:
  258. switch (connector.direction)
  259. {
  260. default:
  261. case TargetDirection.Back: return new Vector3Int(1, 0, 0);
  262. case TargetDirection.Left: return new Vector3Int(0, 0, -1);
  263. case TargetDirection.Front: return new Vector3Int(-1, 0, 0);
  264. case TargetDirection.Right: return new Vector3Int(0, 0, 1);
  265. case TargetDirection.Up: return new Vector3Int(0, 1, 0);
  266. case TargetDirection.Down: return new Vector3Int(0, -1, 0);
  267. }
  268. }
  269. }
  270. #endregion
  271. #region Rules
  272. public bool EvaluateRuleset(Vector3Int position, int rotationIndex, int templateIndex)
  273. {
  274. List<bool> results = new List<bool>();
  275. BuildingTemplate template = buildingTemplates[currentBuildingTemplate];
  276. for (int i = 0; i < template.rulesets.Count; i++)
  277. {
  278. results.Add(template.rulesets[i].Evaluate(this, templateIndex, position, rotationIndex));
  279. }
  280. if(template.rulesetCondition == ConditionType.AND)
  281. {
  282. foreach (var value in results)
  283. {
  284. if(value == false) return false;
  285. }
  286. return true;
  287. }
  288. else
  289. {
  290. foreach (var value in results)
  291. {
  292. if(value == true) return true;
  293. }
  294. return false;
  295. }
  296. }
  297. #endregion
  298. }
  299. public enum VoxelBuildingTool
  300. {
  301. None,
  302. Build,
  303. Remove
  304. }
  305. }