diff --git a/src/main/java/net/onelitefeather/coris/shape/CuboidShape.java b/src/main/java/net/onelitefeather/coris/shape/CuboidShape.java index a8b3282..62b8cbe 100644 --- a/src/main/java/net/onelitefeather/coris/shape/CuboidShape.java +++ b/src/main/java/net/onelitefeather/coris/shape/CuboidShape.java @@ -1,5 +1,6 @@ package net.onelitefeather.coris.shape; +import net.minestom.server.coordinate.Point; import net.minestom.server.coordinate.Vec; import org.jetbrains.annotations.ApiStatus; @@ -9,7 +10,7 @@ * @param start the starting point of the cuboid * @param end the ending point of the cuboid * @author theEvilReaper - * @version 1.3.0 + * @version 1.4.0 * @since 0.1.0 */ @ApiStatus.Experimental @@ -30,6 +31,9 @@ public record CuboidShape(Vec start, Vec end) implements Shape { } } + /** + * {@inheritDoc} + */ @Override public int compareTo(Shape o) { if (!(o instanceof CuboidShape(Vec start1, Vec end1))) { @@ -40,6 +44,25 @@ public int compareTo(Shape o) { return compareVec(this.end, end1); } + /** + * {@inheritDoc} + */ + @Override + public boolean intersect2D(Point position) { + return position.blockX() >= start.blockX() && position.blockX() <= end.blockX() && + position.blockZ() >= start.blockZ() && position.blockZ() <= end.blockZ(); + } + + /** + * {@inheritDoc} + */ + @Override + public boolean intersect3D(Point position) { + return position.blockX() >= start.blockX() && position.blockX() <= end.blockX() && + position.blockY() >= start.blockY() && position.blockY() <= end.blockY() && + position.blockZ() >= start.blockZ() && position.blockZ() <= end.blockZ(); + } + /** * Compares two vectors lexicographically. * diff --git a/src/main/java/net/onelitefeather/coris/shape/PointShape.java b/src/main/java/net/onelitefeather/coris/shape/PointShape.java index 045e0f0..7989f20 100644 --- a/src/main/java/net/onelitefeather/coris/shape/PointShape.java +++ b/src/main/java/net/onelitefeather/coris/shape/PointShape.java @@ -1,5 +1,6 @@ package net.onelitefeather.coris.shape; +import net.minestom.server.coordinate.Point; import net.minestom.server.coordinate.Vec; /** @@ -8,11 +9,14 @@ * * @param position the position of the point in the 3D space * @author theEvilReaper - * @version 1.3.0 + * @version 1.4.0 * @since 0.1.0 */ public record PointShape(Vec position) implements Shape { + /** + * {@inheritDoc} + */ @Override public int compareTo(Shape o) { if (!(o instanceof PointShape(Vec position1))) return -1; @@ -22,4 +26,20 @@ public int compareTo(Shape o) { if (cmpY != 0) return cmpY; return Double.compare(this.position.z(), position1.z()); } + + /** + * {@inheritDoc} + */ + @Override + public boolean intersect2D(Point position) { + return this.intersect3D(position); + } + + /** + * {@inheritDoc} + */ + @Override + public boolean intersect3D(Point position) { + return this.position.equals(position); + } } diff --git a/src/main/java/net/onelitefeather/coris/shape/Shape.java b/src/main/java/net/onelitefeather/coris/shape/Shape.java index 22a1e57..f8b3539 100644 --- a/src/main/java/net/onelitefeather/coris/shape/Shape.java +++ b/src/main/java/net/onelitefeather/coris/shape/Shape.java @@ -1,5 +1,7 @@ package net.onelitefeather.coris.shape; +import net.minestom.server.coordinate.Point; +import net.onelitefeather.coris.util.Intersect; import org.jetbrains.annotations.ApiStatus; /** @@ -8,9 +10,9 @@ * The shape is a 2D or 3D representation of the area. * * @author theEvilReaper - * @version 1.2.0 + * @version 1.3.0 * @since 0.1.0 */ @ApiStatus.Experimental -public interface Shape extends Comparable { +public interface Shape extends Comparable, Intersect { } diff --git a/src/test/java/net/onelitefeather/coris/shape/intersect/CuboidShapeIntersectTest.java b/src/test/java/net/onelitefeather/coris/shape/intersect/CuboidShapeIntersectTest.java new file mode 100644 index 0000000..c787f52 --- /dev/null +++ b/src/test/java/net/onelitefeather/coris/shape/intersect/CuboidShapeIntersectTest.java @@ -0,0 +1,51 @@ +package net.onelitefeather.coris.shape.intersect; + +import net.minestom.server.coordinate.Vec; +import net.onelitefeather.coris.shape.CuboidShape; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class CuboidShapeIntersectTest { + + private static final CuboidShape SHAPE = new CuboidShape(new Vec(0, 0, 0), new Vec(5, 5, 5)); + + static Stream intersect3DProvider() { + return Stream.of( + Arguments.of(new Vec(2, 2, 2), true, "Inside shape"), + Arguments.of(new Vec(0, 0, 0), true, "On start border"), + Arguments.of(new Vec(5, 5, 5), true, "On end border"), + Arguments.of(new Vec(6, 6, 6), false, "Outside shape"), + Arguments.of(new Vec(-1, 2, 2), false, "Negative X outside"), + Arguments.of(new Vec(2, 6, 2), false, "Outside only Y") + ); + } + + static Stream intersect2DProvider() { + return Stream.of( + Arguments.of(new Vec(2, 99, 2), true, "Inside, Y ignored"), + Arguments.of(new Vec(0, 0, 0), true, "On start border"), + Arguments.of(new Vec(5, 0, 5), true, "On end border XZ"), + Arguments.of(new Vec(6, 2, 2), false, "Outside X"), + Arguments.of(new Vec(2, 2, 6), false, "Outside Z"), + Arguments.of(new Vec(2, 999, 2), true, "Large Y ignored"), + Arguments.of(new Vec(2, -999, 2), true, "Negative Y ignored") + ); + } + + @ParameterizedTest(name = "{2}") + @MethodSource("intersect3DProvider") + void testIntersect3D(Vec position, boolean expected, String description) { + assertEquals(expected, SHAPE.intersect3D(position)); + } + + @ParameterizedTest(name = "{2}") + @MethodSource("intersect2DProvider") + void testIntersect2D(Vec position, boolean expected, String description) { + assertEquals(expected, SHAPE.intersect2D(position)); + } +} diff --git a/src/test/java/net/onelitefeather/coris/shape/intersect/PointShapeIntersectTest.java b/src/test/java/net/onelitefeather/coris/shape/intersect/PointShapeIntersectTest.java new file mode 100644 index 0000000..5dead7d --- /dev/null +++ b/src/test/java/net/onelitefeather/coris/shape/intersect/PointShapeIntersectTest.java @@ -0,0 +1,37 @@ +package net.onelitefeather.coris.shape.intersect; + +import net.minestom.server.coordinate.Vec; +import net.onelitefeather.coris.shape.PointShape; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class PointShapeIntersectTest { + + private static final PointShape SHAPE = new PointShape(new Vec(2, 2, 2)); + + static Stream intersectProvider() { + return Stream.of( + Arguments.of(new Vec(2, 2, 2), true, "Exact match"), + Arguments.of(new Vec(3, 2, 2), false, "X mismatch"), + Arguments.of(new Vec(2, 3, 2), false, "Y mismatch"), + Arguments.of(new Vec(2, 2, 3), false, "Z mismatch") + ); + } + + @ParameterizedTest(name = "{2}") + @MethodSource("intersectProvider") + void testIntersect3D(Vec position, boolean expected, String description) { + assertEquals(expected, SHAPE.intersect3D(position)); + } + + @ParameterizedTest(name = "{2}") + @MethodSource("intersectProvider") + void testIntersect2D(Vec position, boolean expected, String description) { + assertEquals(expected, SHAPE.intersect2D(position)); + } +} \ No newline at end of file