Browse Source

Refactored VoxelBuilding System script

James Peret 2 years ago
parent
commit
6bd87f8b0b
1 changed files with 63 additions and 35 deletions
  1. 63 35
      Runtime/VoxelBuildingSystem.cs

+ 63 - 35
Runtime/VoxelBuildingSystem.cs

@@ -12,30 +12,29 @@ namespace KairoEngine.VoxelBuildingSystem
     [HideMonoScript]
     public class VoxelBuildingSystem : MonoBehaviour
     {
+        #region Variables
+
         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 bool debugBlocks = false;
         public GameObject blockVisualPrefab;
         public Transform blockContainer;
-
         [ReadOnly] public int currentBuildingTemplate;
         [ReadOnly] public int currentRotationIndex;
-
         [ReadOnly] public VoxelBuildingTool tool = VoxelBuildingTool.Build;
-
         [ReadOnly] public List<Vector3Int> rotationList = new List<Vector3Int>();
         public ChunkSystem<VoxelBuildData> chunkSystem;
         [HideInInspector] public Dictionary<Vector3Int, BuildingBehaviour> buildings = new Dictionary<Vector3Int, BuildingBehaviour>();
         [HideInInspector] public List<BuildingBehaviour> buildingList = new List<BuildingBehaviour>();
-        
-
         private Vector3 pointerPosition;
 
+        #endregion
+
+        #region Lifecycle
         public void CreateVoxelSystem()
         {
             SetupRotationList();
@@ -73,6 +72,10 @@ namespace KairoEngine.VoxelBuildingSystem
             if(Input.GetKeyDown(KeyCode.Alpha1)) CycleBuildingTemplate();
         }
 
+        #endregion
+
+        #region Actions
+
         public void RotateBuilding() 
         {
             if(currentRotationIndex < rotationList.Count - 1) currentRotationIndex += 1;
@@ -106,7 +109,21 @@ namespace KairoEngine.VoxelBuildingSystem
                 chunkSystem.SetBlock(voxelPositions[i], block);
                 if(debugBlocks) Instantiate(blockVisualPrefab, voxelPositions[i], new Quaternion(), blockContainer);
             }
-        } 
+        }
+
+        public void RemoveBuilding(Vector3 pos) {}
+
+        public void CycleBuildingTemplate()
+        {
+            if(currentBuildingTemplate < buildingTemplates.Count - 1) currentBuildingTemplate += 1;
+            else currentBuildingTemplate = 0;
+            GenericEvents.Trigger("OnBuildingTemplateChanged");
+            //Debug.Log($"Changing current building template to {currentBuildingTemplate}");
+        }
+
+        #endregion
+
+        #region Queries
 
         public bool CanPlaceBuilding(Vector3Int voxelPos, int buildingTemplateIndex, int buildingRotationIndex = 0, bool debug = false)
         {
@@ -123,8 +140,6 @@ namespace KairoEngine.VoxelBuildingSystem
             return true;
         }
 
-        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));
@@ -140,14 +155,6 @@ namespace KairoEngine.VoxelBuildingSystem
             return voxelPos + rotationOffset;
         }
 
-        public void CycleBuildingTemplate()
-        {
-            if(currentBuildingTemplate < buildingTemplates.Count - 1) currentBuildingTemplate += 1;
-            else currentBuildingTemplate = 0;
-            GenericEvents.Trigger("OnBuildingTemplateChanged");
-            //Debug.Log($"Changing current building template to {currentBuildingTemplate}");
-        }
-
         public List<Vector3Int> GetBuildingVoxelList(Vector3Int offset, int buildingTemplateId, int buildingRotationIndex = 0) 
         { 
             BuildingTemplate template = buildingTemplates[buildingTemplateId];
@@ -190,6 +197,10 @@ namespace KairoEngine.VoxelBuildingSystem
             return positionList;
         }
 
+        #endregion
+
+        #region Rotation
+
         private void SetupRotationList()
         {
             rotationList = new List<Vector3Int>
@@ -213,6 +224,17 @@ namespace KairoEngine.VoxelBuildingSystem
             }
         }
         
+        #endregion
+
+        #region Connectors
+
+        /// <summary>Get an offset value for calculating a connectors position, accounting for the buildings rotation and size. 
+        /// For now it only works in the Y axis with rotation indexes ranging from 0 to 3.</summary>
+        /// <param name="connector">A reference for the Voxel Building Connector</param>
+        /// <param name="size">The size of the building that the connector belongs to</param>
+        /// <param name="rotationIndex">The rotation index for the building</param>
+        /// <returns>A position offset that can be added to a connector position to get its coordinates</returns>
+        // TODO: Add rotations to the X and Z axis
         public Vector3Int GetConnectorPositionOffset(VoxelConnectorData connector, Vector3Int size, int rotationIndex)
         {
             Vector3Int pos = connector.position;
@@ -224,18 +246,14 @@ namespace KairoEngine.VoxelBuildingSystem
                 case 2:  return new Vector3Int((pos.x * -1) + size.x - 1, pos.y, (pos.z * -1) + size.z - 1);
                 case 3:  return new Vector3Int((pos.z * -1) + size.z - 1, pos.y, pos.x);
             }
