Search in sources :

Example 1 with Pointing

use of com.simibubi.create.foundation.utility.Pointing in project Create by Creators-of-Create.

the class MechanicalCrafterBlock method getStateForPlacement.

@Override
public BlockState getStateForPlacement(BlockPlaceContext context) {
    Direction face = context.getClickedFace();
    BlockPos placedOnPos = context.getClickedPos().relative(face.getOpposite());
    BlockState blockState = context.getLevel().getBlockState(placedOnPos);
    if ((blockState.getBlock() != this) || (context.getPlayer() != null && context.getPlayer().isShiftKeyDown())) {
        BlockState stateForPlacement = super.getStateForPlacement(context);
        Direction direction = stateForPlacement.getValue(HORIZONTAL_FACING);
        if (direction != face)
            stateForPlacement = stateForPlacement.setValue(POINTING, pointingFromFacing(face, direction));
        return stateForPlacement;
    }
    Direction otherFacing = blockState.getValue(HORIZONTAL_FACING);
    Pointing pointing = pointingFromFacing(face, otherFacing);
    return defaultBlockState().setValue(HORIZONTAL_FACING, otherFacing).setValue(POINTING, pointing);
}
Also used : Pointing(com.simibubi.create.foundation.utility.Pointing) BlockState(net.minecraft.world.level.block.state.BlockState) BlockPos(net.minecraft.core.BlockPos) Direction(net.minecraft.core.Direction) AxisDirection(net.minecraft.core.Direction.AxisDirection)

Example 2 with Pointing

use of com.simibubi.create.foundation.utility.Pointing in project Create by Creators-of-Create.

the class MechanicalCrafterBlock method pointingFromFacing.

public static Pointing pointingFromFacing(Direction pointingFace, Direction blockFacing) {
    boolean positive = blockFacing.getAxisDirection() == AxisDirection.POSITIVE;
    Pointing pointing = pointingFace == Direction.DOWN ? Pointing.UP : Pointing.DOWN;
    if (pointingFace == Direction.EAST)
        pointing = positive ? Pointing.LEFT : Pointing.RIGHT;
    if (pointingFace == Direction.WEST)
        pointing = positive ? Pointing.RIGHT : Pointing.LEFT;
    if (pointingFace == Direction.NORTH)
        pointing = positive ? Pointing.LEFT : Pointing.RIGHT;
    if (pointingFace == Direction.SOUTH)
        pointing = positive ? Pointing.RIGHT : Pointing.LEFT;
    return pointing;
}
Also used : Pointing(com.simibubi.create.foundation.utility.Pointing)

Example 3 with Pointing

use of com.simibubi.create.foundation.utility.Pointing in project Create by Creators-of-Create.

the class MechanicalCrafterRenderer method renderItems.

