Skip to content

Instantly share code, notes, and snippets.

@GibTreaty
Last active October 5, 2016 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GibTreaty/f33bc50ea7f28311d10e957277a0e390 to your computer and use it in GitHub Desktop.
Save GibTreaty/f33bc50ea7f28311d10e957277a0e390 to your computer and use it in GitHub Desktop.
A bunch of enums and helper function to aid in finding directions (originally used in a voxel system) (Requires the VectorI.cs)
using System;
using UnityEngine;
public enum Direction {
Left = 0,
Right = 1,
Bottom = 2, Down = 2,
Top = 3, Up = 3,
Backward = 4, Back = 4,
Forward = 5, Front = 5
}
[Flags]
public enum DirectionFlag {
None = 0,
Left = 1,
Right = 1 << 1,
Bottom = 1 << 2, Down = 1 << 2,
Top = 1 << 3, Up = 1 << 3,
Backward = 1 << 4, Back = 1 << 4,
Forward = 1 << 5, Front = 1 << 5
}
[Flags]
public enum CubeDirectionFlag {
None = 0,
LeftDownBack = 1 << 1,
DownBack = 1 << 2,
RightDownBack = 1 << 3,
LeftBack = 1 << 4,
Back = 1 << 5,
RightBack = 1 << 6,
LeftUpBack = 1 << 7,
UpBack = 1 << 8,
RightUpBack = 1 << 9,
LeftDown = 1 << 10,
Down = 1 << 11,
RightDown = 1 << 12,
Left = 1 << 13,
Right = 1 << 14,
LeftUp = 1 << 15,
Up = 1 << 16,
RightUp = 1 << 17,
LeftDownForward = 1 << 18,
DownForward = 1 << 19,
RightDownForward = 1 << 20,
LeftForward = 1 << 21,
Forward = 1 << 22,
RightForward = 1 << 23,
LeftUpForward = 1 << 24,
UpForward = 1 << 25,
RightUpForward = 1 << 26,
All = 134217726,
Edges = DownBack | LeftBack | RightBack | UpBack | DownForward | LeftForward | RightForward | UpForward,
Corners = LeftDownBack | RightDownBack | LeftUpBack | RightUpBack | LeftDownForward | RightDownForward | LeftUpForward | RightUpForward,
EdgesAndCorners = Edges | Corners,
Faces = Left | Right | Down | Up | Back | Forward
}
public static class DirectionHelper {
public const int DirectionCount = 26;
public const int FaceCount = 6;
public static VectorI3[] cubeDirections = new VectorI3[] {
new VectorI3(-1, -1, -1),
new VectorI3(0, -1, -1),
new VectorI3(1, -1, -1),
new VectorI3(-1, 0, -1),
new VectorI3(0, 0, -1),
new VectorI3(1, 0, -1),
new VectorI3(-1, 1, -1),
new VectorI3(0, 1, -1),
new VectorI3(1, 1, -1),
new VectorI3(-1, -1, 0),
new VectorI3(0, -1, 0),
new VectorI3(1, -1, 0),
new VectorI3(-1, 0, 0),
new VectorI3(1, 0, 0),
new VectorI3(-1, 1, 0),
new VectorI3(0, 1, 0),
new VectorI3(1, 1, 0),
new VectorI3(-1, -1, 1),
new VectorI3(0, -1, 1),
new VectorI3(1, -1, 1),
new VectorI3(-1, 0, 1),
new VectorI3(0, 0, 1),
new VectorI3(1, 0, 1),
new VectorI3(-1, 1, 1),
new VectorI3(0, 1, 1),
new VectorI3(1, 1, 1)
};
public static int BitRoot(int n) {
return (int)Mathf.Log(n, 2);
}
public static int BitCount(int n) {
int count = 0;
while(n != 0) {
count++;
n &= (n - 1);
}
return count;
}
public static VectorI3 Direction(this Direction direction) {
return MathHelper.direction[(int)direction];
}
public static Direction Direction(int direction) {
return (Direction)Enum.Parse(typeof(Direction), direction.ToString());
}
public static DirectionFlag GetDirectionFlag(int direction) {
return (DirectionFlag)Enum.Parse(typeof(DirectionFlag), direction.ToString());
}
public static bool HasDirection(this DirectionFlag direction, DirectionFlag value) {
return (direction & value) != DirectionFlag.None;
}
public static bool HasDirection(this CubeDirectionFlag direction, CubeDirectionFlag value) {
return (direction & value) != CubeDirectionFlag.None;
}
public static CubeDirectionFlag FaceToCubeDirection(this int face) {
switch(face) {
default: return CubeDirectionFlag.None;
case 0: return CubeDirectionFlag.Left;
case 1: return CubeDirectionFlag.Right;
case 2: return CubeDirectionFlag.Down;
case 3: return CubeDirectionFlag.Up;
case 4: return CubeDirectionFlag.Back;
case 5: return CubeDirectionFlag.Forward;
}
}
public static CubeDirectionFlag ToCubeDirection(this int face) {
switch(face) {
default: return CubeDirectionFlag.None;
case 0: return CubeDirectionFlag.LeftDownBack;
case 1: return CubeDirectionFlag.DownBack;
case 2: return CubeDirectionFlag.RightDownBack;
case 3: return CubeDirectionFlag.LeftBack;
case 4: return CubeDirectionFlag.Back;
case 5: return CubeDirectionFlag.RightBack;
case 6: return CubeDirectionFlag.LeftUpBack;
case 7: return CubeDirectionFlag.UpBack;
case 8: return CubeDirectionFlag.RightUpBack;
case 9: return CubeDirectionFlag.LeftDown;
case 10: return CubeDirectionFlag.Down;
case 11: return CubeDirectionFlag.RightDown;
case 12: return CubeDirectionFlag.Left;
case 13: return CubeDirectionFlag.Right;
case 14: return CubeDirectionFlag.LeftUp;
case 15: return CubeDirectionFlag.Up;
case 16: return CubeDirectionFlag.RightUp;
case 17: return CubeDirectionFlag.LeftDownForward;
case 18: return CubeDirectionFlag.DownForward;
case 19: return CubeDirectionFlag.RightDownForward;
case 20: return CubeDirectionFlag.LeftForward;
case 21: return CubeDirectionFlag.Forward;
case 22: return CubeDirectionFlag.RightForward;
case 23: return CubeDirectionFlag.LeftUpForward;
case 24: return CubeDirectionFlag.UpForward;
case 25: return CubeDirectionFlag.RightUpForward;
}
}
public static CubeDirectionFlag ToCubeDirection(this VectorI3 vector) {
if(vector == VectorI3.zero) return CubeDirectionFlag.None;
string enumName = "";
if(vector.x != 0) enumName += Mathf.Sign(vector.x) > 0 ? "Left" : "Right";
if(vector.y != 0) enumName += Mathf.Sign(vector.y) > 0 ? "Up" : "Down";
if(vector.z != 0) enumName += Mathf.Sign(vector.z) > 0 ? "Forward" : "Back";
return (CubeDirectionFlag)Enum.Parse(typeof(CubeDirectionFlag), enumName);
}
public static string ToCubeDirectionString(this VectorI3 vector) {
if(vector == VectorI3.zero) return "";
string enumName = "";
if(vector.x != 0) enumName += Mathf.Sign(vector.x) > 0 ? "Left" : "Right";
if(vector.y != 0) enumName += Mathf.Sign(vector.y) > 0 ? "Up" : "Down";
if(vector.z != 0) enumName += Mathf.Sign(vector.z) > 0 ? "Forward" : "Back";
return enumName;
}
public static VectorI3 ToDirectionVector(this CubeDirectionFlag direction) {
return BitCount((int)direction) == 1 ? cubeDirections[BitRoot((int)direction) - 1] : VectorI3.zero;
}
public static VectorI3 ToDirectionVector(this int index) {
return cubeDirections[index];
}
public static VectorI3 ToFace(this int index) {
switch(index) {
default: return VectorI3.zero;
case 0: return VectorI3.left;
case 1: return VectorI3.right;
case 2: return VectorI3.down;
case 3: return VectorI3.up;
case 4: return VectorI3.back;
case 5: return VectorI3.forward;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment