public static Dir8 Rotate(this Dir8 dir, int n) { if (!dir.Validate()) { throw new ArgumentOutOfRangeException(nameof(dir), dir, "Invalid enum value."); } if (dir == Dir8.None) { return(Dir8.None); } return((Dir8)(((int)dir + n) & (DIR8_COUNT - 1))); }
public static Dir8 AddAngles(Dir8 dir1, Dir8 dir2) { // dir1 is validated by Dir8.Rotate if (!dir2.Validate()) { throw new ArgumentOutOfRangeException(nameof(dir2), dir2, "Invalid enum value."); } if (dir1 == Dir8.None || dir2 == Dir8.None) { return(Dir8.None); } return(dir1.Rotate((int)dir2)); }
public static bool IsDirBlocked(Loc loc, Dir8 dir, LocTest checkBlock, LocTest checkDiagBlock, int distance) { if (checkBlock == null) { throw new ArgumentNullException(nameof(checkBlock)); } if (checkDiagBlock == null) { throw new ArgumentNullException(nameof(checkDiagBlock)); } if (!dir.Validate()) { throw new ArgumentException("Invalid value to check."); } else if (dir == Dir8.None) { return(false); } for (int ii = 0; ii < distance; ii++) { if (dir.IsDiagonal()) { dir.Separate(out DirH horiz, out DirV vert); Loc diagLoc = loc + horiz.GetLoc(); if (checkDiagBlock(diagLoc)) { return(true); } diagLoc = loc + vert.GetLoc(); if (checkDiagBlock(diagLoc)) { return(true); } } loc += dir.GetLoc(); if (checkBlock(loc)) { return(true); } } return(false); }
/// <summary> /// Given a first entity at 0,0 and its facing direction, determines if the second entity is in front of the first entity. /// </summary> /// <param name="testLoc">Second entity location</param> /// <param name="dir">First entity direction</param> /// <param name="range">-1 for infinite range.</param> /// <returns></returns> public static bool InFront(Loc testLoc, Dir8 dir, int range) { if (!dir.Validate()) { throw new ArgumentException("Invalid value to convert."); } if (testLoc == Loc.Zero) { return(true); } int foundRange = testLoc.Dist8(); if (range >= 0 && foundRange > range) { return(false); } return(dir.GetLoc() * foundRange == testLoc); }
public static void Separate(this Dir8 dir, out DirH horiz, out DirV vert) { if (!dir.Validate()) { throw new ArgumentOutOfRangeException(nameof(dir), dir, "Invalid enum value."); } switch (dir) { case Dir8.Down: case Dir8.DownLeft: case Dir8.DownRight: vert = DirV.Down; break; case Dir8.Up: case Dir8.UpLeft: case Dir8.UpRight: vert = DirV.Up; break; default: vert = DirV.None; break; } switch (dir) { case Dir8.Left: case Dir8.UpLeft: case Dir8.DownLeft: horiz = DirH.Left; break; case Dir8.Right: case Dir8.UpRight: case Dir8.DownRight: horiz = DirH.Right; break; default: horiz = DirH.None; break; } }