Search in sources :

Example 1 with StabilizedContraption

use of com.simibubi.create.content.contraptions.components.structureMovement.bearing.StabilizedContraption in project Create by Creators-of-Create.

the class Contraption method onEntityCreated.

public void onEntityCreated(AbstractContraptionEntity entity) {
    this.entity = entity;
    // Create subcontraptions
    for (BlockFace blockFace : pendingSubContraptions) {
        Direction face = blockFace.getFace();
        StabilizedContraption subContraption = new StabilizedContraption(face);
        Level world = entity.level;
        BlockPos pos = blockFace.getPos();
        try {
            if (!subContraption.assemble(world, pos))
                continue;
        } catch (AssemblyException e) {
            continue;
        }
        subContraption.removeBlocksFromWorld(world, BlockPos.ZERO);
        OrientedContraptionEntity movedContraption = OrientedContraptionEntity.create(world, subContraption, face);
        BlockPos anchor = blockFace.getConnectedPos();
        movedContraption.setPos(anchor.getX() + .5f, anchor.getY(), anchor.getZ() + .5f);
        world.addFreshEntity(movedContraption);
        stabilizedSubContraptions.put(movedContraption.getUUID(), new BlockFace(toLocalPos(pos), face));
    }
    // Gather itemhandlers of mounted storage
    List<IItemHandlerModifiable> list = storage.values().stream().map(MountedStorage::getItemHandler).collect(Collectors.toList());
    inventory = new ContraptionInvWrapper(Arrays.copyOf(list.toArray(), list.size(), IItemHandlerModifiable[].class));
    List<IFluidHandler> fluidHandlers = fluidStorage.values().stream().map(MountedFluidStorage::getFluidHandler).collect(Collectors.toList());
    fluidInventory = new CombinedTankWrapper(Arrays.copyOf(fluidHandlers.toArray(), fluidHandlers.size(), IFluidHandler[].class));
    gatherBBsOffThread();
}
Also used : BlockFace(com.simibubi.create.foundation.utility.BlockFace) Direction(net.minecraft.core.Direction) StabilizedContraption(com.simibubi.create.content.contraptions.components.structureMovement.bearing.StabilizedContraption) IFluidHandler(net.minecraftforge.fluids.capability.IFluidHandler) IItemHandlerModifiable(net.minecraftforge.items.IItemHandlerModifiable) Level(net.minecraft.world.level.Level) ServerLevel(net.minecraft.server.level.ServerLevel) BlockPos(net.minecraft.core.BlockPos) CombinedTankWrapper(com.simibubi.create.foundation.fluid.CombinedTankWrapper)

Example 2 with StabilizedContraption

use of com.simibubi.create.content.contraptions.components.structureMovement.bearing.StabilizedContraption in project Create by Creators-of-Create.

the class OrientedContraptionEntity method updateOrientation.