public void renderItems(MechanicalCrafterTileEntity te, float partialTicks, PoseStack ms, MultiBufferSource buffer, int light, int overlay) {
    if (te.phase == Phase.IDLE) {
        ItemStack stack = te.getInventory().getItem(0);
        if (!stack.isEmpty()) {
            ms.pushPose();
            ms.translate(0, 0, -1 / 256f);
            ms.mulPose(Vector3f.YP.rotationDegrees(180));
            Minecraft.getInstance().getItemRenderer().renderStatic(stack, TransformType.FIXED, light, overlay, ms, buffer, 0);
            ms.popPose();
        }
    } else {
        // render grouped items
        GroupedItems items = te.groupedItems;
        float distance = .5f;
        ms.pushPose();
        if (te.phase == Phase.CRAFTING) {
            items = te.groupedItemsBeforeCraft;
            items.calcStats();
            float progress = Mth.clamp((2000 - te.countDown + te.getCountDownSpeed() * partialTicks) / 1000f, 0, 1);
            float earlyProgress = Mth.clamp(progress * 2, 0, 1);
            float lateProgress = Mth.clamp(progress * 2 - 1, 0, 1);
            ms.scale(1 - lateProgress, 1 - lateProgress, 1 - lateProgress);
            Vec3 centering = new Vec3(-items.minX + (-items.width + 1) / 2f, -items.minY + (-items.height + 1) / 2f, 0).scale(earlyProgress);
            ms.translate(centering.x * .5f, centering.y * .5f, 0);
            distance += (-4 * (progress - .5f) * (progress - .5f) + 1) * .25f;
        }
        boolean onlyRenderFirst = te.phase == Phase.INSERTING || te.phase == Phase.CRAFTING && te.countDown < 1000;
        final float spacing = distance;
        items.grid.forEach((pair, stack) -> {
            if (onlyRenderFirst && (pair.getLeft().intValue() != 0 || pair.getRight().intValue() != 0))
                return;
            ms.pushPose();
            Integer x = pair.getKey();
            Integer y = pair.getValue();
            ms.translate(x * spacing, y * spacing, 0);
            int offset = 0;
            if (te.phase == Phase.EXPORTING && te.getBlockState().hasProperty(MechanicalCrafterBlock.POINTING)) {
                Pointing value = te.getBlockState().getValue(MechanicalCrafterBlock.POINTING);
                offset = value == Pointing.UP ? -1 : value == Pointing.LEFT ? 2 : value == Pointing.RIGHT ? -2 : 1;
            }
            TransformStack.cast(ms).rotateY(180).translate(0, 0, (x + y * 3 + offset * 9) / 1024f);
            Minecraft.getInstance().getItemRenderer().renderStatic(stack, TransformType.FIXED, light, overlay, ms, buffer, 0);
            ms.popPose();
        });
        ms.popPose();
        if (te.phase == Phase.CRAFTING) {
            items = te.groupedItems;
            float progress = Mth.clamp((1000 - te.countDown + te.getCountDownSpeed() * partialTicks) / 1000f, 0, 1);
            float earlyProgress = Mth.clamp(progress * 2, 0, 1);
            float lateProgress = Mth.clamp(progress * 2 - 1, 0, 1);
            ms.mulPose(Vector3f.ZP.rotationDegrees(earlyProgress * 2 * 360));
            float upScaling = earlyProgress * 1.125f;
            float downScaling = 1 + (1 - lateProgress) * .125f;
            ms.scale(upScaling, upScaling, upScaling);
            ms.scale(downScaling, downScaling, downScaling);
            items.grid.forEach((pair, stack) -> {
                if (pair.getLeft().intValue() != 0 || pair.getRight().intValue() != 0)
                    return;
                ms.pushPose();
                ms.mulPose(Vector3f.YP.rotationDegrees(180));
                Minecraft.getInstance().getItemRenderer().renderStatic(stack, TransformType.FIXED, light, overlay, ms, buffer, 0);
                ms.popPose();
            });
        }
    }
}
Also used : Pointing(com.simibubi.create.foundation.utility.Pointing) GroupedItems(com.simibubi.create.content.contraptions.components.crafter.RecipeGridHandler.GroupedItems) Vec3(net.minecraft.world.phys.Vec3) ItemStack(net.minecraft.world.item.ItemStack)

Example 4 with Pointing

use of com.simibubi.create.foundation.utility.Pointing in project Create by Creators-of-Create.

the class MechanicalCrafterTileEntity method tick.

