Search in sources :

Example 1 with Matrix4d

use of org.joml.Matrix4d 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

EntityLivingBase (net.minecraft.entity.EntityLivingBase)1 EntityPlayer (net.minecraft.entity.player.EntityPlayer)1 Matrix4d (org.joml.Matrix4d)1 Vector3d (org.joml.Vector3d)1 Vector3dc (org.joml.Vector3dc)1 EntityShipMovementData (org.valkyrienskies.mod.common.entity.EntityShipMovementData)1 ShipData (org.valkyrienskies.mod.common.ships.ShipData)1