Skip to content

Commit

Permalink
refactoring triangulation algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanstudioroot committed May 12, 2020
1 parent 20e21ff commit 5ca4749
Show file tree
Hide file tree
Showing 35 changed files with 1,752 additions and 1,152 deletions.
8 changes: 4 additions & 4 deletions Assets/rootgen-unitycsharp/Runtime/Camera/HexGridCamera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -419,12 +419,12 @@ float cellOuterRadius
HexagonPoint.GetOuterToInnerRadius(cellOuterRadius) * 2f;

float xMax =
(grid.CellCountX - 0.5f) * innerDiameter;
(grid.WidthInCells - 0.5f) * innerDiameter;

position.x = Mathf.Clamp(position.x, 0f, xMax);

float zMax =
(grid.CellCountZ - 1) * (1.5f * cellOuterRadius);
(grid.HeightInCells - 1) * (1.5f * cellOuterRadius);

position.z = Mathf.Clamp(position.z, 0f, zMax);

Expand All @@ -439,7 +439,7 @@ float cellOuterRadius
float innerDiameter =
HexagonPoint.GetOuterToInnerRadius(cellOuterRadius) * 2f;

float width = grid.CellCountX * innerDiameter;
float width = grid.WidthInCells * innerDiameter;

while (position.x < 0f) {
position.x += width;
Expand All @@ -450,7 +450,7 @@ float cellOuterRadius
}

float zMax =
(grid.CellCountZ - 1) * (1.5f * cellOuterRadius);
(grid.HeightInCells - 1) * (1.5f * cellOuterRadius);

