start moving to int vector impls where possible

This commit is contained in:
dfsek
2021-11-24 13:54:43 -07:00
parent ba7722ce45
commit b04f7cfc55
15 changed files with 196 additions and 31 deletions

View File

@@ -0,0 +1,57 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.util;
import net.jafama.FastMath;
public enum Rotation {
CW_90(90),
CW_180(180),
CCW_90(270),
NONE(0);
private final int degrees;
Rotation(int degrees) {
this.degrees = degrees;
}
public static Rotation fromDegrees(int deg) {
return switch(FastMath.floorMod(deg, 360)) {
case 0 -> Rotation.NONE;
case 90 -> Rotation.CW_90;
case 180 -> Rotation.CW_180;
case 270 -> Rotation.CCW_90;
default -> throw new IllegalArgumentException();
};
}
public Rotation inverse() {
return switch(this) {
case NONE -> NONE;
case CCW_90 -> CW_90;
case CW_90 -> CCW_90;
case CW_180 -> CW_180;
};
}
public Rotation rotate(Rotation rotation) {
return fromDegrees(this.getDegrees() + rotation.getDegrees());
}
public int getDegrees() {
return degrees;
}
public enum Axis {
X,
Y,
Z
}
}

View File

@@ -0,0 +1,86 @@
package com.dfsek.terra.api.util.vector.integer;
import com.dfsek.terra.api.util.Rotation;
/**
* oh yeah
*/
public class Vector2Int {
private final int x;
private final int z;
private static final Vector2Int ZERO = new Vector2Int(0, 0);
private static final Vector2Int UNIT = new Vector2Int(0, 1);
protected Vector2Int(int x, int z) {
this.x = x;
this.z = z;
}
public static Vector2Int zero() {
return ZERO;
}
public static Vector2Int unit() {
return UNIT;
}
public static Vector2Int of(int x, int z) {
return new Vector2Int(x, z);
}
public int getX() {
return x;
}
public int getZ() {
return z;
}
public Vector3Int toVector3(int y) {
return new Vector3Int(x, y, z);
}
public Mutable mutable() {
return new Mutable(x, z);
}
public Vector2Int rotate(Rotation rotation) {
return switch(rotation) {
case CW_90 -> of(z, -x);
case CCW_90 -> of(-z, x);
case CW_180 -> of(-x, -z);
default -> this;
};
}
public static class Mutable {
private int x, z;
protected Mutable(int x, int z) {
this.x = x;
this.z = z;
}
public int getZ() {
return z;
}
public int getX() {
return x;
}
public void setZ(int z) {
this.z = z;
}
public void setX(int x) {
this.x = x;
}
public Vector2Int immutable() {
return new Vector2Int(x, z);
}
}
}

View File

@@ -0,0 +1,80 @@
package com.dfsek.terra.api.util.vector.integer;
public class Vector3Int {
private final int x, y, z;
private static final Vector3Int ZERO = new Vector3Int(0, 0, 0);
private static final Vector3Int UNIT = new Vector3Int(0, 1, 0);
protected Vector3Int(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getZ() {
return z;
}
public Mutable mutable() {
return new Mutable(x, y, z);
}
public static Vector3Int unit() {
return UNIT;
}
public static Vector3Int zero() {
return ZERO;
}
public static Vector3Int of(int x, int y, int z) {
return new Vector3Int(x, y, z);
}
public static class Mutable {
private int x, y, z;
protected Mutable(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public void setZ(int z) {
this.z = z;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getZ() {
return z;
}
public Vector3Int immutable() {
return Vector3Int.of(x, y, z);
}
}
}