protected boolean updateOrientation(boolean rotationLock, boolean wasStalled, Entity riding, boolean isOnCoupling) {
    if (isOnCoupling) {
        Couple<MinecartController> coupledCarts = getCoupledCartsIfPresent();
        if (coupledCarts == null)
            return false;
        Vec3 positionVec = coupledCarts.getFirst().cart().position();
        Vec3 coupledVec = coupledCarts.getSecond().cart().position();
        double diffX = positionVec.x - coupledVec.x;
        double diffY = positionVec.y - coupledVec.y;
        double diffZ = positionVec.z - coupledVec.z;
        prevYaw = yaw;
        prevPitch = pitch;
        yaw = (float) (Mth.atan2(diffZ, diffX) * 180 / Math.PI);
        pitch = (float) (Math.atan2(diffY, Math.sqrt(diffX * diffX + diffZ * diffZ)) * 180 / Math.PI);
        if (getCouplingId().equals(riding.getUUID())) {
            pitch *= -1;
            yaw += 180;
        }
        return false;
    }
    if (contraption instanceof StabilizedContraption) {
        if (!(riding instanceof OrientedContraptionEntity))
            return false;
        StabilizedContraption stabilized = (StabilizedContraption) contraption;
        Direction facing = stabilized.getFacing();
        if (facing.getAxis().isVertical())
            return false;
        OrientedContraptionEntity parent = (OrientedContraptionEntity) riding;
        prevYaw = yaw;
        yaw = -parent.getViewYRot(1);
        return false;
    }
    prevYaw = yaw;
    if (wasStalled)
        return false;
    boolean rotating = false;
    Vec3 movementVector = riding.getDeltaMovement();
    Vec3 locationDiff = riding.position().subtract(riding.xo, riding.yo, riding.zo);
    if (!(riding instanceof AbstractMinecart))
        movementVector = locationDiff;
    Vec3 motion = movementVector.normalize();
    if (!rotationLock) {
        if (riding instanceof AbstractMinecart) {
            AbstractMinecart minecartEntity = (AbstractMinecart) riding;
            BlockPos railPosition = minecartEntity.getCurrentRailPosition();
            BlockState blockState = level.getBlockState(railPosition);
            if (blockState.getBlock() instanceof BaseRailBlock) {
                BaseRailBlock abstractRailBlock = (BaseRailBlock) blockState.getBlock();
                RailShape railDirection = abstractRailBlock.getRailDirection(blockState, level, railPosition, minecartEntity);
                motion = VecHelper.project(motion, MinecartSim2020.getRailVec(railDirection));
            }
        }
        if (motion.length() > 0) {
            targetYaw = yawFromVector(motion);
            if (targetYaw < 0)
                targetYaw += 360;
            if (yaw < 0)
                yaw += 360;
        }
        prevYaw = yaw;
        float maxApproachSpeed = (float) (motion.length() * 12f / (Math.max(1, getBoundingBox().getXsize() / 6f)));
        float yawHint = AngleHelper.getShortestAngleDiff(yaw, yawFromVector(locationDiff));
        float approach = AngleHelper.getShortestAngleDiff(yaw, targetYaw, yawHint);
        approach = Mth.clamp(approach, -maxApproachSpeed, maxApproachSpeed);
        yaw += approach;
        if (Math.abs(AngleHelper.getShortestAngleDiff(yaw, targetYaw)) < 1f)
            yaw = targetYaw;
        else
            rotating = true;
    }
    return rotating;
}
Also used : AbstractMinecart(net.minecraft.world.entity.vehicle.AbstractMinecart) MinecartController(com.simibubi.create.content.contraptions.components.structureMovement.train.capability.MinecartController) CapabilityMinecartController(com.simibubi.create.content.contraptions.components.structureMovement.train.capability.CapabilityMinecartController) Direction(net.minecraft.core.Direction) StabilizedContraption(com.simibubi.create.content.contraptions.components.structureMovement.bearing.StabilizedContraption) BlockState(net.minecraft.world.level.block.state.BlockState) BaseRailBlock(net.minecraft.world.level.block.BaseRailBlock) RailShape(net.minecraft.world.level.block.state.properties.RailShape) Vec3(net.minecraft.world.phys.Vec3) BlockPos(net.minecraft.core.BlockPos)

Example 3 with StabilizedContraption

use of com.simibubi.create.content.contraptions.components.structureMovement.bearing.StabilizedContraption in project Create by Creators-of-Create.

the class OrientedContraptionEntity method tickContraption.

