diff --git a/Assets/Scripts/Terrain/HexGrid.cs b/Assets/Scripts/Terrain/HexGrid.cs index d35dfe23..ffdc50ed 100644 --- a/Assets/Scripts/Terrain/HexGrid.cs +++ b/Assets/Scripts/Terrain/HexGrid.cs @@ -29,6 +29,9 @@ public class HexGrid : MonoBehaviour [Header("Terrain Data")] private Transform[] chunks = default; + [SerializeField] + private Vector2[] chunkBuildable = default; + [HideInInspector] public HexCell[] cells; @@ -61,6 +64,9 @@ public class HexGrid : MonoBehaviour [SerializeField] private HexCellType[] cellTypes = default; + [SerializeField] + private HexCellType[] buildableCellTypes = default; + private HexCellType tallestCellType = default; [Header("Savekey")] @@ -236,7 +242,23 @@ private void CreateCell(int x, int z, int i) cell.transform.SetParent(chunks[chunkX + chunkZ * chunkCountX]); - SetTypeForCell(cell); + //No buildable chunk set, assumes every chunk should be buildable + if (chunkBuildable.Length == 0) { + SetTypeForCell(cell); + return; + } + + foreach (Vector2 chunk in chunkBuildable) + { + if (chunk.x == chunkX && chunk.y == chunkZ) + { + SetTypeForCell(cell, true); + return; + } + } + + SetTypeForCell(cell, false); + } /// @@ -250,6 +272,26 @@ private void SetTypeForCell(HexCell cell) cell.CellType = cellTypes[cellTypeIndex]; } + /// + /// Adjust a cells transform and material information based off the elevation integer and if the chunk is allowed to be built on. + /// + /// The target cell + private void SetTypeForCell(HexCell cell, bool isBuildable) + { + float noiseValue = HexMetrics.SampleNoise(cell.transform.localPosition, noise).y; // will be between 0.0 and 1.0 (both inclusive) + + if (!isBuildable) + { + int cellTypeIndex = Mathf.FloorToInt(noiseValue * cellTypes.Length * 0.99f); // will be between 0 and the last index of `cellTypes` + cell.CellType = cellTypes[cellTypeIndex]; + return; + } + + int buildableCellTypeIndex = Mathf.FloorToInt(noiseValue * buildableCellTypes.Length * 0.99f); // will be between 0 and the last index of `BuildableCellTypes` + cell.CellType = cellTypes[buildableCellTypeIndex]; + + } + /// /// An array containing the cells on the edge of the map (not including the "mountain" edge) that enemies can spawn on. /// @@ -327,6 +369,26 @@ private void PlaceSceneryOnPlayArea(GameObject[] sceneryObjects, bool occupying) || cell.IsOccupied) continue; + if (chunkBuildable.Length > 0) + { + int chunkX = cell.coordinates.X / HexMetrics.CHUNK_SIZE_X; + int chunkZ = cell.coordinates.Z / HexMetrics.CHUNK_SIZE_Z; + foreach (Vector2 chunk in chunkBuildable) + { + if (chunk.x == chunkX && chunk.y == chunkZ) + { + if (occupying){ continue; } + + int sceneryIndexBuildable = Random.Range(0, sceneryObjects.Length); + GameObject sceneryObjectBuildable = cell.InstantiatePrefabOnCell(sceneryObjects[sceneryIndexBuildable]); + float yRotationBuildable = Random.Range(0f, 360f); + sceneryObjectBuildable.transform.Rotate(0, yRotationBuildable, 0); + cell.OccupyingObject = null; + continue; + } + } + } + int sceneryIndex = Random.Range(0, sceneryObjects.Length); GameObject sceneryObject = cell.InstantiatePrefabOnCell(sceneryObjects[sceneryIndex]); float yRotation = Random.Range(0f, 360f);