Skip to content

Commit

Permalink
Fix animation
Browse files Browse the repository at this point in the history
  • Loading branch information
jptrzy committed Sep 27, 2021
1 parent 0c577e9 commit a043fb9
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 92 deletions.
12 changes: 12 additions & 0 deletions src/main/java/net/jptrzy/infusion/table/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,16 @@ public void onInitialize() {
Registry.register(Registry.BLOCK, new Identifier(Main.MOD_ID, "infusion_table"), INFUSION_TABLE);
Registry.register(Registry.ITEM, new Identifier(Main.MOD_ID, "infusion_table"), new BlockItem(INFUSION_TABLE, new FabricItemSettings().group(ItemGroup.DECORATIONS)));
}

public static float aroundRadial(float angle){
while(angle >= 3.1415927F) {
angle -= 6.2831855F;
}

while(angle < -3.1415927F) {
angle += 6.2831855F;
}

return angle;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,13 @@ public class InfusionTableEntity extends BlockEntity implements BlockEntityClien
private ItemStack item = new ItemStack(Items.AIR);
private ItemStack book = new ItemStack(Items.AIR);

private float bookOpenAngle = 0F;
private float bookAngle = 0F;
public float bookOpenAngle = 0F;
public float bookLastOpenAngle = 0F;

public float bookRot = 0;
public float bookLastRot = 0;
public float bookRotDir = 0;
public float bookRotForce = 0;

/*
0 - empty | passive
Expand Down Expand Up @@ -133,10 +138,7 @@ private static void dropStack(World world, Supplier<ItemEntity> itemEntitySuppli

public float bookAngleForce = 0;

public float bookRot = 0;
public float bookLastRot = 0;
public float bookRotDir = 0;
public float bookRotForce = 0;


public static void tick(World world, BlockPos pos, BlockState state, InfusionTableEntity entity) {

Expand All @@ -151,65 +153,71 @@ public static void tick(World world, BlockPos pos, BlockState state, InfusionTab
entity.bookRotDir += 0.02F;
}

entity.bookRotForce = (entity.bookRotDir - entity.bookRot) * 0.4F;
entity.bookRot += entity.bookRotForce;

//for( entity.bookAngleForce = entity.bookAngle - entity.bookAngleForce; entity.bookAngleForce >= 3.1415927F; entity.bookAngleForce -= 6.2831855F) {}



// switch (entity.state) {
// case 2:
// if(entity.item.isEmpty()){
// if(entity.bookOpenAngle > 0F) {
// entity.bookOpenAngle -= 0.1F;
// }else if(!world.isClient()){
// entity.bookOpenAngle = 0;
// entity.state = 1;
// entity.sync();
// }
// }else{
// if(entity.bookOpenAngle < 1F) {
// entity.bookOpenAngle += 0.1F;
// }else if(!world.isClient()){
// entity.bookOpenAngle = 1;
// entity.state = 3;
// entity.sync();
// }
// }
// break;
// case 4: // TODO
// if(entity.time > 60){
// if(entity.bookOpenAngle > 0F) {
// entity.bookOpenAngle -= 0.1F;
// }else{
// if(!world.isClient()){
// entity.state = 5;
// entity.time = 0;
// entity.bookOpenAngle = 0;
//
// Map<Enchantment, Integer> list = EnchantmentHelper.fromNbt(entity.item.getEnchantments());
// for ( Map.Entry<Enchantment, Integer> entry : list.entrySet() ) {
// entity.book.addEnchantment(entry.getKey(), entry.getValue());
// }
//
// entity.item = new ItemStack(Items.ENCHANTED_BOOK);
// entity.item.setNbt(entity.book.getNbt());
// entity.book = entity.item.copy();
//
// entity.sync();
// }
// }
// }else if(entity.time < 40){
// Random r = new Random();
// world.addParticle(ParticleTypes.ENCHANT, (double)pos.getX() + 0.5D, (double)pos.getY() + 1.0D, (double)pos.getZ() + 0.5D,
// (r.nextBoolean() ? -1 : 1) * r.nextFloat(),
// 1,
// (r.nextBoolean() ? -1 : 1) * r.nextFloat());
// }
// entity.time += 1;
// break;
// }
entity.bookRot = Main.aroundRadial(entity.bookRot);

entity.bookRotDir = Main.aroundRadial(entity.bookRotDir);

entity.bookRotForce = entity.bookRotDir - entity.bookRot;

entity.bookRotForce = Main.aroundRadial(entity.bookRotForce);

entity.bookRot += entity.bookRotForce * 0.4F;

entity.bookLastOpenAngle = entity.bookOpenAngle;

switch (entity.state) {
case 2:

if(entity.item.isEmpty()){
if(entity.bookOpenAngle > 0F) {
entity.bookOpenAngle -= 0.1F;
}else if(!world.isClient()){
entity.bookOpenAngle = 0;
entity.state = 1;
entity.sync();
}
}else{
if(entity.bookOpenAngle < 1F) {
entity.bookOpenAngle += 0.1F;
}else if(!world.isClient()){
entity.bookOpenAngle = 1;
entity.state = 3;
entity.sync();
}
}
break;
case 4: // TODO
if(entity.time > 60){
if(entity.bookOpenAngle > 0F) {
entity.bookOpenAngle -= 0.1F;
}else{
if(!world.isClient()){
entity.state = 5;
entity.time = 0;
entity.bookOpenAngle = 0;

Map<Enchantment, Integer> list = EnchantmentHelper.fromNbt(entity.item.getEnchantments());
for ( Map.Entry<Enchantment, Integer> entry : list.entrySet() ) {
entity.book.addEnchantment(entry.getKey(), entry.getValue());
}

entity.item = new ItemStack(Items.ENCHANTED_BOOK);
entity.item.setNbt(entity.book.getNbt());
entity.book = entity.item.copy();

entity.sync();
}
}
}else if(entity.time < 40){
Random r = new Random();
world.addParticle(ParticleTypes.ENCHANT, (double)pos.getX() + 0.5D, (double)pos.getY() + 1.0D, (double)pos.getZ() + 0.5D,
(r.nextBoolean() ? -1 : 1) * r.nextFloat(),
1,
(r.nextBoolean() ? -1 : 1) * r.nextFloat());
}
entity.time += 1;
break;
}
}

@Override
Expand Down Expand Up @@ -254,8 +262,8 @@ public void cleanUp(World world){
}
}


public ItemStack getItem(){return this.item;}
public ItemStack getBook (){return this.book;}
public float getBookOpenAngle (){return this.bookOpenAngle;}
public float getBookAngle (){return this.bookAngle;}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,40 +29,36 @@ public InfusionTableEntityRenderer(BlockEntityRendererFactory.Context ctx) {

@Override
public void render(InfusionTableEntity entity, float tickDelta, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light, int overlay) {
Main.LOGGER.warn(tickDelta);
// if(!entity.getBook().isEmpty()){
if(!entity.getBook().isEmpty()){
matrices.push();

matrices.translate(0.5, 0.75, 0.5);
matrices.translate(0.0D, (double)(0.1F + MathHelper.sin((entity.getWorld().getTime() + tickDelta) * 0.1F) * 0.01F), 0.0D);


matrices.multiply(Vec3f.POSITIVE_Y.getRadialQuaternion( -(entity.bookLastRot + entity.bookRotForce * tickDelta) ));

matrices.multiply(Vec3f.POSITIVE_Y.getRadialQuaternion( -(entity.bookLastRot + Main.aroundRadial(entity.bookRot - entity.bookLastRot) * tickDelta) ));
matrices.multiply(Vec3f.POSITIVE_Z.getDegreesQuaternion(80.0F));

VertexConsumer vertexConsumer = BOOK_TEXTURE.getVertexConsumer(vertexConsumers, RenderLayer::getEntitySolid);
this.book.setPageAngles(1, 0, 0, entity.bookLastOpenAngle + (entity.bookOpenAngle - entity.bookLastOpenAngle) * tickDelta );

VertexConsumer vertexConsumer = BOOK_TEXTURE.getVertexConsumer(vertexConsumers, RenderLayer::getEntitySolid);
vertexConsumer = getBookGlintConsumer(vertexConsumers, vertexConsumer, !entity.getBook().isEnchantable());

this.book.setPageAngles(1, 0, 0, entity.getBookOpenAngle());

this.book.renderBook(matrices, vertexConsumer, light, overlay, 1.0F, 1.0F, 1.0F, 1.0F);

matrices.pop();

// if(entity.getBookOpenAngle() >= 1 && !entity.getItem().isEmpty()){
// matrices.push();
//
// matrices.translate(0.5, 1.2, 0.5);
//
// // Rotate the item
// matrices.multiply(Vec3f.POSITIVE_Y.getDegreesQuaternion((entity.getWorld().getTime() + tickDelta) * 2));
//
// MinecraftClient.getInstance().getItemRenderer().renderItem(entity.getItem(), ModelTransformation.Mode.GROUND, light, overlay, matrices, vertexConsumers, 0);
//
// matrices.pop();
// }
// }
if(entity.getBookOpenAngle() >= 1 && !entity.getItem().isEmpty()){
matrices.push();

matrices.translate(0.5, 1.2, 0.5);

// Rotate the item
matrices.multiply(Vec3f.POSITIVE_Y.getDegreesQuaternion((entity.getWorld().getTime() + tickDelta) * 2));

MinecraftClient.getInstance().getItemRenderer().renderItem(entity.getItem(), ModelTransformation.Mode.GROUND, light, overlay, matrices, vertexConsumers, 0);

matrices.pop();
}
}
}

static {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"type": "minecraft:crafting_shaped",
"pattern": [
"bbb",
" a ",
"ccc",
"aba",
"bbb"
],
"key": {
"a": {"item": "minecraft:copper_block"},
"b": {"item": "minecraft:cut_copper_slab"}
"a": {"item": "minecraft:amethyst_shard"},
"b": {"item": "minecraft:crying_obsidian"},
"c": {"item": "minecraft:polished_deepslate"}
},
"result": {
"item": "infusion_table:infusion_table"
Expand Down

0 comments on commit a043fb9

Please sign in to comment.