public EnvironmentCube GetRandomAdjacentCube(EnvironmentCube source, bool unoccupied = false) { EnvironmentCube adjacent = null; GridCoordinates coordinates = source._coordinates; int option = Random.Range(0, 4); while (adjacent == null) { switch (option) { case 0: coordinates.z += 1; // north break; case 1: coordinates.z -= 1; // south break; case 2: coordinates.x += 1; // east break; case 3: coordinates.x -= 1; // west break; } if (AreValidCoordinates(coordinates)) { adjacent = _grid[coordinates.x, coordinates.z]; } if (adjacent == null || (unoccupied && adjacent.IsOccupied())) { coordinates = source._coordinates; if (++option > 3) { option = 0; } } } return(adjacent); }
public EnvironmentCube GetRandomCubeOfType(EnvironmentCube.CubeType type, bool unoccupied = false) { EnvironmentCube randomCube = null; while (randomCube == null) { randomCube = GetRandomCube(); bool isUnoccupied = !randomCube.IsOccupied(); if (randomCube.cubeType == type && (!unoccupied || (unoccupied && isUnoccupied))) { break; } else { randomCube = null; } } return(randomCube); }
private EnvironmentCube GetAdjacentCube(EnvironmentCube cube, Axis axis, bool unoccupied = false, int distance = 1) { EnvironmentCube adjacent = null; while (adjacent == null) { GridCoordinates coordinates = cube.GetCoordinates(); if (axis == Axis.East) { coordinates.x += distance; } else if (axis == Axis.West) { coordinates.x -= distance; } else if (axis == Axis.North) { coordinates.z += distance; } else if (axis == Axis.South) { coordinates.z -= distance; } else if (axis == Axis.NorthEast) { coordinates.x += distance; coordinates.z += distance; } else if (axis == Axis.NorthWest) { coordinates.x -= distance; coordinates.z += distance; } else if (axis == Axis.SouthEast) { coordinates.x += distance; coordinates.z -= distance; } else if (axis == Axis.SouthWest) { coordinates.x -= distance; coordinates.z -= distance; } if (AreValidCoordinates(coordinates)) { adjacent = _grid[coordinates.x, coordinates.z]; } if (adjacent != null && unoccupied && adjacent.IsOccupied()) { adjacent = null; } if (adjacent == null) { distance--; } } return(adjacent); }