As request of Simon's comment:
Direction.java
package net.woopa.dungeon.datatypes;
import java.util.Random;
public enum Direction {
NORTH, EAST, SOUTH, WEST;
private static Random rnd = new Random();
static public Direction randomDirection() {
return Direction.values()[rnd.nextInt(4)];
}
// Rotate 90 degrees clockwise
public Direction rotate90() {
return values()[(ordinal() + 1) % 4];
}
// Rotate 180 degrees
public Direction rotate180() {
return values()[(ordinal() + 2) % 4];
}
// Rotate 270 degrees clockwise (90 counterclockwise)
public Direction rotate270() {
return values()[(ordinal() + 3) % 4];
}
public Boolean isHorizontal() {
return this == EAST || this == WEST;
}
public Boolean isVertical() {
return this == NORTH || this == SOUTH;
}
public int dx(int steps) {
if (this == EAST) {
return steps;
}
if (this == WEST) {
return -steps;
}
return 0;
}
public int dy(int steps) {
if (this == NORTH) {
return steps;
}
if (this == SOUTH) {
return -steps;
}
return 0;
}
public int forwards_x(int n) {
if (this == EAST) {
return n + 1;
}
if (this == WEST) {
return n - 1;
}
return n;
}
public int forwards_y(int n) {
if (this == NORTH) {
return n + 1;
}
if (this == SOUTH) {
return n - 1;
}
return n;
}
public int backwards_x(int n) {
if (this == EAST) {
return n - 1;
}
if (this == WEST) {
return n + 1;
}
return n;
}
public int backwards_y(int n) {
if (this == NORTH) {
return n - 1;
}
if (this == SOUTH) {
return n + 1;
}
return n;
}
public int left_x(int n) {
if (this == NORTH) {
return n - 1;
}
if (this == SOUTH) {
return n + 1;
}
return n;
}
public int left_y(int n) {
if (this == EAST) {
return n + 1;
}
if (this == WEST) {
return n - 1;
}
return n;
}
public int right_x(int n) {
if (this == NORTH) {
return n + 1;
}
if (this == SOUTH) {
return n - 1;
}
return n;
}
public int right_y(int n) {
if (this == EAST) {
return n - 1;
}
if (this == WEST) {
return n + 1;
}
return n;
}
}
What are your thoughts about this class? The main grip I have personally is the left_y
, right_x
, etc methods. The code works, and here is some example of its usage below. But I feel there may be a better way?
Example usage:
Direction dir = Direction.randomDirection();
if (grid.get(dir.left_x(x), dir.left_y(y)).equals(CoreMaterial.WALL) && grid.get(dir.right_x(x), dir.right_y(y)).equals(CoreMaterial.WALL) && grid.get(dir.backwards_x(x), dir.backwards_y(y)).equals(CoreMaterial.FLOOR) && grid.get(dir.forwards_x(x), dir.forwards_y(y)).equals(CoreMaterial.FLOOR)) {
StandardMethods.build_door(dir, new Vector2D(x, y), CoreMaterial.CAKE, grid);
}
The above example has a location (x,y) for a door on a grid and then checks around it to ensure that there are doors either side and floors the other two either sides?
Symbols:
.
= floor
#
= wall
+
= door
Example:
.#.
.+.
.#.
This would be okay for the placing of the door.