@Override
protected void tickContraption() {
    Entity e = getVehicle();
    if (e == null)
        return;
    boolean rotationLock = false;
    boolean pauseWhileRotating = false;
    boolean wasStalled = isStalled();
    if (contraption instanceof MountedContraption) {
        MountedContraption mountedContraption = (MountedContraption) contraption;
        rotationLock = mountedContraption.rotationMode == CartMovementMode.ROTATION_LOCKED;
        pauseWhileRotating = mountedContraption.rotationMode == CartMovementMode.ROTATE_PAUSED;
    }
    Entity riding = e;
    while (riding.getVehicle() != null && !(contraption instanceof StabilizedContraption)) riding = riding.getVehicle();
    boolean isOnCoupling = false;
    UUID couplingId = getCouplingId();
    isOnCoupling = couplingId != null && riding instanceof AbstractMinecart;
    if (!attachedExtraInventories) {
        attachInventoriesFromRidingCarts(riding, isOnCoupling, couplingId);
        attachedExtraInventories = true;
    }
    boolean rotating = updateOrientation(rotationLock, wasStalled, riding, isOnCoupling);
    if (!rotating || !pauseWhileRotating)
        tickActors();
    boolean isStalled = isStalled();
    LazyOptional<MinecartController> capability = riding.getCapability(CapabilityMinecartController.MINECART_CONTROLLER_CAPABILITY);
    if (capability.isPresent()) {
        if (!level.isClientSide())
            capability.orElse(null).setStalledExternally(isStalled);
    } else {
        if (isStalled) {
            if (!wasStalled)
                motionBeforeStall = riding.getDeltaMovement();
            riding.setDeltaMovement(0, 0, 0);
        }
        if (wasStalled && !isStalled) {
            riding.setDeltaMovement(motionBeforeStall);
            motionBeforeStall = Vec3.ZERO;
        }
    }
    if (level.isClientSide)
        return;
    if (!isStalled()) {
        if (isOnCoupling) {
            Couple<MinecartController> coupledCarts = getCoupledCartsIfPresent();
            if (coupledCarts == null)
                return;
            coupledCarts.map(MinecartController::cart).forEach(this::powerFurnaceCartWithFuelFromStorage);
            return;
        }
        powerFurnaceCartWithFuelFromStorage(riding);
    }
}
Also used : Entity(net.minecraft.world.entity.Entity) MountedContraption(com.simibubi.create.content.contraptions.components.structureMovement.mounted.MountedContraption) AbstractMinecart(net.minecraft.world.entity.vehicle.AbstractMinecart) UUID(java.util.UUID) MinecartController(com.simibubi.create.content.contraptions.components.structureMovement.train.capability.MinecartController) CapabilityMinecartController(com.simibubi.create.content.contraptions.components.structureMovement.train.capability.CapabilityMinecartController) StabilizedContraption(com.simibubi.create.content.contraptions.components.structureMovement.bearing.StabilizedContraption)

Aggregations

StabilizedContraption (com.simibubi.create.content.contraptions.components.structureMovement.bearing.StabilizedContraption)3 CapabilityMinecartController (com.simibubi.create.content.contraptions.components.structureMovement.train.capability.CapabilityMinecartController)2 MinecartController (com.simibubi.create.content.contraptions.components.structureMovement.train.capability.MinecartController)2 BlockPos (net.minecraft.core.BlockPos)2 Direction (net.minecraft.core.Direction)2 AbstractMinecart (net.minecraft.world.entity.vehicle.AbstractMinecart)2 MountedContraption (com.simibubi.create.content.contraptions.components.structureMovement.mounted.MountedContraption)1 CombinedTankWrapper (com.simibubi.create.foundation.fluid.CombinedTankWrapper)1 BlockFace (com.simibubi.create.foundation.utility.BlockFace)1 UUID (java.util.UUID)1 ServerLevel (net.minecraft.server.level.ServerLevel)1 Entity (net.minecraft.world.entity.Entity)1 Level (net.minecraft.world.level.Level)1 BaseRailBlock (net.minecraft.world.level.block.BaseRailBlock)1 BlockState (net.minecraft.world.level.block.state.BlockState)1 RailShape (net.minecraft.world.level.block.state.properties.RailShape)1 Vec3 (net.minecraft.world.phys.Vec3)1 IFluidHandler (net.minecraftforge.fluids.capability.IFluidHandler)1 IItemHandlerModifiable (net.minecraftforge.items.IItemHandlerModifiable)1