position.z = Mathf.Clamp(position.z, 0f, zMax);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ float cellOuterRadius
if (
nearCell.HasWalls != farCell.HasWalls &&
!nearCell.IsUnderwater && !farCell.IsUnderwater &&
nearCell.GetEdgeType(farCell) != EdgeType.Cliff
nearCell.GetEdgeType(farCell) != ElevationEdgeType.Cliff
) {
AddWallSegment(
near.vertex1,
Expand Down Expand Up @@ -422,10 +422,10 @@ float cellOuterRadius
}

bool hasLeftWall = !leftCell.IsUnderwater &&
pivotCell.GetEdgeType(leftCell) != EdgeType.Cliff;
pivotCell.GetEdgeType(leftCell) != ElevationEdgeType.Cliff;

bool hasRightWall = !rightCell.IsUnderwater &&
pivotCell.GetEdgeType(rightCell) != EdgeType.Cliff;
pivotCell.GetEdgeType(rightCell) != ElevationEdgeType.Cliff;

if (hasLeftWall) {
if (hasRightWall) {
Expand Down
12 changes: 12 additions & 0 deletions Assets/rootgen-unitycsharp/Runtime/Core/CellData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using UnityEngine;

public struct CellData {
public HexCoordinates Coordinates { get; set; }
public int Index { get; set; }
public int ColumnIndex { get; set; }
public CellShaderData CellShaderData { get; set; }
public bool IsExplorable { get; set; }
public Vector3 WorldPosition { get; set; }
public HexGridChunk HexGridChunk { get; set; }

}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

265 changes: 265 additions & 0 deletions Assets/rootgen-unitycsharp/Runtime/Core/DenseArray.cs

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions Assets/rootgen-unitycsharp/Runtime/Core/DenseArray.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

198 changes: 198 additions & 0 deletions Assets/rootgen-unitycsharp/Runtime/Core/HexAdjacencyGraph.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
using QuikGraph;
using System.Collections.Generic;

public class HexAdjacencyGraph : AdjacencyGraph<HexCell, HexEdge> {

/// <summary>
/// Returns true if the given cell in the graph has a river.
/// </summary>
/// <param name="cell">
/// The cell which is the subject of the query.
/// </param>
/// <returns>
/// A boolean value representing whether the query cell has a river.
/// </returns>
public bool CellHasRiver(HexCell cell) {
IEnumerable<HexEdge> edges;

if (TryGetOutEdges(cell, out edges)) {
foreach(HexEdge edge in edges) {
if (edge.HasRiver)
return true;
}
}

return false;
}

/// <summary>
/// Returns true if the given cell in the graph has a road.
/// </summary>
/// <param name="cell">
/// The cell which is the subject of the query.
/// </param>
/// <returns>
/// A boolean value representing whether the query cell has a road.
/// </returns>
public bool CellHasRoad(HexCell cell) {
IEnumerable<HexEdge> edges;

if (TryGetOutEdges(cell, out edges)) {
foreach(HexEdge edge in edges) {
if (edge.HasRoad)
return true;
}
}

return false;
}

/// <summary>
/// Returns true if the given cell in the graph contains
/// the termination of a river.
/// </summary>
/// <param name="cell">
/// The cell which is the subject of the query.
/// </param>
/// <returns>
/// A boolean value representing whether the query cell
/// contains the termination of a river.
/// </returns>
public bool CellHasRiverTerminus(
HexCell cell
) {
IEnumerable<HexEdge> edges;

if (TryGetOutEdges(cell, out edges)) {
int count = 0;
foreach(HexEdge edge in edges) {
if (edge.HasRiver)
count++;

if (count > 2) {
return true;
}
}
}

return false;
}

public bool CellHasIncomingRiver(
HexCell cell
) {
IEnumerable<HexEdge> edges;

if (TryGetOutEdges(cell, out edges)) {
foreach(HexEdge edge in edges) {
if (edge.HasRoad)
return true;
}
}

return false;
}

public IEnumerable<HexEdge> TryGetOutEdgesInDirection(
HexCell cell,
HexDirection direction
) {
IEnumerable<HexEdge> edges;
List<HexEdge> result = new List<HexEdge>();

if (!TryGetOutEdges(cell, out edges)) {

foreach(HexEdge edge in edges) {
if (edge.Direction == direction)
result.Add(edge);
}
}
else {
throw new System.ArgumentException(
"HexCell did not exist in graph or had no out edges."
);
}

return result;
}

public HexEdge GetHexDirectionOppositeEdge(
HexEdge edge
) {
IEnumerable<HexEdge> edges;

if (!TryGetOutEdges(edge.Source, out edges)) {
foreach(HexEdge currentEdge in edges) {
if (currentEdge.Direction == edge.Direction.Opposite()) {
return currentEdge;
}
}
}

return null;
}

public HexEdge GetHexDirectionNextEdge(
HexEdge edge
) {
IEnumerable<HexEdge> edges;

if (!TryGetOutEdges(edge.Source, out edges)) {
foreach(HexEdge currentEdge in edges) {
if (currentEdge.Direction == edge.Direction.Next()) {
return currentEdge;
}
}
}

return null;
}

public HexEdge GetHexDirectionPreviousEdge(
HexEdge edge
) {
IEnumerable<HexEdge> edges;

if (!TryGetOutEdges(edge.Source, out edges)) {
foreach(HexEdge currentEdge in edges) {
if (currentEdge.Direction == edge.Direction.Previous()) {
return currentEdge;
}
}
}

return null;
}

public HexEdge GetHexDirectionNext2Edge(
HexEdge edge
) {
IEnumerable<HexEdge> edges;

if (!TryGetOutEdges(edge.Source, out edges)) {
foreach(HexEdge currentEdge in edges) {
if (currentEdge.Direction == edge.Direction.Next2()) {
return currentEdge;
}
}
}

return null;
}

public HexEdge GetHexDirectionPrevious2Edge(
HexEdge edge
) {
IEnumerable<HexEdge> edges;

if (!TryGetOutEdges(edge.Source, out edges)) {
foreach(HexEdge currentEdge in edges) {
if (currentEdge.Direction == edge.Direction.Previous2()) {
return currentEdge;
}
}
}

return null;
}
}
11 changes: 11 additions & 0 deletions Assets/rootgen-unitycsharp/Runtime/Core/HexAdjacencyGraph.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5ca4749

Please sign in to comment.