Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Implement equals() and hasCode() methods in Direction subclasses and …

…add rotate(float)
  • Loading branch information...
commit b27e57b9dcfd02f42737d1601953a81bb71a91ab 1 parent 57809c6
authored
7 core/src/main/java/fr/aumgn/bukkitutils/geom/Direction.java
@@ -10,6 +10,8 @@
10 10
11 11 Vector getVector();
12 12
  13 + Direction rotate(float angle);
  14 +
13 15 public static final Direction NONE = new Direction() {
14 16 @Override
15 17 public Vector2D getVector2D() {
@@ -30,5 +32,10 @@ public float getYaw() {
30 32 public float getPitch() {
31 33 return Float.NaN;
32 34 }
  35 +
  36 + @Override
  37 + public Direction rotate(float angle) {
  38 + return NONE;
  39 + }
33 40 };
34 41 }
75 core/src/main/java/fr/aumgn/bukkitutils/geom/Face.java
... ... @@ -1,30 +1,32 @@
1 1 package fr.aumgn.bukkitutils.geom;
2 2
3   -public enum Face implements Direction {
  3 +import fr.aumgn.bukkitutils.geom.direction.LocationDirection;
4 4
5   - UP ( 0.0f, 180.0f, 0, 1, 0),
6   - DOWN ( 0.0f, -180.0f, 0, -1, 0),
  5 +public final class Face implements Direction {
7 6
8   - NORTH ( 90.0f, 0.0f, -1, 0, 0),
9   - EAST (180.0f, 0.0f, 0, 0, -1),
10   - SOUTH (270.0f, 0.0f, 1, 0, 0),
11   - WEST ( 0.0f, 0.0f, 0, 0, 1),
  7 + public static final Face UP = new Face( 0.0f, 180.0f, 0, 1, 0);
  8 + public static final Face DOWN = new Face( 0.0f, -180.0f, 0, -1, 0);
12 9
13   - UP_NORTH ( 90.0f, 180.0f, -1, 1, 0),
14   - UP_EAST (180.0f, 180.0f, 0, 1, -1),
15   - UP_SOUTH (270.0f, 180.0f, 1, 1, 0),
16   - UP_WEST ( 0.0f, 180.0f, 0, 1, 1),
  10 + public static final Face NORTH = new Face( 90.0f, 0.0f, -1, 0, 0);
  11 + public static final Face EAST = new Face(180.0f, 0.0f, 0, 0, -1);
  12 + public static final Face SOUTH = new Face(270.0f, 0.0f, 1, 0, 0);
  13 + public static final Face WEST = new Face( 0.0f, 0.0f, 0, 0, 1);
17 14
18   - DOWN_NORTH ( 90.0f, -180.0f, -1, -1, 0),
19   - DOWN_EAST (180.0f, -180.0f, 0, -1, -1),
20   - DOWN_SOUTH (270.0f, -180.0f, 1, -1, 0),
21   - DOWN_WEST ( 0.0f, -180.0f, 0, -1, 1),
  15 + public static final Face UP_NORTH = new Face( 90.0f, 180.0f, -1, 1, 0);
  16 + public static final Face UP_EAST = new Face(180.0f, 180.0f, 0, 1, -1);
  17 + public static final Face UP_SOUTH = new Face(270.0f, 180.0f, 1, 1, 0);
  18 + public static final Face UP_WEST = new Face( 0.0f, 180.0f, 0, 1, 1);
22 19
23   - NORTH_EAST (135.0f, 0.0f, -1, 0, -1),
24   - NORTH_WEST ( 45.0f, 0.0f, -1, 0, 1),
  20 + public static final Face DOWN_NORTH = new Face( 90.0f, -180.0f, -1, -1, 0);
  21 + public static final Face DOWN_EAST = new Face(180.0f, -180.0f, 0, -1, -1);
  22 + public static final Face DOWN_SOUTH = new Face(270.0f, -180.0f, 1, -1, 0);
  23 + public static final Face DOWN_WEST = new Face( 0.0f, -180.0f, 0, -1, 1);
25 24
26   - SOUTH_EAST (225.0f, 0.0f, 1, 0, -1),
27   - SOUTH_WEST (315.0f, 0.0f, 1, 0, 1);
  25 + public static final Face NORTH_EAST = new Face(135.0f, 0.0f, -1, 0, -1);
  26 + public static final Face NORTH_WEST = new Face( 45.0f, 0.0f, -1, 0, 1);
  27 +
  28 + public static final Face SOUTH_EAST = new Face(225.0f, 0.0f, 1, 0, -1);
  29 + public static final Face SOUTH_WEST = new Face(315.0f, 0.0f, 1, 0, 1);
28 30
29 31 private final float yaw;
30 32 private final float pitch;
@@ -59,4 +61,39 @@ public Vector2D getVector2D() {
59 61 public Vector getVector() {
60 62 return new Vector(x, y, z);
61 63 }
  64 +
  65 + @Override
  66 + public Direction rotate(float angle) {
  67 + return new LocationDirection(yaw, pitch).rotate(angle);
  68 + }
  69 +
  70 + @Override
  71 + public int hashCode() {
  72 + final int prime = 31;
  73 + int result = 1;
  74 + result = prime * result + Float.floatToIntBits(yaw);
  75 + result = prime * result + Float.floatToIntBits(pitch);
  76 + return result;
  77 + }
  78 +
  79 + @Override
  80 + public boolean equals(Object obj) {
  81 + if (this == obj) {
  82 + return true;
  83 + }
  84 +
  85 + if (!(obj instanceof Direction)) {
  86 + return false;
  87 + }
  88 +
  89 + Direction other = (Direction) obj;
  90 + if (yaw != other.getYaw()) {
  91 + return false;
  92 + }
  93 + if (pitch != other.getPitch()) {
  94 + return false;
  95 + }
  96 +
  97 + return true;
  98 + }
62 99 }
40 core/src/main/java/fr/aumgn/bukkitutils/geom/direction/HorizontalDirection.java
@@ -12,6 +12,11 @@ public static Vector2D calculateVector2D(float yaw) {
12 12 Math.sin(radians), Math.cos(radians));
13 13 }
14 14
  15 + public static Direction calculateHorizontalRotation(Direction dir, float angle) {
  16 + float oppositeYaw = (dir.getYaw() + 180f) % 360f;
  17 + return new HorizontalDirection(oppositeYaw);
  18 + }
  19 +
15 20 protected final float yaw;
16 21 private Vector2D vector;
17 22
@@ -41,4 +46,39 @@ public Vector2D getVector2D() {
41 46 public Vector getVector() {
42 47 return new Vector();
43 48 }
  49 +
  50 + @Override
  51 + public Direction rotate(float angle) {
  52 + return calculateHorizontalRotation(this, angle);
  53 + }
  54 +
  55 + @Override
  56 + public int hashCode() {
  57 + final int prime = 31;
  58 + int result = 1;
  59 + result = prime * result + Float.floatToIntBits(yaw);
  60 + result = prime * result;
  61 + return result;
  62 + }
  63 +
  64 + @Override
  65 + public boolean equals(Object obj) {
  66 + if (this == obj) {
  67 + return true;
  68 + }
  69 +
  70 + if (!(obj instanceof Direction)) {
  71 + return false;
  72 + }
  73 +
  74 + Direction other = (Direction) obj;
  75 + if (yaw != other.getYaw()) {
  76 + return false;
  77 + }
  78 + if (0f != other.getPitch()) {
  79 + return false;
  80 + }
  81 +
  82 + return true;
  83 + }
44 84 }
42 core/src/main/java/fr/aumgn/bukkitutils/geom/direction/LocationDirection.java
@@ -2,6 +2,7 @@
2 2
3 3 import org.bukkit.Location;
4 4
  5 +import fr.aumgn.bukkitutils.geom.Direction;
5 6 import fr.aumgn.bukkitutils.geom.Vector;
6 7
7 8 public class LocationDirection extends HorizontalDirection {
@@ -10,6 +11,12 @@ public static double calculateVectorY(float yaw, float pitch) {
10 11 return 0.0;
11 12 }
12 13
  14 + public static Direction calculateRotation(Direction dir, float angle) {
  15 + float oppositeYaw = (dir.getYaw() + 180f) % 360f;
  16 + float oppositePitch = (dir.getPitch() + 180f + angle) % 360f - 180f;
  17 + return new LocationDirection(oppositeYaw, oppositePitch);
  18 + }
  19 +
13 20 private final float pitch;
14 21 private double y;
15 22
@@ -37,4 +44,39 @@ public double getVectorY() {
37 44 public Vector getVector() {
38 45 return getVector2D().to3D(getVectorY());
39 46 }
  47 +
  48 + @Override
  49 + public Direction rotate(float angle) {
  50 + return calculateRotation(this, angle);
  51 + }
  52 +
  53 + @Override
  54 + public int hashCode() {
  55 + final int prime = 31;
  56 + int result = 1;
  57 + result = prime * result + Float.floatToIntBits(yaw);
  58 + result = prime * result + Float.floatToIntBits(pitch);
  59 + return result;
  60 + }
  61 +
  62 + @Override
  63 + public boolean equals(Object obj) {
  64 + if (this == obj) {
  65 + return true;
  66 + }
  67 +
  68 + if (!(obj instanceof Direction)) {
  69 + return false;
  70 + }
  71 +
  72 + Direction other = (Direction) obj;
  73 + if (yaw != other.getYaw()) {
  74 + return false;
  75 + }
  76 + if (pitch != other.getPitch()) {
  77 + return false;
  78 + }
  79 +
  80 + return true;
  81 + }
40 82 }
28 core/src/main/java/fr/aumgn/bukkitutils/geom/direction/Vector2DDirection.java
@@ -52,4 +52,32 @@ public Vector2D getVector2D() {
52 52 public Vector getVector() {
53 53 return vector.to3D();
54 54 }
  55 +
  56 + @Override
  57 + public Direction rotate(float angle) {
  58 + return HorizontalDirection.calculateHorizontalRotation(this, angle);
  59 + }
  60 +
  61 + @Override
  62 + public int hashCode() {
  63 + final int prime = 31;
  64 + int result = 1;
  65 + result = prime * result + Float.floatToIntBits(getYaw());
  66 + result = prime * result + Float.floatToIntBits(getPitch());
  67 + return result;
  68 + }
  69 +
  70 + @Override
  71 + public boolean equals(Object obj) {
  72 + if (this == obj) {
  73 + return true;
  74 + }
  75 +
  76 + if (!(obj instanceof Direction)) {
  77 + return false;
  78 + }
  79 +
  80 + Direction other = (Direction) obj;
  81 + return vector.to3D().equals(other.getVector());
  82 + }
55 83 }
28 core/src/main/java/fr/aumgn/bukkitutils/geom/direction/VectorDirection.java
@@ -54,4 +54,32 @@ public Vector2D getVector2D() {
54 54 public Vector getVector() {
55 55 return vector;
56 56 }
  57 +
  58 + @Override
  59 + public Direction rotate(float angle) {
  60 + return LocationDirection.calculateRotation(this, angle);
  61 + }
  62 +
  63 + @Override
  64 + public int hashCode() {
  65 + final int prime = 31;
  66 + int result = 1;
  67 + result = prime * result + Float.floatToIntBits(getYaw());
  68 + result = prime * result + Float.floatToIntBits(getPitch());
  69 + return result;
  70 + }
  71 +
  72 + @Override
  73 + public boolean equals(Object obj) {
  74 + if (this == obj) {
  75 + return true;
  76 + }
  77 +
  78 + if (!(obj instanceof Direction)) {
  79 + return false;
  80 + }
  81 +
  82 + Direction other = (Direction) obj;
  83 + return vector.equals(other.getVector());
  84 + }
57 85 }

0 comments on commit b27e57b

Please sign in to comment.
Something went wrong with that request. Please try again.