Make Pos an interface

This commit is contained in:
TheMode 2021-07-03 02:43:09 +02:00
parent 0f869f3cf2
commit f44fd0af8c
4 changed files with 141 additions and 64 deletions

View File

@ -0,0 +1,14 @@
package net.minestom.server.utils.incubator;
import org.jetbrains.annotations.Contract;
public interface Point {
@Contract(pure = true)
double x();
@Contract(pure = true)
double y();
@Contract(pure = true)
double z();
}

View File

@ -3,68 +3,47 @@ package net.minestom.server.utils.incubator;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
public class Pos implements Vec {
public static final Pos ZERO = new Pos();
public interface Pos extends Point {
private final double x, y, z;
private final float yaw, pitch;
public Pos(double x, double y, double z, float yaw, float pitch) {
this.x = x;
this.y = y;
this.z = z;
this.yaw = yaw;
this.pitch = pitch;
}
public Pos(double x, double y, double z) {
this(x, y, z, 0, 0);
}
public Pos() {
this(0, 0, 0, 0, 0);
@Contract(pure = true)
static @NotNull Pos pos(double x, double y, double z, float yaw, float pitch) {
return new PosImpl(x, y, z, yaw, pitch);
}
@Contract(pure = true)
public @NotNull Pos withYaw(float yaw) {
return new Pos(x, y, z, yaw, pitch);
static @NotNull Pos pos(@NotNull Vec vec, float yaw, float pitch) {
return pos(vec.x(), vec.y(), vec.z(), yaw, pitch);
}
@Contract(pure = true)
public @NotNull Pos withPitch(float pitch) {
return new Pos(x, y, z, yaw, pitch);
static @NotNull Pos pos(double x, double y, double z) {
return new PosImpl(x, y, z);
}
@Override
public @NotNull Pos with(double x, double y, double z) {
return new Pos(x, y, z, pitch, yaw);
@Contract(pure = true)
static @NotNull Pos pos(@NotNull Vec vec) {
return pos(vec.x(), vec.y(), vec.z());
}
@Override
public double x() {
return x;
@Contract(pure = true)
@NotNull Pos withCoord(double x, double y, double z);
@Contract(pure = true)
default @NotNull Pos withCoord(Vec vec) {
return withCoord(vec.x(), vec.y(), vec.z());
}
@Override
public double y() {
return y;
}
@Contract(pure = true)
@NotNull Pos withView(float yaw, float pitch);
@Override
public double z() {
return z;
}
@Contract(pure = true)
float yaw();
public float yaw() {
return yaw;
}
@Contract(pure = true)
float pitch();
public float pitch() {
return pitch;
}
@Override
public @NotNull Pos asPosition() {
return this;
@Contract(pure = true)
default @NotNull Vec asVec() {
return Vec.vec(x(), y(), z());
}
}

View File

@ -0,0 +1,55 @@
package net.minestom.server.utils.incubator;
import org.jetbrains.annotations.NotNull;
class PosImpl implements Pos {
private final double x, y, z;
private final float yaw, pitch;
PosImpl(double x, double y, double z, float yaw, float pitch) {
this.x = x;
this.y = y;
this.z = z;
this.yaw = yaw;
this.pitch = pitch;
}
PosImpl(double x, double y, double z) {
this(x, y, z, 0, 0);
}
@Override
public double x() {
return x;
}
@Override
public double y() {
return y;
}
@Override
public double z() {
return z;
}
@Override
public @NotNull Pos withCoord(double x, double y, double z) {
return new PosImpl(x, y, z, yaw, pitch);
}
@Override
public @NotNull Pos withView(float yaw, float pitch) {
return new PosImpl(x, y, z, yaw, pitch);
}
@Override
public float yaw() {
return yaw;
}
@Override
public float pitch() {
return pitch;
}
}

View File

@ -7,28 +7,66 @@ import org.jetbrains.annotations.NotNull;
import java.util.function.DoubleUnaryOperator;
import java.util.function.UnaryOperator;
public interface Vec {
/**
* Represents an immutable 3D vector.
*/
public interface Vec extends Point {
Vec ZERO = vec(0);
Vec ONE = vec(1);
/**
* Creates a new vec with the 3 coordinates set.
*
* @param x the X coordinate
* @param y the Y coordinate
* @param z the Z coordinate
* @return the created vec
*/
@Contract(pure = true)
static @NotNull Vec vec(double x, double y, double z) {
return new VecImpl.Vec3(x, y, z);
}
/**
* Creates a new vec with the [x;z] coordinates set. Y is set to 0.
*
* @param x the X coordinate
* @param z the Z coordinate
* @return the created vec
*/
@Contract(pure = true)
static @NotNull Vec vec(double x, double z) {
return new VecImpl.Tuple(x, z);
}
/**
* Creates a vec with all 3 coordinates sharing the same value.
*
* @param value the coordinates
* @return the created vec
*/
@Contract(pure = true)
static @NotNull Vec vec(double value) {
return new VecImpl.Single(value);
}
/**
* Creates a new vec of the same type with the specified coordinates.
*
* @param x the X coordinate
* @param y the Y coordinate
* @param z the Z coordinate
* @return the creates vec
*/
@Contract(pure = true)
@NotNull Vec with(double x, double y, double z);
/**
* Creates a new vec with coordinated depending on {@code this}.
*
* @param operator the operator
* @return the created vec
*/
@Contract(pure = true)
default @NotNull Vec with(@NotNull Operator operator) {
return operator.apply(x(), y(), z());
@ -71,7 +109,7 @@ public interface Vec {
@Contract(pure = true)
default @NotNull Vec add(double value) {
return add(vec(value));
return with(x() + value, y() + value, z() + value);
}
@Contract(pure = true)
@ -81,7 +119,7 @@ public interface Vec {
@Contract(pure = true)
default @NotNull Vec sub(double value) {
return sub(vec(value));
return with(x() - value, y() - value, z() - value);
}
@Contract(pure = true)
@ -91,7 +129,7 @@ public interface Vec {
@Contract(pure = true)
default @NotNull Vec mul(double value) {
return mul(vec(value));
return with(x() * value, y() * value, z() * value);
}
@Contract(pure = true)
@ -101,7 +139,7 @@ public interface Vec {
@Contract(pure = true)
default @NotNull Vec div(double value) {
return div(vec(value));
return with(x() / value, y() / value, z() / value);
}
@Contract(pure = true)
@ -121,7 +159,7 @@ public interface Vec {
@Contract(pure = true)
default @NotNull Vec min(double value) {
return min(vec(value));
return with(Math.min(x(), value), Math.min(y(), value), Math.min(z(), value));
}
@Contract(pure = true)
@ -131,7 +169,7 @@ public interface Vec {
@Contract(pure = true)
default @NotNull Vec max(double value) {
return max(vec(value));
return with(Math.max(x(), value), Math.max(y(), value), Math.max(z(), value));
}
@Contract(pure = true)
@ -141,7 +179,7 @@ public interface Vec {
@Contract(pure = true)
default @NotNull Pos asPosition() {
return new Pos(x(), y(), z());
return Pos.pos(this);
}
@Contract(pure = true)
@ -152,15 +190,6 @@ public interface Vec {
(int) Math.floor(z()));
}
@Contract(pure = true)
double x();
@Contract(pure = true)
double y();
@Contract(pure = true)
double z();
/**
* Gets the magnitude of the vector squared.
*