Search in sources :

Example 6 with EntityShipMovementData

use of org.valkyrienskies.mod.common.entity.EntityShipMovementData in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.

the class BlockBoatChair method onBlockActivated.

@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) {
    if (!worldIn.isRemote) {
        Optional<PhysicsObject> physicsObject = ValkyrienUtils.getPhysoManagingBlock(worldIn, pos);
        if (physicsObject.isPresent()) {
            TileEntity tileEntity = worldIn.getTileEntity(pos);
            if (tileEntity instanceof TileEntityBoatChair) {
                Vector3d playerPos = new Vector3d(playerIn.posX, playerIn.posY, playerIn.posZ);
                physicsObject.get().getShipTransformationManager().getCurrentTickTransform().transformPosition(playerPos, TransformType.SUBSPACE_TO_GLOBAL);
                playerIn.posX = playerPos.x;
                playerIn.posY = playerPos.y;
                playerIn.posZ = playerPos.z;
                // Only mount the player if they're standing on the ship.
                final EntityShipMovementData entityShipMovementData = ValkyrienUtils.getEntityShipMovementDataFor(playerIn);
                if (entityShipMovementData.getTicksSinceTouchedShip() == 0 && (entityShipMovementData.getLastTouchedShip() == physicsObject.get().getShipData())) {
                    Vector3dc localMountPos = getPlayerMountOffset(state, pos);
                    ValkyrienUtils.fixEntityToShip(playerIn, localMountPos, physicsObject.get());
                }
                ((TileEntityBoatChair) tileEntity).setPilotEntity(playerIn);
                physicsObject.get().getShipTransformationManager().getCurrentTickTransform().transformPosition(playerPos, TransformType.GLOBAL_TO_SUBSPACE);
                playerIn.posX = playerPos.x;
                playerIn.posY = playerPos.y;
                playerIn.posZ = playerPos.z;
            }
        }
    }
    return true;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) EntityShipMovementData(org.valkyrienskies.mod.common.entity.EntityShipMovementData) Vector3dc(org.joml.Vector3dc) Vector3d(org.joml.Vector3d) TileEntityBoatChair(org.valkyrienskies.mod.common.tileentity.TileEntityBoatChair) PhysicsObject(org.valkyrienskies.mod.common.ships.ship_world.PhysicsObject)

Example 7 with EntityShipMovementData

use of org.valkyrienskies.mod.common.entity.EntityShipMovementData in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.

the class EntityDraggable method addEntityVelocityFromShipBelow.

/**
 * Adds the ship below velocity to entity.
 */
