|
@@ -0,0 +1,87 @@
|
|
|
+using System.Collections;
|
|
|
+using System.Collections.Generic;
|
|
|
+using UnityEngine;
|
|
|
+using KairoEngine.Chunks;
|
|
|
+using KairoEngine.Core;
|
|
|
+using KairoEngine.UI;
|
|
|
+using Utils = KairoEngine.Utility.Utilities;
|
|
|
+using Sirenix.OdinInspector;
|
|
|
+
|
|
|
+namespace KairoEngine.VoxelBuildingSystem
|
|
|
+{
|
|
|
+ [HideMonoScript]
|
|
|
+ public class VoxelBuildingSystem : MonoBehaviour
|
|
|
+ {
|
|
|
+ public static VoxelBuildingSystem instance;
|
|
|
+ public Vector3Int chunkSize = new Vector3Int(16, 16, 16);
|
|
|
+ public Vector3Int chunkCount = new Vector3Int(3, 3, 3);
|
|
|
+ public Vector3 voxelSize = new Vector3(1f, 1f, 1f);
|
|
|
+ public LayerMask layerMask;
|
|
|
+ public List<BuildingTemplate> buildingTemplates = new List<BuildingTemplate>();
|
|
|
+
|
|
|
+ public VoxelBuildingTool tool = VoxelBuildingTool.Build;
|
|
|
+ private ChunkSystem<VoxelBuildData> chunkSystem;
|
|
|
+ [HideInInspector] public Dictionary<Vector3Int, BuildingBehaviour> buildings = new Dictionary<Vector3Int, BuildingBehaviour>();
|
|
|
+ [HideInInspector] public int currentBuildingTemplate;
|
|
|
+
|
|
|
+ private Vector3 pointerPosition;
|
|
|
+
|
|
|
+ public void CreateVoxelSystem()
|
|
|
+ {
|
|
|
+ chunkSystem = new ChunkSystem<VoxelBuildData>(chunkSize, false);
|
|
|
+ for (int x = 0; x < chunkCount.x; x++) {
|
|
|
+ for (int z = 0; z < chunkCount.z; z++) {
|
|
|
+ for (int y = 0; y < chunkCount.y; y++) {
|
|
|
+ Vector3Int chunkPos = new Vector3Int(x * chunkSize.x, y * chunkSize.y, z * chunkSize.z);
|
|
|
+ chunkSystem.CreateChunk(chunkPos, (Chunk<VoxelBuildData> c, Vector3Int pos) => new VoxelBuildData(pos, -1));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ void Update()
|
|
|
+ {
|
|
|
+ pointerPosition = Utils.GetMouseWorldPosition(layerMask.value);
|
|
|
+ if(!MouseInputUIBlocker.BlockedByUI)
|
|
|
+ {
|
|
|
+ switch (tool)
|
|
|
+ {
|
|
|
+ case VoxelBuildingTool.Build:
|
|
|
+ if(Input.GetKeyDown(KeyCode.R)) RotateBuilding();
|
|
|
+ if(Input.GetMouseButtonDown(0)) PlaceBuilding(pointerPosition);
|
|
|
+ break;
|
|
|
+ case VoxelBuildingTool.Remove:
|
|
|
+ if(Input.GetMouseButtonDown(0)) RemoveBuilding(pointerPosition);
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void RotateBuilding() {}
|
|
|
+
|
|
|
+ public void PlaceBuilding(Vector3 pos)
|
|
|
+ {
|
|
|
+ Vector3 voxelPos = GetMouseWorldSnappedPosition();
|
|
|
+ Transform newBuilding = Instantiate(buildingTemplates[currentBuildingTemplate].prefab, voxelPos, this.transform.rotation, this.transform);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void RemoveBuilding(Vector3 pos) {}
|
|
|
+
|
|
|
+ public Vector3Int FindVoxelPosition(Vector3 pos)
|
|
|
+ {
|
|
|
+ return new Vector3Int(Mathf.FloorToInt(pos.x/voxelSize.x), Mathf.FloorToInt(pos.y/voxelSize.y), Mathf.FloorToInt(pos.z/voxelSize.z));
|
|
|
+ }
|
|
|
+
|
|
|
+ public Vector3 GetMouseWorldSnappedPosition() => (Vector3)FindVoxelPosition(pointerPosition);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public enum VoxelBuildingTool
|
|
|
+ {
|
|
|
+ None,
|
|
|
+ Build,
|
|
|
+ Remove
|
|
|
+ }
|
|
|
+}
|