public static LocRay4 GetWallDir(Loc loc, Grid.LocTest checkBlock, Grid.LocTest checkGround) { if (checkBlock == null) { throw new ArgumentNullException(nameof(checkBlock)); } if (checkGround == null) { throw new ArgumentNullException(nameof(checkGround)); } // check the four directions Dir4 chosenDir = Dir4.None; // ensure that there is only one direction where it is unblocked foreach (Dir4 dir in DirExt.VALID_DIR4) { Loc newLoc = loc + dir.GetLoc(); if (checkGround(newLoc)) { if (chosenDir != Dir4.None) { return(new LocRay4(loc)); } else { chosenDir = dir.Reverse(); } } else if (!checkBlock(newLoc)) { // all four directions must be valid ground, or valid block return(new LocRay4(loc)); } } if (chosenDir == Dir4.None) { return(new LocRay4(loc)); } // then check to make sure that the left and right diagonal of this direction are also valid blocked Loc lLoc = loc + DirExt.AddAngles(chosenDir.ToDir8(), Dir8.DownLeft).GetLoc(); if (!checkBlock(lLoc)) { return(new LocRay4(loc)); } Loc rLoc = loc + DirExt.AddAngles(chosenDir.ToDir8(), Dir8.DownRight).GetLoc(); if (!checkBlock(rLoc)) { return(new LocRay4(loc)); } return(new LocRay4(loc, chosenDir)); }
public void ToDir8(Dir4 dir, Dir8 expected, bool exception = false) { if (exception) { Assert.Throws <ArgumentOutOfRangeException>(() => { dir.ToDir8(); }); } else { Assert.That(dir.ToDir8(), Is.EqualTo(expected)); } }