Created
June 16, 2019 02:35
-
-
Save dshook/ea62c52c03e4a540517214ed03766388 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Linq; | |
using UnityEngine; | |
public class HexPerimeter | |
{ | |
//Convert the cell perim to a list of world coords on the hex border around the cells | |
public static List<Vector3> GetLinePositions(List<HexCell> cellPerimeter){ | |
var ret = new List<Vector3>(); | |
HexDirection prevDir; | |
HexDirection nextDir; | |
HexCell firstHexCell = cellPerimeter[0]; | |
HexCell lastHexCell = cellPerimeter[cellPerimeter.Count - 1]; | |
HexCell nextHexCell = null; | |
for(int p = 0; p < cellPerimeter.Count; p++){ | |
var cell = cellPerimeter[p]; | |
if(p > 0){ | |
lastHexCell = cellPerimeter[p - 1]; | |
} | |
prevDir = HexDirectionExtensions.CoordDirection(cell.coordinates, lastHexCell.coordinates); | |
if (p + 1 < cellPerimeter.Count) { | |
nextHexCell = cellPerimeter[p + 1]; | |
} | |
else { | |
nextHexCell = firstHexCell; | |
} | |
nextDir = HexDirectionExtensions.CoordDirection(cell.coordinates, nextHexCell.coordinates); | |
var bendType = GetBendType(prevDir, nextDir); | |
var currentCorner = startingCornerMap[prevDir]; | |
for(int i = 0; i < (int)bendType; i++){ | |
AddDir(ret, cell, currentCorner); | |
currentCorner = currentCorner.Next(); | |
} | |
} | |
return ret; | |
} | |
// the value signify how many vertices are used on the line going around the corner | |
enum HexBendType{ | |
Inner = 1, | |
Straight = 2, | |
Outer = 3, | |
Acute = 4, | |
Uturn = 5 | |
} | |
//Maintaining the clockwise motion starting from the top right most cell, translate the previous/next tile pair into a bend direction | |
static HexBendType GetBendType(HexDirection prevDir, HexDirection nextDir){ | |
if(prevDir == nextDir.Opposite()){ | |
return HexBendType.Straight; | |
} | |
if(prevDir == nextDir){ | |
return HexBendType.Uturn; | |
} | |
//Not sure what the axiom is here that makes this works but it does! | |
if(nextDir == prevDir.Next2()){ | |
return HexBendType.Inner; | |
} | |
if(nextDir == prevDir.Previous2()){ | |
return HexBendType.Outer; | |
} | |
if(nextDir == prevDir.Previous()){ | |
return HexBendType.Acute; | |
} | |
//Shouldn't hit here | |
Debug.LogWarning("Unknown bend type " + prevDir + " " + nextDir); | |
return HexBendType.Straight; | |
} | |
//For the perimeter line what hex corner do we need to start adding on | |
static Dictionary<HexDirection, HexCornerDirection> startingCornerMap = new Dictionary<HexDirection, HexCornerDirection>(){ | |
{HexDirection.NE, HexCornerDirection.SE}, | |
{HexDirection.E, HexCornerDirection.S}, | |
{HexDirection.SE, HexCornerDirection.SW}, | |
{HexDirection.SW, HexCornerDirection.NW}, | |
{HexDirection.W, HexCornerDirection.N}, | |
{HexDirection.NW, HexCornerDirection.NE}, | |
}; | |
static void AddDir(List<Vector3> ret, HexCell cell, HexCornerDirection dir){ | |
ret.Add( HexCoordinates.corners[dir] + cell.gameObject.transform.position ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment