2021-08-25 21:53:43 +02:00
|
|
|
package com.jozufozu.flywheel.vanilla;
|
|
|
|
|
2021-09-30 22:42:17 +02:00
|
|
|
import com.jozufozu.flywheel.backend.instancing.IDynamicInstance;
|
|
|
|
import com.jozufozu.flywheel.backend.instancing.ITickableInstance;
|
2021-08-25 21:53:43 +02:00
|
|
|
import com.jozufozu.flywheel.backend.instancing.entity.EntityInstance;
|
|
|
|
import com.jozufozu.flywheel.backend.material.MaterialManager;
|
|
|
|
import com.jozufozu.flywheel.backend.state.TextureRenderState;
|
|
|
|
import com.jozufozu.flywheel.core.Materials;
|
2021-09-12 23:49:00 +02:00
|
|
|
import com.jozufozu.flywheel.core.materials.model.ModelData;
|
2021-09-30 22:42:17 +02:00
|
|
|
import com.jozufozu.flywheel.core.model.IModel;
|
2021-08-25 21:53:43 +02:00
|
|
|
import com.jozufozu.flywheel.core.model.ModelPart;
|
|
|
|
import com.jozufozu.flywheel.util.AnimationTickHolder;
|
|
|
|
import com.jozufozu.flywheel.util.transform.MatrixTransformStack;
|
2021-11-18 23:59:39 +01:00
|
|
|
import com.mojang.math.Vector3f;
|
2021-08-25 21:53:43 +02:00
|
|
|
|
2021-09-18 00:59:20 +02:00
|
|
|
import net.minecraft.core.Vec3i;
|
2021-09-15 08:45:29 +02:00
|
|
|
import net.minecraft.resources.ResourceLocation;
|
|
|
|
import net.minecraft.util.Mth;
|
2021-11-18 23:59:39 +01:00
|
|
|
import net.minecraft.world.entity.vehicle.AbstractMinecart;
|
|
|
|
import net.minecraft.world.level.block.RenderShape;
|
|
|
|
import net.minecraft.world.level.block.state.BlockState;
|
2021-09-15 08:45:29 +02:00
|
|
|
import net.minecraft.world.phys.Vec3;
|
2021-08-25 21:53:43 +02:00
|
|
|
|
2021-09-30 22:42:17 +02:00
|
|
|
public class MinecartInstance<T extends AbstractMinecart> extends EntityInstance<T> implements IDynamicInstance, ITickableInstance {
|
2021-08-25 21:53:43 +02:00
|
|
|
|
|
|
|
private static final ResourceLocation MINECART_LOCATION = new ResourceLocation("textures/entity/minecart.png");
|
|
|
|
|
|
|
|
MatrixTransformStack stack = new MatrixTransformStack();
|
|
|
|
|
|
|
|
private final ModelData body;
|
2021-08-29 23:40:46 +02:00
|
|
|
private ModelData contents;
|
|
|
|
private BlockState blockstate;
|
2021-08-25 21:53:43 +02:00
|
|
|
|
|
|
|
public MinecartInstance(MaterialManager materialManager, T entity) {
|
|
|
|
super(materialManager, entity);
|
|
|
|
|
2021-08-29 23:40:46 +02:00
|
|
|
blockstate = entity.getDisplayBlockState();
|
2021-08-25 21:53:43 +02:00
|
|
|
contents = getContents();
|
|
|
|
body = getBody();
|
|
|
|
}
|
|
|
|
|
2021-08-29 23:40:46 +02:00
|
|
|
@Override
|
|
|
|
public void tick() {
|
|
|
|
BlockState displayBlockState = entity.getDisplayBlockState();
|
|
|
|
|
|
|
|
if (displayBlockState != blockstate) {
|
|
|
|
blockstate = displayBlockState;
|
|
|
|
contents.delete();
|
|
|
|
contents = getContents();
|
|
|
|
updateLight();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-25 21:53:43 +02:00
|
|
|
@Override
|
|
|
|
public void beginFrame() {
|
|
|
|
stack.setIdentity();
|
|
|
|
float pt = AnimationTickHolder.getPartialTicks();
|
|
|
|
|
2021-09-18 00:59:20 +02:00
|
|
|
Vec3i originCoordinate = materialManager.getOriginCoordinate();
|
2021-08-25 21:53:43 +02:00
|
|
|
stack.translate(
|
2021-09-18 00:59:20 +02:00
|
|
|
Mth.lerp(pt, entity.xOld, entity.getX()) - originCoordinate.getX(),
|
|
|
|
Mth.lerp(pt, entity.yOld, entity.getY()) - originCoordinate.getY(),
|
|
|
|
Mth.lerp(pt, entity.zOld, entity.getZ()) - originCoordinate.getZ());
|
2021-08-25 21:53:43 +02:00
|
|
|
|
2021-09-15 09:26:51 +02:00
|
|
|
float yaw = Mth.lerp(pt, entity.yRotO, entity.getYRot());
|
2021-08-25 21:53:43 +02:00
|
|
|
|
|
|
|
long i = (long)entity.getId() * 493286711L;
|
|
|
|
i = i * i * 4392167121L + i * 98761L;
|
|
|
|
float f = (((float)(i >> 16 & 7L) + 0.5F) / 8 - 0.5F) * 0.004F;
|
|
|
|
float f1 = (((float)(i >> 20 & 7L) + 0.5F) / 8 - 0.5F) * 0.004F;
|
|
|
|
float f2 = (((float)(i >> 24 & 7L) + 0.5F) / 8 - 0.5F) * 0.004F;
|
|
|
|
stack.translate(f, f1, f2);
|
|
|
|
stack.nudge(entity.getId());
|
2021-09-15 08:45:29 +02:00
|
|
|
double d0 = Mth.lerp(pt, entity.xOld, entity.getX());
|
|
|
|
double d1 = Mth.lerp(pt, entity.yOld, entity.getY());
|
|
|
|
double d2 = Mth.lerp(pt, entity.zOld, entity.getZ());
|
|
|
|
Vec3 vector3d = entity.getPos(d0, d1, d2);
|
2021-09-15 09:26:51 +02:00
|
|
|
float f3 = Mth.lerp(pt, entity.xRotO, entity.getXRot());
|
2021-08-25 21:53:43 +02:00
|
|
|
if (vector3d != null) {
|
2021-09-15 08:45:29 +02:00
|
|
|
Vec3 vector3d1 = entity.getPosOffs(d0, d1, d2, 0.3F);
|
|
|
|
Vec3 vector3d2 = entity.getPosOffs(d0, d1, d2, -0.3F);
|
2021-08-25 21:53:43 +02:00
|
|
|
if (vector3d1 == null) {
|
|
|
|
vector3d1 = vector3d;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vector3d2 == null) {
|
|
|
|
vector3d2 = vector3d;
|
|
|
|
}
|
|
|
|
|
|
|
|
stack.translate(vector3d.x - d0, (vector3d1.y + vector3d2.y) / 2.0D - d1, vector3d.z - d2);
|
2021-09-15 08:45:29 +02:00
|
|
|
Vec3 vector3d3 = vector3d2.add(-vector3d1.x, -vector3d1.y, -vector3d1.z);
|
2021-08-25 21:53:43 +02:00
|
|
|
if (vector3d3.length() != 0.0D) {
|
|
|
|
vector3d3 = vector3d3.normalize();
|
|
|
|
yaw = (float)(Math.atan2(vector3d3.z, vector3d3.x) * 180.0D / Math.PI);
|
|
|
|
f3 = (float)(Math.atan(vector3d3.y) * 73.0D);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stack.translate(0.0D, 0.375D, 0.0D);
|
|
|
|
stack.multiply(Vector3f.YP.rotationDegrees(180 - yaw));
|
|
|
|
stack.multiply(Vector3f.ZP.rotationDegrees(-f3));
|
|
|
|
float f5 = (float)entity.getHurtTime() - pt;
|
|
|
|
float f6 = entity.getDamage() - pt;
|
|
|
|
if (f6 < 0) {
|
|
|
|
f6 = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (f5 > 0) {
|
2021-09-15 08:45:29 +02:00
|
|
|
stack.multiply(Vector3f.XP.rotationDegrees(Mth.sin(f5) * f5 * f6 / 10 * (float)entity.getHurtDir()));
|
2021-08-25 21:53:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int j = entity.getDisplayOffset();
|
|
|
|
if (contents != null) {
|
|
|
|
stack.push();
|
|
|
|
stack.scale(0.75F);
|
|
|
|
stack.translate(-0.5D, (float)(j - 8) / 16, 0.5D);
|
|
|
|
stack.multiply(Vector3f.YP.rotationDegrees(90));
|
|
|
|
contents.setTransform(stack.unwrap());
|
|
|
|
stack.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
body.setTransform(stack.unwrap());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void updateLight() {
|
|
|
|
if (contents == null)
|
|
|
|
relight(getWorldPosition(), body);
|
|
|
|
else
|
|
|
|
relight(getWorldPosition(), body, contents);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void remove() {
|
|
|
|
body.delete();
|
|
|
|
if (contents != null) contents.delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
private ModelData getContents() {
|
2021-09-15 08:45:29 +02:00
|
|
|
if (blockstate.getRenderShape() == RenderShape.INVISIBLE)
|
2021-08-25 21:53:43 +02:00
|
|
|
return null;
|
|
|
|
|
|
|
|
return materialManager.defaultSolid()
|
|
|
|
.material(Materials.TRANSFORMED)
|
|
|
|
.getModel(blockstate)
|
|
|
|
.createInstance();
|
|
|
|
}
|
|
|
|
|
|
|
|
private ModelData getBody() {
|
|
|
|
return materialManager.solid(TextureRenderState.get(MINECART_LOCATION))
|
|
|
|
.material(Materials.TRANSFORMED)
|
|
|
|
.model(entity.getType(), this::getBodyModel)
|
|
|
|
.createInstance();
|
|
|
|
}
|
|
|
|
|
2021-09-30 22:42:17 +02:00
|
|
|
private IModel getBodyModel() {
|
2021-08-25 21:53:43 +02:00
|
|
|
int y = -3;
|
2021-11-09 05:48:02 +01:00
|
|
|
return ModelPart.builder("minecart", 64, 32)
|
2021-08-25 21:53:43 +02:00
|
|
|
.cuboid().invertYZ().start(-10, -8, -y).size(20, 16, 2).textureOffset(0, 10).rotateX(((float)Math.PI / 2F)).endCuboid()
|
|
|
|
.cuboid().invertYZ().start(-8, y, -10).size(16, 8, 2).rotateY(((float)Math.PI * 1.5F)).endCuboid()
|
|
|
|
.cuboid().invertYZ().start(-8, y, -10).size(16, 8, 2).rotateY(((float)Math.PI / 2F)).endCuboid()
|
|
|
|
.cuboid().invertYZ().start(-8, y, -8).size(16, 8, 2).rotateY((float)Math.PI).endCuboid()
|
|
|
|
.cuboid().invertYZ().start(-8, y, -8).size(16, 8, 2).endCuboid()
|
|
|
|
.build();
|
|
|
|
}
|
|
|
|
}
|