Skip to content

Commit

Permalink
feat: rotatable bowls (#1111)
Browse files Browse the repository at this point in the history
* feat: allow sacrificial bowls to be rotated

* feat: make item bob in facing direction

* fix: improve item bobbing location

* fix: final fix to item bobbing location
  • Loading branch information
klikli-dev authored Apr 11, 2024
1 parent 831deb4 commit b181f5d
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"variants": {
"facing=down": {
"model": "occultism:block/sacrificial_bowl",
"x": 180
},
"facing=east": {
"model": "occultism:block/sacrificial_bowl",
"x": 90,
"y": 90
},
"facing=north": {
"model": "occultism:block/sacrificial_bowl",
"x": 90
},
"facing=south": {
"model": "occultism:block/sacrificial_bowl",
"x": 90,
"y": 180
},
"facing=up": {
"model": "occultism:block/sacrificial_bowl"
},
"facing=west": {
"model": "occultism:block/sacrificial_bowl",
"x": 90,
"y": 270
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@
import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;
import net.minecraft.client.renderer.entity.ItemRenderer;
import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.core.Direction;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.ItemDisplayContext;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.phys.Vec3;
import org.joml.Vector3f;

public class SacrificialBowlRenderer implements BlockEntityRenderer<SacrificialBowlBlockEntity> {

Expand All @@ -57,12 +61,29 @@ public void render(SacrificialBowlBlockEntity blockEntity, float partialTicks, P
blockEntity.itemStackHandler.ifPresent(handler -> {
ItemStack stack = handler.getStackInSlot(0);
long time = blockEntity.getLevel().getGameTime();

var facing = blockEntity.getBlockState().hasProperty(BlockStateProperties.FACING) ?
blockEntity.getBlockState().getValue(BlockStateProperties.FACING) : Direction.UP;

poseStack.pushPose();

//TODO: Currently the items bob up and down, instead of away from the bowl facing and back

poseStack.pushPose();

//slowly bob up and down following a sine
double offset = Math.sin((time - blockEntity.lastChangeTime + partialTicks) / 16) * 0.5f + 0.5f; // * 0.5f + 0.5f; move sine between 0.0-1.0
offset = offset / 4.0f; //reduce amplitude
poseStack.translate(0.5, 0.6 + offset, 0.5);

// Fixed offset to push the item away from the bowl
double fixedOffset = 0.2;

// Adjust the translation based on the facing direction
double xOffset = facing.getAxis() == Direction.Axis.X ? (facing.getAxisDirection() == Direction.AxisDirection.POSITIVE ? offset + fixedOffset : -offset - fixedOffset) : 0.0;
double yOffset = facing.getAxis() == Direction.Axis.Y ? (facing.getAxisDirection() == Direction.AxisDirection.POSITIVE ? offset + fixedOffset : -offset - fixedOffset) : 0.0;
double zOffset = facing.getAxis() == Direction.Axis.Z ? (facing.getAxisDirection() == Direction.AxisDirection.POSITIVE ? offset + fixedOffset : -offset - fixedOffset) : 0.0;

poseStack.translate(0.5 + xOffset, 0.5 + yOffset, 0.5 + zOffset);

//use system time to become independent of game time
long systemTime = System.currentTimeMillis();
Expand All @@ -80,6 +101,10 @@ public void render(SacrificialBowlBlockEntity blockEntity, float partialTicks, P
combinedLight, combinedOverlay, model);

poseStack.popPose();

poseStack.mulPose(facing.getRotation());

poseStack.popPose();
});
}
//endregion Static Methods
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.klikli_dev.occultism.common.block;

import net.minecraft.core.Direction;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.phys.shapes.VoxelShape;

public class DirectionalBlockShape {

protected final float length;
protected final float width;
protected final float height;
protected final float center;
protected final VoxelShape up;
protected final VoxelShape down;
protected final VoxelShape west;
protected final VoxelShape east;
protected final VoxelShape north;
protected final VoxelShape south;


public DirectionalBlockShape(float length, float width, float height) {
this(length, width, height, 8.0F);
}

public DirectionalBlockShape(float length, float width, float height, float center) {
this.length = length;
this.width = width;
this.height = height;
this.center = center;

this.up = Block.box(center - width / 2, 0.0F, center - length / 2, center + width / 2, height, center + length / 2);
this.down = Block.box(center - width / 2, 16.0F - height, center - length / 2, center + width / 2, 16.0F, center + length / 2);
this.west = Block.box(16.0F - height, center - width / 2, center - length / 2, 16.0F, center + width / 2, center + length / 2);
this.east = Block.box(0.0F, center - width / 2, center - length / 2, height, center + width / 2, center + length / 2);
this.north = Block.box(center - width / 2, center - length / 2, 16.0F - height, center + width / 2, center + length / 2, 16.0F);
this.south = Block.box(center - width / 2, center - length / 2, 0.0F, center + width / 2, center + length / 2, height);
}

public VoxelShape getShape(Direction direction) {
return switch (direction) {
case UP -> this.up;
case DOWN -> this.down;
case WEST -> this.west;
case EAST -> this.east;
case NORTH -> this.north;
case SOUTH -> this.south;
};
}

public VoxelShape up() {
return this.up;
}

public VoxelShape down() {
return this.down;
}

public VoxelShape west() {
return this.west;
}

public VoxelShape east() {
return this.east;
}

public VoxelShape north() {
return this.north;
}

public VoxelShape south() {
return this.south;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,22 @@
import com.klikli_dev.occultism.registry.OccultismTiles;
import com.klikli_dev.occultism.util.StorageUtil;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.DirectionalBlock;
import net.minecraft.world.level.block.EntityBlock;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.pathfinder.PathComputationType;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.shapes.CollisionContext;
Expand All @@ -47,12 +51,16 @@

import javax.annotation.Nullable;

public class SacrificialBowlBlock extends Block implements EntityBlock {
public class SacrificialBowlBlock extends DirectionalBlock implements EntityBlock {

private static final VoxelShape SHAPE = Block.box(4, 0, 4, 12, 2.3, 12);
private static final DirectionalBlockShape SHAPE = new DirectionalBlockShape(12, 12, 2.3f);

//TODO Rotate renderer

public SacrificialBowlBlock(Properties properties) {
super(properties);

this.registerDefaultState(this.stateDefinition.any().setValue(FACING, Direction.UP));
}

@Override
Expand Down Expand Up @@ -108,7 +116,21 @@ public InteractionResult use(BlockState state, Level level, BlockPos pos, Player
@Override
@SuppressWarnings("deprecation")
public VoxelShape getShape(BlockState state, BlockGetter worldIn, BlockPos pos, CollisionContext context) {
return SHAPE;
return SHAPE.getShape(state.getValue(FACING));
}

@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> pBuilder) {
pBuilder.add(FACING);
}

@Override
public BlockState getStateForPlacement(BlockPlaceContext pContext) {
Direction direction = pContext.getClickedFace();
BlockState blockstate = pContext.getLevel().getBlockState(pContext.getClickedPos().relative(direction.getOpposite()));
return blockstate.is(this) && blockstate.getValue(FACING) == direction
? this.defaultBlockState().setValue(FACING, direction.getOpposite())
: this.defaultBlockState().setValue(FACING, direction);
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ protected void registerStatesAndModels() {
this.models().getExistingFile(this.modLoc("block/storage_controller_base")));
this.models().withExistingParent("item/storage_controller_base", this.modLoc("block/storage_controller_base"));
this.generateStableWormholeState(OccultismBlocks.STABLE_WORMHOLE.get());
this.directionalBlock(OccultismBlocks.SACRIFICIAL_BOWL.get(),
this.models().getExistingFile(this.modLoc("block/sacrificial_bowl")));
this.directionalBlock(OccultismBlocks.STORAGE_STABILIZER_TIER1.get(),
this.models().getExistingFile(this.modLoc("block/storage_stabilizer_tier1")));
this.directionalBlock(OccultismBlocks.STORAGE_STABILIZER_TIER2.get(),
Expand Down

This file was deleted.

0 comments on commit b181f5d

Please sign in to comment.