@Override
public void tick() {
    super.tick();
    if (phase == Phase.ACCEPTING)
        return;
    boolean onClient = level.isClientSide;
    boolean runLogic = !onClient || isVirtual();
    if (wasPoweredBefore != level.hasNeighborSignal(worldPosition)) {
        wasPoweredBefore = level.hasNeighborSignal(worldPosition);
        if (wasPoweredBefore) {
            if (!runLogic)
                return;
            checkCompletedRecipe(true);
        }
    }
    if (phase == Phase.ASSEMBLING) {
        countDown -= getCountDownSpeed();
        if (countDown < 0) {
            countDown = 0;
            if (!runLogic)
                return;
            if (RecipeGridHandler.getTargetingCrafter(this) != null) {
                phase = Phase.EXPORTING;
                countDown = 1000;
                sendData();
                return;
            }
            ItemStack result = isVirtual() ? scriptedResult : RecipeGridHandler.tryToApplyRecipe(level, groupedItems);
            if (result != null) {
                List<ItemStack> containers = new ArrayList<>();
                groupedItems.grid.values().forEach(stack -> {
                    if (stack.hasContainerItem())
                        containers.add(stack.getContainerItem().copy());
                });
                if (isVirtual())
                    groupedItemsBeforeCraft = groupedItems;
                groupedItems = new GroupedItems(result);
                for (int i = 0; i < containers.size(); i++) {
                    ItemStack stack = containers.get(i);
                    GroupedItems container = new GroupedItems();
                    container.grid.put(Pair.of(i, 0), stack);
                    container.mergeOnto(groupedItems, Pointing.LEFT);
                }
                phase = Phase.CRAFTING;
                countDown = 2000;
                sendData();
                return;
            }
            ejectWholeGrid();
            return;
        }
    }
    if (phase == Phase.EXPORTING) {
        countDown -= getCountDownSpeed();
        if (countDown < 0) {
            countDown = 0;
            if (!runLogic)
                return;
            MechanicalCrafterTileEntity targetingCrafter = RecipeGridHandler.getTargetingCrafter(this);
            if (targetingCrafter == null) {
                ejectWholeGrid();
                return;
            }
            Pointing pointing = getBlockState().getValue(MechanicalCrafterBlock.POINTING);
            groupedItems.mergeOnto(targetingCrafter.groupedItems, pointing);
            groupedItems = new GroupedItems();
            float pitch = targetingCrafter.groupedItems.grid.size() * 1 / 16f + .5f;
            AllSoundEvents.CRAFTER_CLICK.playOnServer(level, worldPosition, 1, pitch);
            phase = Phase.WAITING;
            countDown = 0;
            sendData();
            targetingCrafter.continueIfAllPrecedingFinished();
            targetingCrafter.sendData();
            return;
        }
    }
    if (phase == Phase.CRAFTING) {
        if (onClient) {
            Direction facing = getBlockState().getValue(MechanicalCrafterBlock.HORIZONTAL_FACING);
            float progress = countDown / 2000f;
            Vec3 facingVec = Vec3.atLowerCornerOf(facing.getNormal());
            Vec3 vec = facingVec.scale(.65).add(VecHelper.getCenterOf(worldPosition));
            Vec3 offset = VecHelper.offsetRandomly(Vec3.ZERO, level.random, .125f).multiply(VecHelper.axisAlingedPlaneOf(facingVec)).normalize().scale(progress * .5f).add(vec);
            if (progress > .5f)
                level.addParticle(ParticleTypes.CRIT, offset.x, offset.y, offset.z, 0, 0, 0);
            if (!groupedItemsBeforeCraft.grid.isEmpty() && progress < .5f) {
                if (groupedItems.grid.containsKey(Pair.of(0, 0))) {
                    ItemStack stack = groupedItems.grid.get(Pair.of(0, 0));
                    groupedItemsBeforeCraft = new GroupedItems();
                    for (int i = 0; i < 10; i++) {
                        Vec3 randVec = VecHelper.offsetRandomly(Vec3.ZERO, level.random, .125f).multiply(VecHelper.axisAlingedPlaneOf(facingVec)).normalize().scale(.25f);
                        Vec3 offset2 = randVec.add(vec);
                        randVec = randVec.scale(.35f);
                        level.addParticle(new ItemParticleOption(ParticleTypes.ITEM, stack), offset2.x, offset2.y, offset2.z, randVec.x, randVec.y, randVec.z);
                    }
                }
            }
        }
        int prev = countDown;
        countDown -= getCountDownSpeed();
        if (countDown < 1000 && prev >= 1000) {
            AllSoundEvents.CRAFTER_CLICK.playOnServer(level, worldPosition, 1, 2);
            AllSoundEvents.CRAFTER_CRAFT.playOnServer(level, worldPosition);
        }
        if (countDown < 0) {
            countDown = 0;
            if (!runLogic)
                return;
            tryInsert();
            return;
        }
    }
    if (phase == Phase.INSERTING) {
        if (runLogic && isTargetingBelt())
            tryInsert();
        return;
    }
}
Also used : Pointing(com.simibubi.create.foundation.utility.Pointing) GroupedItems(com.simibubi.create.content.contraptions.components.crafter.RecipeGridHandler.GroupedItems) Vec3(net.minecraft.world.phys.Vec3) ArrayList(java.util.ArrayList) ItemParticleOption(net.minecraft.core.particles.ItemParticleOption) ItemStack(net.minecraft.world.item.ItemStack) Direction(net.minecraft.core.Direction)

Example 5 with Pointing

use of com.simibubi.create.foundation.utility.Pointing in project Create by Creators-of-Create.

the class MechanicalCrafterBlock method getTargetDirection.

public static Direction getTargetDirection(BlockState state) {
    if (!AllBlocks.MECHANICAL_CRAFTER.has(state))
        return Direction.UP;
    Direction facing = state.getValue(HORIZONTAL_FACING);
    Pointing point = state.getValue(POINTING);
    Vec3 targetVec = new Vec3(0, 1, 0);
    targetVec = VecHelper.rotate(targetVec, -point.getXRotation(), Axis.Z);
    targetVec = VecHelper.rotate(targetVec, AngleHelper.horizontalAngle(facing), Axis.Y);
    return Direction.getNearest(targetVec.x, targetVec.y, targetVec.z);
}
Also used : Pointing(com.simibubi.create.foundation.utility.Pointing) Vec3(net.minecraft.world.phys.Vec3) Direction(net.minecraft.core.Direction) AxisDirection(net.minecraft.core.Direction.AxisDirection)

Aggregations

Pointing (com.simibubi.create.foundation.utility.Pointing)5 Direction (net.minecraft.core.Direction)3 Vec3 (net.minecraft.world.phys.Vec3)3 GroupedItems (com.simibubi.create.content.contraptions.components.crafter.RecipeGridHandler.GroupedItems)2 AxisDirection (net.minecraft.core.Direction.AxisDirection)2 ItemStack (net.minecraft.world.item.ItemStack)2 ArrayList (java.util.ArrayList)1 BlockPos (net.minecraft.core.BlockPos)1 ItemParticleOption (net.minecraft.core.particles.ItemParticleOption)1 BlockState (net.minecraft.world.level.block.state.BlockState)1