mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-06-17 22:31:52 +00:00
make vector2 immutable by default
This commit is contained in:
+3
-3
@@ -16,12 +16,12 @@ import com.dfsek.terra.api.util.vector.Vector2;
|
|||||||
|
|
||||||
|
|
||||||
public class BiomeHolderImpl implements BiomeHolder {
|
public class BiomeHolderImpl implements BiomeHolder {
|
||||||
private final Vector2 origin;
|
private final Vector2.Mutable origin;
|
||||||
private final int width;
|
private final int width;
|
||||||
private final int offset;
|
private final int offset;
|
||||||
private BiomeDelegate[][] biomes;
|
private BiomeDelegate[][] biomes;
|
||||||
|
|
||||||
public BiomeHolderImpl(int width, Vector2 origin) {
|
public BiomeHolderImpl(int width, Vector2.Mutable origin) {
|
||||||
width += 4;
|
width += 4;
|
||||||
this.width = width;
|
this.width = width;
|
||||||
biomes = new BiomeDelegate[width][width];
|
biomes = new BiomeDelegate[width][width];
|
||||||
@@ -29,7 +29,7 @@ public class BiomeHolderImpl implements BiomeHolder {
|
|||||||
this.offset = 2;
|
this.offset = 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
private BiomeHolderImpl(BiomeDelegate[][] biomes, Vector2 origin, int width, int offset) {
|
private BiomeHolderImpl(BiomeDelegate[][] biomes, Vector2.Mutable origin, int width, int offset) {
|
||||||
this.biomes = biomes;
|
this.biomes = biomes;
|
||||||
this.origin = origin;
|
this.origin = origin;
|
||||||
this.width = width;
|
this.width = width;
|
||||||
|
|||||||
+1
-1
@@ -39,7 +39,7 @@ public class BiomePipeline {
|
|||||||
* @return BiomeHolder containing biomes.
|
* @return BiomeHolder containing biomes.
|
||||||
*/
|
*/
|
||||||
public BiomeHolder getBiomes(int x, int z, long seed) {
|
public BiomeHolder getBiomes(int x, int z, long seed) {
|
||||||
BiomeHolder holder = new BiomeHolderImpl(init, Vector2.of(x * (init - 1), z * (init - 1)));
|
BiomeHolder holder = new BiomeHolderImpl(init, Vector2.of(x * (init - 1), z * (init - 1)).mutable());
|
||||||
holder.fill(source, seed);
|
holder.fill(source, seed);
|
||||||
for(Stage stage : stages) holder = stage.apply(holder, seed);
|
for(Stage stage : stages) holder = stage.apply(holder, seed);
|
||||||
return holder;
|
return holder;
|
||||||
|
|||||||
+2
-3
@@ -41,10 +41,9 @@ public class BiomeFunction implements Function<String> {
|
|||||||
public String apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
|
public String apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
|
||||||
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
|
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
|
||||||
|
|
||||||
Vector2 xz = Vector2.of(x.apply(implementationArguments, variableMap).doubleValue(),
|
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.apply(implementationArguments, variableMap).doubleValue(),
|
||||||
z.apply(implementationArguments, variableMap).doubleValue());
|
z.apply(implementationArguments, variableMap).doubleValue()), arguments.getRotation());
|
||||||
|
|
||||||
RotationUtil.rotateVector(xz, arguments.getRotation());
|
|
||||||
|
|
||||||
BiomeProvider grid = arguments.getWorld().getBiomeProvider();
|
BiomeProvider grid = arguments.getWorld().getBiomeProvider();
|
||||||
|
|
||||||
|
|||||||
+2
-3
@@ -71,10 +71,9 @@ public class BlockFunction implements Function<Void> {
|
|||||||
|
|
||||||
void setBlock(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap,
|
void setBlock(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap,
|
||||||
TerraImplementationArguments arguments, BlockState rot) {
|
TerraImplementationArguments arguments, BlockState rot) {
|
||||||
Vector2 xz = Vector2.of(x.apply(implementationArguments, variableMap).doubleValue(),
|
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.apply(implementationArguments, variableMap).doubleValue(),
|
||||||
z.apply(implementationArguments, variableMap).doubleValue());
|
z.apply(implementationArguments, variableMap).doubleValue()), arguments.getRotation());
|
||||||
|
|
||||||
RotationUtil.rotateVector(xz, arguments.getRotation());
|
|
||||||
|
|
||||||
RotationUtil.rotateBlockData(rot, arguments.getRotation().inverse());
|
RotationUtil.rotateBlockData(rot, arguments.getRotation().inverse());
|
||||||
try {
|
try {
|
||||||
|
|||||||
+2
-3
@@ -38,10 +38,9 @@ public class CheckBlockFunction implements Function<String> {
|
|||||||
public String apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
|
public String apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
|
||||||
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
|
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
|
||||||
|
|
||||||
Vector2 xz = Vector2.of(x.apply(implementationArguments, variableMap).doubleValue(),
|
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.apply(implementationArguments, variableMap).doubleValue(),
|
||||||
z.apply(implementationArguments, variableMap).doubleValue());
|
z.apply(implementationArguments, variableMap).doubleValue()), arguments.getRotation());
|
||||||
|
|
||||||
RotationUtil.rotateVector(xz, arguments.getRotation());
|
|
||||||
|
|
||||||
String data = arguments.getWorld()
|
String data = arguments.getWorld()
|
||||||
.getBlockState(arguments.getOrigin()
|
.getBlockState(arguments.getOrigin()
|
||||||
|
|||||||
+2
-3
@@ -47,10 +47,9 @@ public class EntityFunction implements Function<Void> {
|
|||||||
@Override
|
@Override
|
||||||
public Void apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
|
public Void apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
|
||||||
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
|
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
|
||||||
Vector2 xz = Vector2.of(x.apply(implementationArguments, variableMap).doubleValue(),
|
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.apply(implementationArguments, variableMap).doubleValue(),
|
||||||
z.apply(implementationArguments, variableMap).doubleValue());
|
z.apply(implementationArguments, variableMap).doubleValue()), arguments.getRotation());
|
||||||
|
|
||||||
RotationUtil.rotateVector(xz, arguments.getRotation());
|
|
||||||
Entity entity = arguments.getWorld().spawnEntity(Vector3.of(xz.getX(), y.apply(implementationArguments, variableMap).doubleValue(), xz.getZ()).add(arguments.getOrigin()).add(0.5, 0, 0.5), data);
|
Entity entity = arguments.getWorld().spawnEntity(Vector3.of(xz.getX(), y.apply(implementationArguments, variableMap).doubleValue(), xz.getZ()).add(arguments.getOrigin()).add(0.5, 0, 0.5), data);
|
||||||
platform.getEventManager().callEvent(new EntitySpawnEvent(entity.world().getPack(), entity));
|
platform.getEventManager().callEvent(new EntitySpawnEvent(entity.world().getPack(), entity));
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
+2
-3
@@ -36,10 +36,9 @@ public class GetMarkFunction implements Function<String> {
|
|||||||
@Override
|
@Override
|
||||||
public String apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
|
public String apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
|
||||||
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
|
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
|
||||||
Vector2 xz = Vector2.of(x.apply(implementationArguments, variableMap).doubleValue(),
|
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.apply(implementationArguments, variableMap).doubleValue(),
|
||||||
z.apply(implementationArguments, variableMap).doubleValue());
|
z.apply(implementationArguments, variableMap).doubleValue()), arguments.getRotation());
|
||||||
|
|
||||||
RotationUtil.rotateVector(xz, arguments.getRotation());
|
|
||||||
String mark = arguments.getMark(Vector3.of(FastMath.floorToInt(xz.getX()), FastMath.floorToInt(
|
String mark = arguments.getMark(Vector3.of(FastMath.floorToInt(xz.getX()), FastMath.floorToInt(
|
||||||
y.apply(implementationArguments, variableMap).doubleValue()), FastMath.floorToInt(xz.getZ())).add(arguments.getOrigin()));
|
y.apply(implementationArguments, variableMap).doubleValue()), FastMath.floorToInt(xz.getZ())).add(arguments.getOrigin()));
|
||||||
return mark == null ? "" : mark;
|
return mark == null ? "" : mark;
|
||||||
|
|||||||
+2
-3
@@ -58,10 +58,9 @@ public class LootFunction implements Function<Void> {
|
|||||||
@Override
|
@Override
|
||||||
public Void apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
|
public Void apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
|
||||||
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
|
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
|
||||||
Vector2 xz = Vector2.of(x.apply(implementationArguments, variableMap).doubleValue(),
|
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.apply(implementationArguments, variableMap).doubleValue(),
|
||||||
z.apply(implementationArguments, variableMap).doubleValue());
|
z.apply(implementationArguments, variableMap).doubleValue()), arguments.getRotation());
|
||||||
|
|
||||||
RotationUtil.rotateVector(xz, arguments.getRotation());
|
|
||||||
|
|
||||||
String id = data.apply(implementationArguments, variableMap);
|
String id = data.apply(implementationArguments, variableMap);
|
||||||
|
|
||||||
|
|||||||
+2
-3
@@ -45,10 +45,9 @@ public class PullFunction implements Function<Void> {
|
|||||||
@Override
|
@Override
|
||||||
public Void apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
|
public Void apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
|
||||||
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
|
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
|
||||||
Vector2 xz = Vector2.of(x.apply(implementationArguments, variableMap).doubleValue(),
|
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.apply(implementationArguments, variableMap).doubleValue(),
|
||||||
z.apply(implementationArguments, variableMap).doubleValue());
|
z.apply(implementationArguments, variableMap).doubleValue()), arguments.getRotation());
|
||||||
|
|
||||||
RotationUtil.rotateVector(xz, arguments.getRotation());
|
|
||||||
BlockState rot = data.clone();
|
BlockState rot = data.clone();
|
||||||
RotationUtil.rotateBlockData(rot, arguments.getRotation().inverse());
|
RotationUtil.rotateBlockData(rot, arguments.getRotation().inverse());
|
||||||
|
|
||||||
|
|||||||
+2
-3
@@ -38,10 +38,9 @@ public class SetMarkFunction implements Function<Void> {
|
|||||||
@Override
|
@Override
|
||||||
public Void apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
|
public Void apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
|
||||||
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
|
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
|
||||||
Vector2 xz = Vector2.of(x.apply(implementationArguments, variableMap).doubleValue(),
|
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.apply(implementationArguments, variableMap).doubleValue(),
|
||||||
z.apply(implementationArguments, variableMap).doubleValue());
|
z.apply(implementationArguments, variableMap).doubleValue()), arguments.getRotation());
|
||||||
|
|
||||||
RotationUtil.rotateVector(xz, arguments.getRotation());
|
|
||||||
|
|
||||||
arguments.setMark(Vector3.of(FastMath.floorToInt(xz.getX()),
|
arguments.setMark(Vector3.of(FastMath.floorToInt(xz.getX()),
|
||||||
FastMath.floorToInt(
|
FastMath.floorToInt(
|
||||||
|
|||||||
+3
-3
@@ -45,9 +45,9 @@ public class StateFunction implements Function<Void> {
|
|||||||
@Override
|
@Override
|
||||||
public Void apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
|
public Void apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
|
||||||
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
|
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
|
||||||
Vector2 xz = Vector2.of(x.apply(implementationArguments, variableMap).doubleValue(),
|
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.apply(implementationArguments, variableMap).doubleValue(),
|
||||||
z.apply(implementationArguments, variableMap).doubleValue());
|
z.apply(implementationArguments, variableMap).doubleValue()), arguments.getRotation());
|
||||||
RotationUtil.rotateVector(xz, arguments.getRotation());
|
|
||||||
|
|
||||||
Vector3 origin = Vector3.of(FastMath.roundToInt(xz.getX()), y.apply(implementationArguments, variableMap).intValue(),
|
Vector3 origin = Vector3.of(FastMath.roundToInt(xz.getX()), y.apply(implementationArguments, variableMap).intValue(),
|
||||||
FastMath.roundToInt(xz.getZ())).add(arguments.getOrigin());
|
FastMath.roundToInt(xz.getZ())).add(arguments.getOrigin());
|
||||||
|
|||||||
+2
-3
@@ -63,10 +63,9 @@ public class StructureFunction implements Function<Boolean> {
|
|||||||
if(arguments.getRecursions() > platform.getTerraConfig().getMaxRecursion())
|
if(arguments.getRecursions() > platform.getTerraConfig().getMaxRecursion())
|
||||||
throw new RuntimeException("Structure recursion too deep: " + arguments.getRecursions());
|
throw new RuntimeException("Structure recursion too deep: " + arguments.getRecursions());
|
||||||
|
|
||||||
Vector2 xz = Vector2.of(x.apply(implementationArguments, variableMap).doubleValue(),
|
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.apply(implementationArguments, variableMap).doubleValue(),
|
||||||
z.apply(implementationArguments, variableMap).doubleValue());
|
z.apply(implementationArguments, variableMap).doubleValue()), arguments.getRotation());
|
||||||
|
|
||||||
RotationUtil.rotateVector(xz, arguments.getRotation());
|
|
||||||
|
|
||||||
String app = id.apply(implementationArguments, variableMap);
|
String app = id.apply(implementationArguments, variableMap);
|
||||||
return registry.get(app).map(script -> {
|
return registry.get(app).map(script -> {
|
||||||
|
|||||||
@@ -16,10 +16,10 @@ import com.dfsek.terra.api.block.state.properties.enums.RailShape;
|
|||||||
import com.dfsek.terra.api.block.state.properties.enums.RedstoneConnection;
|
import com.dfsek.terra.api.block.state.properties.enums.RedstoneConnection;
|
||||||
import com.dfsek.terra.api.block.state.properties.enums.WallHeight;
|
import com.dfsek.terra.api.block.state.properties.enums.WallHeight;
|
||||||
import com.dfsek.terra.api.util.vector.Vector2;
|
import com.dfsek.terra.api.util.vector.Vector2;
|
||||||
|
import com.dfsek.terra.api.util.vector.Vector2.Mutable;
|
||||||
|
|
||||||
|
|
||||||
public final class RotationUtil {
|
public final class RotationUtil {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Rotate and mirror a coordinate pair.
|
* Rotate and mirror a coordinate pair.
|
||||||
*
|
*
|
||||||
@@ -29,15 +29,13 @@ public final class RotationUtil {
|
|||||||
* @return Rotated vector
|
* @return Rotated vector
|
||||||
*/
|
*/
|
||||||
public static Vector2 rotateVector(Vector2 orig, Rotation r) {
|
public static Vector2 rotateVector(Vector2 orig, Rotation r) {
|
||||||
Vector2 copy = orig.clone();
|
Mutable copy = orig.mutable();
|
||||||
switch(r) {
|
switch(r) {
|
||||||
case CW_90 -> copy.setX(orig.getZ()).setZ(-orig.getX());
|
case CW_90 -> copy.setX(orig.getZ()).setZ(-orig.getX());
|
||||||
case CCW_90 -> copy.setX(-orig.getZ()).setZ(orig.getX());
|
case CCW_90 -> copy.setX(-orig.getZ()).setZ(orig.getX());
|
||||||
case CW_180 -> copy.multiply(-1);
|
case CW_180 -> copy.multiply(-1);
|
||||||
}
|
}
|
||||||
orig.setX(copy.getX());
|
return copy.immutable();
|
||||||
orig.setZ(copy.getZ());
|
|
||||||
return orig;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -16,8 +16,8 @@ import com.dfsek.terra.api.util.MathUtil;
|
|||||||
* oh yeah
|
* oh yeah
|
||||||
*/
|
*/
|
||||||
public class Vector2 implements Cloneable {
|
public class Vector2 implements Cloneable {
|
||||||
private double x;
|
private final double x;
|
||||||
private double z;
|
private final double z;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a vector with a given X and Z component
|
* Create a vector with a given X and Z component
|
||||||
@@ -34,67 +34,7 @@ public class Vector2 implements Cloneable {
|
|||||||
return new Vector2(x, z);
|
return new Vector2(x, z);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Multiply X and Z components by a value.
|
|
||||||
*
|
|
||||||
* @param m Value to multiply
|
|
||||||
*
|
|
||||||
* @return Mutated vector, for chaining.
|
|
||||||
*/
|
|
||||||
public Vector2 multiply(double m) {
|
|
||||||
x *= m;
|
|
||||||
z *= m;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Add this vector to another.
|
|
||||||
*
|
|
||||||
* @param other Vector to add
|
|
||||||
*
|
|
||||||
* @return Mutated vector, for chaining.
|
|
||||||
*/
|
|
||||||
public Vector2 add(Vector2 other) {
|
|
||||||
x += other.getX();
|
|
||||||
z += other.getZ();
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Subtract a vector from this vector,
|
|
||||||
*
|
|
||||||
* @param other Vector to subtract
|
|
||||||
*
|
|
||||||
* @return Mutated vector, for chaining.
|
|
||||||
*/
|
|
||||||
public Vector2 subtract(Vector2 other) {
|
|
||||||
x -= other.getX();
|
|
||||||
z -= other.getZ();
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Normalize this vector to length 1
|
|
||||||
*
|
|
||||||
* @return Mutated vector, for chaining.
|
|
||||||
*/
|
|
||||||
public Vector2 normalize() {
|
|
||||||
divide(length());
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Divide X and Z components by a value.
|
|
||||||
*
|
|
||||||
* @param d Divisor
|
|
||||||
*
|
|
||||||
* @return Mutated vector, for chaining.
|
|
||||||
*/
|
|
||||||
public Vector2 divide(double d) {
|
|
||||||
x /= d;
|
|
||||||
z /= d;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the length of this Vector
|
* Get the length of this Vector
|
||||||
@@ -142,12 +82,6 @@ public class Vector2 implements Cloneable {
|
|||||||
return Vector3.of(this.x, y, this.z);
|
return Vector3.of(this.x, y, this.z);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Vector2 add(double x, double z) {
|
|
||||||
this.x += x;
|
|
||||||
this.z += z;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get X component
|
* Get X component
|
||||||
*
|
*
|
||||||
@@ -157,10 +91,6 @@ public class Vector2 implements Cloneable {
|
|||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Vector2 setX(double x) {
|
|
||||||
this.x = x;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get Z component
|
* Get Z component
|
||||||
@@ -171,10 +101,6 @@ public class Vector2 implements Cloneable {
|
|||||||
return z;
|
return z;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Vector2 setZ(double z) {
|
|
||||||
this.z = z;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getBlockX() {
|
public int getBlockX() {
|
||||||
return FastMath.floorToInt(x);
|
return FastMath.floorToInt(x);
|
||||||
@@ -197,11 +123,124 @@ public class Vector2 implements Cloneable {
|
|||||||
return MathUtil.equals(this.x, other.x) && MathUtil.equals(this.z, other.z);
|
return MathUtil.equals(this.x, other.x) && MathUtil.equals(this.z, other.z);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Vector2 clone() {
|
public Mutable mutable() {
|
||||||
try {
|
return new Mutable(x, z);
|
||||||
return (Vector2) super.clone();
|
}
|
||||||
} catch(CloneNotSupportedException e) {
|
|
||||||
throw new Error(e);
|
public static class Mutable {
|
||||||
|
private double x, z;
|
||||||
|
|
||||||
|
private Mutable(double x, double z) {
|
||||||
|
this.x = x;
|
||||||
|
this.z = z;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getX() {
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getZ() {
|
||||||
|
return z;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Vector2 immutable() {
|
||||||
|
return Vector2.of(x, z);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Mutable setX(double x) {
|
||||||
|
this.x = x;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Mutable setZ(double z) {
|
||||||
|
this.z = z;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the length of this Vector
|
||||||
|
*
|
||||||
|
* @return length
|
||||||
|
*/
|
||||||
|
public double length() {
|
||||||
|
return FastMath.sqrt(lengthSquared());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the squared length of this Vector
|
||||||
|
*
|
||||||
|
* @return squared length
|
||||||
|
*/
|
||||||
|
public double lengthSquared() {
|
||||||
|
return x * x + z * z;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Mutable add(double x, double z) {
|
||||||
|
this.x += x;
|
||||||
|
this.z += z;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Multiply X and Z components by a value.
|
||||||
|
*
|
||||||
|
* @param m Value to multiply
|
||||||
|
*
|
||||||
|
* @return Mutated vector, for chaining.
|
||||||
|
*/
|
||||||
|
public Mutable multiply(double m) {
|
||||||
|
x *= m;
|
||||||
|
z *= m;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add this vector to another.
|
||||||
|
*
|
||||||
|
* @param other Vector to add
|
||||||
|
*
|
||||||
|
* @return Mutated vector, for chaining.
|
||||||
|
*/
|
||||||
|
public Mutable add(Vector2 other) {
|
||||||
|
x += other.getX();
|
||||||
|
z += other.getZ();
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Subtract a vector from this vector,
|
||||||
|
*
|
||||||
|
* @param other Vector to subtract
|
||||||
|
*
|
||||||
|
* @return Mutated vector, for chaining.
|
||||||
|
*/
|
||||||
|
public Mutable subtract(Vector2 other) {
|
||||||
|
x -= other.getX();
|
||||||
|
z -= other.getZ();
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Normalize this vector to length 1
|
||||||
|
*
|
||||||
|
* @return Mutated vector, for chaining.
|
||||||
|
*/
|
||||||
|
public Mutable normalize() {
|
||||||
|
divide(length());
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Divide X and Z components by a value.
|
||||||
|
*
|
||||||
|
* @param d Divisor
|
||||||
|
*
|
||||||
|
* @return Mutated vector, for chaining.
|
||||||
|
*/
|
||||||
|
public Mutable divide(double d) {
|
||||||
|
x /= d;
|
||||||
|
z /= d;
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user