Simplify train and rail logic and consolidate integration tests
This commit is contained in:
@@ -1,42 +1,102 @@
|
||||
package online.astradev.content.rail;
|
||||
|
||||
import com.google.gson.*;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import net.minecraft.world.level.block.state.properties.RailShape;
|
||||
import org.joml.Vector3f;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import com.google.gson.*;
|
||||
|
||||
import net.minecraft.world.level.block.state.properties.RailShape;
|
||||
|
||||
import org.joml.Vector3f;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
class RailDisplayTransformTest {
|
||||
@Test void everyOverlayVertexMatchesNativeRailPlane()throws Exception{
|
||||
for(int age=0;age<4;age++)for(boolean powered:new boolean[]{false,true})for(var shape:new RailShape[]{
|
||||
RailShape.NORTH_SOUTH,RailShape.EAST_WEST,RailShape.ASCENDING_NORTH,RailShape.ASCENDING_SOUTH,RailShape.ASCENDING_EAST,RailShape.ASCENDING_WEST}){
|
||||
String path="/defaults/resources/assets/astracontent/models/rail_display/"+age+"/"+shape.getSerializedName()+(powered?"_on":"")+".json";
|
||||
try(var stream=getClass().getResourceAsStream(path)){
|
||||
assertNotNull(stream,path);var model=JsonParser.parseReader(new InputStreamReader(stream,StandardCharsets.UTF_8)).getAsJsonObject();
|
||||
for(var value:model.getAsJsonArray("elements")){
|
||||
var element=value.getAsJsonObject();var from=vector(element.getAsJsonArray("from"));var to=vector(element.getAsJsonArray("to"));
|
||||
for(float x:new float[]{from.x,to.x})for(float z:new float[]{from.z,to.z}){
|
||||
var point=new Vector3f(x,from.y,z);
|
||||
if(element.has("rotation")){
|
||||
var rotation=element.getAsJsonObject("rotation");var origin=vector(rotation.getAsJsonArray("origin"));
|
||||
float radians=(float)Math.toRadians(rotation.get("angle").getAsFloat());point.sub(origin);
|
||||
if(rotation.get("rescale").getAsBoolean()){float scale=1/(float)Math.cos(radians);point.mul(1,scale,scale);}
|
||||
point.rotateX(radians).add(origin);
|
||||
@Test
|
||||
void everyOverlayVertexMatchesNativeRailPlane() throws Exception {
|
||||
for (int age = 0; age < 4; age++)
|
||||
for (boolean powered : new boolean[] {false, true})
|
||||
for (var shape :
|
||||
new RailShape[] {
|
||||
RailShape.NORTH_SOUTH,
|
||||
RailShape.EAST_WEST,
|
||||
RailShape.ASCENDING_NORTH,
|
||||
RailShape.ASCENDING_SOUTH,
|
||||
RailShape.ASCENDING_EAST,
|
||||
RailShape.ASCENDING_WEST
|
||||
}) {
|
||||
String path =
|
||||
"/defaults/resources/assets/astracontent/models/rail_display/"
|
||||
+ age
|
||||
+ "/"
|
||||
+ shape.getSerializedName()
|
||||
+ (powered ? "_on" : "")
|
||||
+ ".json";
|
||||
try (var stream = getClass().getResourceAsStream(path)) {
|
||||
assertNotNull(stream, path);
|
||||
var model =
|
||||
JsonParser.parseReader(
|
||||
new InputStreamReader(
|
||||
stream, StandardCharsets.UTF_8))
|
||||
.getAsJsonObject();
|
||||
for (var value : model.getAsJsonArray("elements")) {
|
||||
var element = value.getAsJsonObject();
|
||||
var from = vector(element.getAsJsonArray("from"));
|
||||
var to = vector(element.getAsJsonArray("to"));
|
||||
for (float x : new float[] {from.x, to.x})
|
||||
for (float z : new float[] {from.z, to.z}) {
|
||||
var point = new Vector3f(x, from.y, z);
|
||||
if (element.has("rotation")) {
|
||||
var rotation = element.getAsJsonObject("rotation");
|
||||
var origin = vector(rotation.getAsJsonArray("origin"));
|
||||
float radians =
|
||||
(float)
|
||||
Math.toRadians(
|
||||
rotation.get("angle").getAsFloat());
|
||||
point.sub(origin);
|
||||
if (rotation.get("rescale").getAsBoolean()) {
|
||||
float scale = 1 / (float) Math.cos(radians);
|
||||
point.mul(1, scale, scale);
|
||||
}
|
||||
point.rotateX(radians).add(origin);
|
||||
}
|
||||
// Item models are centered at .5, then the vanilla item
|
||||
// renderer turns them by pi.
|
||||
point.div(16)
|
||||
.sub(.5f, .5f, .5f)
|
||||
.rotateY((float) Math.PI)
|
||||
.rotate(RailDisplayTransform.orientation(shape))
|
||||
.add(.5f, .5f, .5f);
|
||||
float nativeHeight =
|
||||
1f / 16
|
||||
+ switch (shape) {
|
||||
case ASCENDING_NORTH -> 1 - point.z;
|
||||
case ASCENDING_SOUTH -> point.z;
|
||||
case ASCENDING_EAST -> point.x;
|
||||
case ASCENDING_WEST -> 1 - point.x;
|
||||
default -> 0;
|
||||
};
|
||||
assertTrue(
|
||||
point.y > nativeHeight && point.y - nativeHeight < .01,
|
||||
path
|
||||
+ " vertex="
|
||||
+ point
|
||||
+ " rail height="
|
||||
+ nativeHeight);
|
||||
assertTrue(
|
||||
point.x > -.01
|
||||
&& point.x < 1.01
|
||||
&& point.z > -.01
|
||||
&& point.z < 1.01,
|
||||
path + " stays inside rail footprint");
|
||||
}
|
||||
}
|
||||
// Item models are centered at .5, then the vanilla item renderer turns them by pi.
|
||||
point.div(16).sub(.5f,.5f,.5f).rotateY((float)Math.PI).rotate(RailDisplayTransform.orientation(shape)).add(.5f,.5f,.5f);
|
||||
float nativeHeight=1f/16+switch(shape){
|
||||
case ASCENDING_NORTH->1-point.z;case ASCENDING_SOUTH->point.z;
|
||||
case ASCENDING_EAST->point.x;case ASCENDING_WEST->1-point.x;default->0;
|
||||
};
|
||||
assertTrue(point.y>nativeHeight && point.y-nativeHeight<.01,path+" vertex="+point+" rail height="+nativeHeight);
|
||||
assertTrue(point.x>-.01 && point.x<1.01 && point.z>-.01 && point.z<1.01,path+" stays inside rail footprint");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
private static Vector3f vector(JsonArray a){return new Vector3f(a.get(0).getAsFloat(),a.get(1).getAsFloat(),a.get(2).getAsFloat());}
|
||||
|
||||
private static Vector3f vector(JsonArray a) {
|
||||
return new Vector3f(a.get(0).getAsFloat(), a.get(1).getAsFloat(), a.get(2).getAsFloat());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user