Created
June 16, 2019 00:47
-
-
Save dshook/83454d3fce98f5d344797a876331f5fa 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 UnityEngine; | |
using System.Collections.Generic; | |
public class HexCell : MonoBehaviour { | |
public HexCoordinates coordinates; | |
HexCell[] neighbors = new HexCell[6]; | |
public HexCell GetNeighbor (HexDirection direction) { | |
return neighbors[(int)direction]; | |
} | |
public void SetNeighbor (HexDirection direction, HexCell cell) { | |
neighbors[(int)direction] = cell; | |
cell.neighbors[(int)direction.Opposite()] = this; | |
} | |
} | |
public class HexCity : MonoBehaviour { | |
public HexCell Cell {get; set;} | |
//How much of a max radius the city influences on the surrounding cells | |
public int AreaOfInfluence{ get{ return 2; } } | |
} | |
public struct HexCoordinates { | |
public const float outerRadius = 0.48f; | |
public const float innerRadius = outerRadius * 0.866025404f; | |
[SerializeField] | |
private int x, z; | |
public static Dictionary<HexCornerDirection, Vector3> corners = new Dictionary<HexCornerDirection, Vector3>(){ | |
{HexCornerDirection.N, new Vector3(0f, outerRadius, 0f)}, | |
{HexCornerDirection.NE, new Vector3(innerRadius, 0.5f * outerRadius, 0f)}, | |
{HexCornerDirection.SE, new Vector3(innerRadius, -0.5f * outerRadius, 0f)}, | |
{HexCornerDirection.S, new Vector3(0f, -outerRadius, 0f)}, | |
{HexCornerDirection.SW, new Vector3(-innerRadius, -0.5f * outerRadius, 0f)}, | |
{HexCornerDirection.NW, new Vector3(-innerRadius, 0.5f * outerRadius, 0f)}, | |
}; | |
} | |
public enum HexDirection { | |
NE, E, SE, SW, W, NW | |
} | |
public enum HexCornerDirection { | |
N, NE, SE, S, SW, NW | |
} | |
public static class HexDirectionExtensions { | |
public static HexDirection Opposite (this HexDirection direction) { | |
return (int)direction < 3 ? (direction + 3) : (direction - 3); | |
} | |
public static HexDirection Previous (this HexDirection direction) { | |
return direction == HexDirection.NE ? HexDirection.NW : (direction - 1); | |
} | |
public static HexDirection Next (this HexDirection direction) { | |
return direction == HexDirection.NW ? HexDirection.NE : (direction + 1); | |
} | |
public static HexDirection Previous2 (this HexDirection direction) { | |
direction -= 2; | |
return direction >= HexDirection.NE ? direction : (direction + 6); | |
} | |
public static HexDirection Next2 (this HexDirection direction) { | |
direction += 2; | |
return direction <= HexDirection.NW ? direction : (direction - 6); | |
} | |
public static HexCoordinates Offset(this HexDirection direction){ | |
switch(direction){ | |
case HexDirection.NE: | |
return new HexCoordinates(0, 1); | |
case HexDirection.E: | |
return new HexCoordinates(1, 0); | |
case HexDirection.SE: | |
return new HexCoordinates(1, -1); | |
case HexDirection.SW: | |
return new HexCoordinates(0, -1); | |
case HexDirection.W: | |
return new HexCoordinates(-1, 0); | |
case HexDirection.NW: | |
return new HexCoordinates(-1, 1); | |
} | |
return new HexCoordinates(0, 0); | |
} | |
//inverse of above and only handles neighboring coordinates | |
public static HexDirection CoordDirection(HexCoordinates first, HexCoordinates second){ | |
var xDiff = second.X - first.X; | |
var zDiff = second.Z - first.Z; | |
if(xDiff == 0 && zDiff == 1){ | |
return HexDirection.NE; | |
} | |
if(xDiff == 1 && zDiff == 0){ | |
return HexDirection.E; | |
} | |
if(xDiff == 1 && zDiff == -1){ | |
return HexDirection.SE; | |
} | |
if(xDiff == 0 && zDiff == -1){ | |
return HexDirection.SW; | |
} | |
if(xDiff == -1 && zDiff == 0){ | |
return HexDirection.W; | |
} | |
if(xDiff == -1 && zDiff == 1){ | |
return HexDirection.NW; | |
} | |
Debug.LogWarning("Getting default coord direction because you're comparing not neighbor cells"); | |
return HexDirection.NE; | |
} | |
public static HexCornerDirection Previous (this HexCornerDirection direction) { | |
return direction == HexCornerDirection.N ? HexCornerDirection.NW : (direction - 1); | |
} | |
public static HexCornerDirection Next (this HexCornerDirection direction) { | |
return direction == HexCornerDirection.NW ? HexCornerDirection.N : (direction + 1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment