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();
}
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;
}
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);
}
}
Aggregations