private static void addEntityVelocityFromShipBelow(final Entity entity) {
    final IDraggable draggable = EntityDraggable.getDraggableFromEntity(entity);
    final EntityShipMountData mountData = ValkyrienUtils.getMountedShipAndPos(entity);
    final EntityShipMovementData oldEntityShipMovementData = draggable.getEntityShipMovementData();
    final ShipData lastShipTouchedPlayer = oldEntityShipMovementData.getLastTouchedShip();
    final int oldTicksSinceTouchedShip = oldEntityShipMovementData.getTicksSinceTouchedShip();
    final Vector3dc oldVelocityAdded = oldEntityShipMovementData.getAddedLinearVelocity();
    final double oldYawVelocityAdded = oldEntityShipMovementData.getAddedYawVelocity();
    if (lastShipTouchedPlayer == null || oldTicksSinceTouchedShip >= VSConfig.ticksToStickToShip) {
        if (entity.onGround) {
            // Player is on ground and not on a ship, therefore set their added velocity to 0.
            draggable.setEntityShipMovementData(oldEntityShipMovementData.withAddedLinearVelocity(new Vector3d()).withAddedYawVelocity(0));
        } else {
            if (entity instanceof EntityPlayer) {
                EntityPlayer player = (EntityPlayer) entity;
                if (player.isCreative() && player.capabilities.isFlying) {
                    // If the player is flying, then slow down their added velocity significantly every tick
                    final Vector3dc newVelocityAdded = oldVelocityAdded.mul(.95, new Vector3d());
                    final double newYawVelocityAdded = oldYawVelocityAdded * .95 * .95;
                    final EntityShipMovementData newMovementData = oldEntityShipMovementData.withAddedLinearVelocity(newVelocityAdded).withAddedYawVelocity(newYawVelocityAdded);
                    draggable.setEntityShipMovementData(newMovementData);
                } else {
                    // Otherwise only slow down their added velocity slightly every tick
                    final Vector3dc newVelocityAdded = oldVelocityAdded.mul(.99, new Vector3d());
                    final double newYawVelocityAdded = oldYawVelocityAdded * .95;
                    final EntityShipMovementData newMovementData = oldEntityShipMovementData.withAddedLinearVelocity(newVelocityAdded).withAddedYawVelocity(newYawVelocityAdded);
                    draggable.setEntityShipMovementData(newMovementData);
                }
            }
        }
    } else {
        final float rotYaw = entity.rotationYaw;
        final float rotPitch = entity.rotationPitch;
        final float prevYaw = entity.prevRotationYaw;
        final float prevPitch = entity.prevRotationPitch;
        final Vector3dc oldPos = new Vector3d(entity.posX, entity.posY, entity.posZ);
        final Matrix4d betweenTransform = ShipTransform.createTransform(lastShipTouchedPlayer.getPrevTickShipTransform(), lastShipTouchedPlayer.getShipTransform());
        ValkyrienUtils.transformEntity(betweenTransform, entity, false);
        final Vector3dc newPos = new Vector3d(entity.posX, entity.posY, entity.posZ);
        // Move the entity back to its old position, the added velocity will be used
        // afterwards
        entity.setPosition(oldPos.x(), oldPos.y(), oldPos.z());
        final Vector3dc addedVel = newPos.sub(oldPos, new Vector3d());
        // Now compute the added yaw velocity
        entity.rotationYaw = rotYaw;
        entity.rotationPitch = rotPitch;
        entity.prevRotationYaw = prevYaw;
        entity.prevRotationPitch = prevPitch;
        // Ignore the pitch, calculate the look vector using only the yaw
        final Vector3d newLookYawVec;
        if (entity instanceof EntityLivingBase && !(entity instanceof EntityPlayer)) {
            newLookYawVec = new Vector3d(-MathHelper.sin(-entity.getRotationYawHead() * 0.017453292F - (float) Math.PI), 0, -MathHelper.cos(-entity.getRotationYawHead() * 0.017453292F - (float) Math.PI));
        } else {
            newLookYawVec = new Vector3d(-MathHelper.sin(-entity.rotationYaw * 0.017453292F - (float) Math.PI), 0, -MathHelper.cos(-entity.rotationYaw * 0.017453292F - (float) Math.PI));
        }
        // Transform the player look vector
        betweenTransform.transformDirection(newLookYawVec);
        // Calculate the yaw of the transformed player look vector
        final Tuple<Double, Double> newPlayerLookYawOnly = VSMath.getPitchYawFromVector(newLookYawVec);
        final double wrappedYaw = MathHelper.wrapDegrees(newPlayerLookYawOnly.getSecond());
        final double wrappedRotYaw;
        // [Changed because EntityPlayerSP is a 'client' class]
        if (entity instanceof EntityLivingBase && !(entity instanceof EntityPlayer)) {
            wrappedRotYaw = MathHelper.wrapDegrees(entity.getRotationYawHead());
        } else {
            wrappedRotYaw = MathHelper.wrapDegrees(entity.rotationYaw);
        }
        double yawDif = wrappedYaw - wrappedRotYaw;
        if (Math.abs(yawDif) > 180D) {
            if (yawDif < 0) {
                yawDif += 360D;
            } else {
                yawDif -= 360D;
            }
        }
        yawDif %= 360D;
        final double threshold = .1D;
        if (Math.abs(yawDif) < threshold) {
            yawDif = 0D;
        }
        draggable.setEntityShipMovementData(oldEntityShipMovementData.withAddedLinearVelocity(addedVel.mul(1, new Vector3d())).withAddedYawVelocity(yawDif));
    }
    final EntityShipMovementData newEntityShipMovementData = draggable.getEntityShipMovementData();
    // it unless we have to.
    if (newEntityShipMovementData.getAddedLinearVelocity().lengthSquared() > 0) {
        // Now that we've determined the added velocity, move the entity forward by that amount
        final boolean originallySneaking = entity.isSneaking();
        entity.setSneaking(false);
        // The added velocity vector of the player, except we have made sure that it won't push the player inside of a
        // solid block.
        final Vector3dc addedVelocityNoNoClip = applyAddedVelocity(newEntityShipMovementData.getAddedLinearVelocity(), entity);
        draggable.setEntityShipMovementData(oldEntityShipMovementData.withAddedLinearVelocity(addedVelocityNoNoClip));
        entity.setSneaking(originallySneaking);
    }
    // Add the yaw velocity to the player as well, because its possible for addedVelocity=0 and yawVel != 0
    final double addedYawVelocity = newEntityShipMovementData.getAddedYawVelocity();
    if (!mountData.isMounted() && addedYawVelocity != 0) {
        entity.setRotationYawHead((float) (entity.getRotationYawHead() + addedYawVelocity));
        entity.rotationYaw += addedYawVelocity;
    }
}
Also used : EntityShipMovementData(org.valkyrienskies.mod.common.entity.EntityShipMovementData) ShipData(org.valkyrienskies.mod.common.ships.ShipData) Vector3dc(org.joml.Vector3dc) Matrix4d(org.joml.Matrix4d) Vector3d(org.joml.Vector3d) EntityLivingBase(net.minecraft.entity.EntityLivingBase) EntityPlayer(net.minecraft.entity.player.EntityPlayer)

Aggregations

Vector3d (org.joml.Vector3d)7 EntityShipMovementData (org.valkyrienskies.mod.common.entity.EntityShipMovementData)7 Vector3dc (org.joml.Vector3dc)5 PhysicsObject (org.valkyrienskies.mod.common.ships.ship_world.PhysicsObject)4 ShipTransform (org.valkyrienskies.mod.common.ships.ship_transform.ShipTransform)3 EntityLivingBase (net.minecraft.entity.EntityLivingBase)2 EntityPlayer (net.minecraft.entity.player.EntityPlayer)2 TileEntity (net.minecraft.tileentity.TileEntity)2 World (net.minecraft.world.World)2 ShipData (org.valkyrienskies.mod.common.ships.ShipData)2 UUID (java.util.UUID)1 Nullable (javax.annotation.Nullable)1 IBlockState (net.minecraft.block.state.IBlockState)1 EntityPlayerSP (net.minecraft.client.entity.EntityPlayerSP)1 Entity (net.minecraft.entity.Entity)1 EnumFacing (net.minecraft.util.EnumFacing)1 AxisAlignedBB (net.minecraft.util.math.AxisAlignedBB)1 BlockPos (net.minecraft.util.math.BlockPos)1 Vec3d (net.minecraft.util.math.Vec3d)1 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)1