-            // switch (connector.direction)
-            // {
-            //     default:
-            //     case TargetDirection.Back:  return connector.position;
-            //     case TargetDirection.Left:  return new Vector3Int(pos.z, pos.y, (pos.x * -1) + size.x - 1);
-            //     case TargetDirection.Front:    return new Vector3Int((pos.x * -1) + size.x - 1, pos.y, (pos.z * -1) + size.z - 1);
-            //     case TargetDirection.Right: return new Vector3Int((pos.z * -1) + size.z - 1, pos.y, pos.x);
-            //     case TargetDirection.Up:  return new Vector3Int(pos.x, (pos.y * -1) + size.y - 1, pos.z);
-            //     case TargetDirection.Down:  return new Vector3Int(pos.x, pos.y * -1, pos.z);
-            // }
         }
 
+        /// <summary>Returns an Offset from a connector's voxel to its target voxel accounting for the building rotation and the direction of the connector.
+        /// For now it only works in the Y axis with rotation indexes ranging from 0 to 3.</summary>
+        /// <param name="rotationIndex">The rotation index for calculating the offset </param>
+        /// <param name="connector">A reference for the Voxel Building Connector</param>
+        /// <returns>A position offset</returns>
+        // TODO: Add rotations to the X and Z axis
         public Vector3Int GetPositionOffsetFromDirection(int rotationIndex, VoxelConnectorData connector)
         {
             switch (rotationIndex)
@@ -245,40 +263,50 @@ namespace KairoEngine.VoxelBuildingSystem
                     switch (connector.direction)
                     {
                         default:
-                        case TargetDirection.Down:  return new Vector3Int(0, 0, -1);
+                        case TargetDirection.Back:  return new Vector3Int(0, 0, -1);
                         case TargetDirection.Left:  return new Vector3Int(-1, 0, 0);
-                        case TargetDirection.Up:    return new Vector3Int(0, 0, 1);
+                        case TargetDirection.Front:    return new Vector3Int(0, 0, 1);
                         case TargetDirection.Right: return new Vector3Int(1, 0, 0);
+                        case TargetDirection.Up: return new Vector3Int(0, 1, 0);
+                        case TargetDirection.Down: return new Vector3Int(0, -1, 0);
                     }
                 case 1:  
                     switch (connector.direction)
                     {
                         default:
-                        case TargetDirection.Down:  return new Vector3Int(-1, 0, 0);
+                        case TargetDirection.Back:  return new Vector3Int(-1, 0, 0);
                         case TargetDirection.Left:  return new Vector3Int(0, 0, 1);
-                        case TargetDirection.Up:    return new Vector3Int(1, 0, 0);
+                        case TargetDirection.Front:    return new Vector3Int(1, 0, 0);
                         case TargetDirection.Right: return new Vector3Int(0, 0, -1);
+                        case TargetDirection.Up: return new Vector3Int(0, 1, 0);
+                        case TargetDirection.Down: return new Vector3Int(0, -1, 0);
                     }
                 case 2:    
                     switch (connector.direction)
                     {
                         default:
-                        case TargetDirection.Down:  return new Vector3Int(0, 0, 1);
+                        case TargetDirection.Back:  return new Vector3Int(0, 0, 1);
                         case TargetDirection.Left:  return new Vector3Int(1, 0, 0);
-                        case TargetDirection.Up:    return new Vector3Int(0, 0, -1);
+                        case TargetDirection.Front:    return new Vector3Int(0, 0, -1);
                         case TargetDirection.Right: return new Vector3Int(-1, 0, 0);
+                        case TargetDirection.Up: return new Vector3Int(0, 1, 0);
+                        case TargetDirection.Down: return new Vector3Int(0, -1, 0);
                     }
                 case 3: 
                     switch (connector.direction)
                     {
                         default:
-                        case TargetDirection.Down:  return new Vector3Int(1, 0, 0);
+                        case TargetDirection.Back:  return new Vector3Int(1, 0, 0);
                         case TargetDirection.Left:  return new Vector3Int(0, 0, -1);
-                        case TargetDirection.Up:    return new Vector3Int(-1, 0, 0);
+                        case TargetDirection.Front:    return new Vector3Int(-1, 0, 0);
                         case TargetDirection.Right: return new Vector3Int(0, 0, 1);
+                        case TargetDirection.Up: return new Vector3Int(0, 1, 0);
+                        case TargetDirection.Down: return new Vector3Int(0, -1, 0);
                     }
             }
         }
+        
+        #endregion
 
     }