Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Terrain generation overhaul #143

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 63 additions & 1 deletion Assets/Scripts/Terrain/HexGrid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public class HexGrid : MonoBehaviour
[Header("Terrain Data")]
private Transform[] chunks = default;

[SerializeField]
private Vector2[] chunkBuildable = default;

[HideInInspector]
public HexCell[] cells;

Expand Down Expand Up @@ -61,6 +64,9 @@ public class HexGrid : MonoBehaviour
[SerializeField]
private HexCellType[] cellTypes = default;

[SerializeField]
private HexCellType[] buildableCellTypes = default;

private HexCellType tallestCellType = default;

[Header("Savekey")]
Expand Down Expand Up @@ -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);

}

/// <summary>
Expand All @@ -250,6 +272,26 @@ private void SetTypeForCell(HexCell cell)
cell.CellType = cellTypes[cellTypeIndex];
}

/// <summary>
/// Adjust a cells transform and material information based off the elevation integer and if the chunk is allowed to be built on.
/// </summary>
/// <param name="cell">The target cell</param>
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];

}

/// <returns>
/// An array containing the cells on the edge of the map (not including the "mountain" edge) that enemies can spawn on.
/// </returns>
Expand Down Expand Up @@ -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);
Expand Down