Create more Vec implementations

This commit is contained in:
TheMode 2021-06-30 01:53:01 +02:00
parent c42b494a62
commit b93f1a57cd
2 changed files with 92 additions and 27 deletions

View File

@ -8,17 +8,22 @@ import java.util.function.DoubleUnaryOperator;
import java.util.function.UnaryOperator;
public interface Vec {
Vec ZERO = new VecImpl(0);
Vec ONE = new VecImpl(1);
Vec ZERO = vec(0);
Vec ONE = vec(1);
@Contract(pure = true)
static @NotNull Vec vec(double x, double y, double z) {
return new VecImpl(x, y, z);
return new VecImpl.Vec3(x, y, z);
}
@Contract(pure = true)
static @NotNull Vec vec(double x, double z) {
return new VecImpl.Tuple(x, z);
}
@Contract(pure = true)
static @NotNull Vec vec(double value) {
return new VecImpl(value);
return new VecImpl.Single(value);
}
@Contract(pure = true)

View File

@ -2,36 +2,96 @@ package net.minestom.server.utils.incubator;
import org.jetbrains.annotations.NotNull;
class VecImpl implements Vec {
private final double x, y, z;
class VecImpl {
public VecImpl(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
static class Vec3 implements Vec {
private final double x, y, z;
Vec3(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
public Vec3(double value) {
this(value, value, value);
}
@Override
public @NotNull Vec with(double x, double y, double z) {
return new Vec3(x, y, z);
}
@Override
public double x() {
return x;
}
@Override
public double y() {
return y;
}
@Override
public double z() {
return z;
}
}
public VecImpl(double value) {
this(value, value, value);
static class Tuple implements Vec {
private final double x, z;
Tuple(double x, double z) {
this.x = x;
this.z = z;
}
@Override
public @NotNull Vec with(double x, double y, double z) {
return new Vec3(x, y, z);
}
@Override
public double x() {
return x;
}
@Override
public double y() {
return 0;
}
@Override
public double z() {
return z;
}
}
@Override
public @NotNull Vec with(double x, double y, double z) {
return new VecImpl(x, y, z);
}
static class Single implements Vec {
private final double value;
@Override
public double x() {
return x;
}
Single(double value) {
this.value = value;
}
@Override
public double y() {
return y;
}
@Override
public @NotNull Vec with(double x, double y, double z) {
return new Vec3(x, y, z);
}
@Override
public double z() {
return z;
@Override
public double x() {
return value;
}
@Override
public double y() {
return value;
}
@Override
public double z() {
return value;
}
}
}