private Vector2Int?ComputeValidDrive() { // Have we moved at all? if (lastDelta != Vector2Int.zero) { // Can we keep moving forward? if (movementController.CanDoMove(movementController.gridElement.position, lastDelta)) { return(lastDelta); } // Nope. What about backward? if (movementController.CanDoMove(movementController.gridElement.position, -lastDelta)) { return(-lastDelta); } } // Bummer. Let's find first unoccupied spot. for (int x = -1; x <= 1; x++) { for (int y = -1; y <= 1; y++) { if (x == 0 && y == 0) { continue; } // Can we move to that spot? if (movementController.CanDoMove(movementController.gridElement.position, new Vector2Int(x, y))) { return(new Vector2Int(x, y)); } } } // Can't move anywhere... Can't build right now. return(null); }
private static List <Vector2Int> AvailableNeighbors(MovementController movingElement, Vector2Int point) { List <Vector2Int> neighbors = new List <Vector2Int>(); for (int x = -1; x <= 1; x++) { for (int y = -1; y <= 1; y++) { if (x == 0 && y == 0) { continue; } Vector2Int drive = new Vector2Int(x, y); if (movingElement.CanDoMove(point, drive)) { neighbors.Add(point + drive); } } } return(neighbors); }