Search in sources :

Example 1 with BlockRailPowered

use of cn.nukkit.block.BlockRailPowered in project Nukkit by Nukkit.

the class EntityMinecartAbstract method processMovement.

private void processMovement(int dx, int dy, int dz, BlockRail block) {
    fallDistance = 0.0F;
    Vector3 vector = getNextRail(x, y, z);
    y = (double) dy;
    boolean isPowered = false;
    boolean isSlowed = false;
    if (block instanceof BlockRailPowered) {
        isPowered = block.isActive();
        isSlowed = !block.isActive();
    }
    switch(Orientation.byMetadata(block.getRealMeta())) {
        case ASCENDING_NORTH:
            motionX -= 0.0078125D;
            y += 1;
            break;
        case ASCENDING_SOUTH:
            motionX += 0.0078125D;
            y += 1;
            break;
        case ASCENDING_EAST:
            motionZ += 0.0078125D;
            y += 1;
            break;
        case ASCENDING_WEST:
            motionZ -= 0.0078125D;
            y += 1;
            break;
    }
    int[][] facing = matrix[block.getRealMeta()];
    double facing1 = (double) (facing[1][0] - facing[0][0]);
    double facing2 = (double) (facing[1][2] - facing[0][2]);
    double speedOnTurns = Math.sqrt(facing1 * facing1 + facing2 * facing2);
    double realFacing = motionX * facing1 + motionZ * facing2;
    if (realFacing < 0) {
        facing1 = -facing1;
        facing2 = -facing2;
    }
    double squareOfFame = Math.sqrt(motionX * motionX + motionZ * motionZ);
    if (squareOfFame > 2) {
        squareOfFame = 2;
    }
    motionX = squareOfFame * facing1 / speedOnTurns;
    motionZ = squareOfFame * facing2 / speedOnTurns;
    double expectedSpeed;
    // PlayerYawNegative
    double playerYawNeg;
    // PlayerYawPositive
    double playerYawPos;
    double motion;
    if (linkedEntity != null && linkedEntity instanceof EntityLiving) {
        expectedSpeed = currentSpeed;
        if (expectedSpeed > 0) {
            // This is a trajectory (Angle of elevation)
            playerYawNeg = -Math.sin(linkedEntity.yaw * 3.1415927F / 180.0F);
            playerYawPos = Math.cos(linkedEntity.yaw * 3.1415927F / 180.0F);
            motion = motionX * motionX + motionZ * motionZ;
            if (motion < 0.01D) {
                motionX += playerYawNeg * 0.1D;
                motionZ += playerYawPos * 0.1D;
                isSlowed = false;
            }
        }
    }
    // http://minecraft.gamepedia.com/Powered_Rail#Rail
    if (isSlowed) {
        expectedSpeed = Math.sqrt(motionX * motionX + motionZ * motionZ);
        if (expectedSpeed < 0.03D) {
            motionX *= 0;
            motionY *= 0;
            motionZ *= 0;
        } else {
            motionX *= 0.5D;
            motionY *= 0;
            motionZ *= 0.5D;
        }
    }
    playerYawNeg = (double) dx + 0.5D + (double) facing[0][0] * 0.5D;
    playerYawPos = (double) dz + 0.5D + (double) facing[0][2] * 0.5D;
    motion = (double) dx + 0.5D + (double) facing[1][0] * 0.5D;
    double wallOfFame = (double) dz + 0.5D + (double) facing[1][2] * 0.5D;
    facing1 = motion - playerYawNeg;
    facing2 = wallOfFame - playerYawPos;
    double motX;
    double motZ;
    if (facing1 == 0) {
        x = (double) dx + 0.5D;
        expectedSpeed = z - (double) dz;
    } else if (facing2 == 0) {
        z = (double) dz + 0.5D;
        expectedSpeed = x - (double) dx;
    } else {
        motX = x - playerYawNeg;
        motZ = z - playerYawPos;
        expectedSpeed = (motX * facing1 + motZ * facing2) * 2;
    }
    x = playerYawNeg + facing1 * expectedSpeed;
    z = playerYawPos + facing2 * expectedSpeed;
    // Hehe, my minstake :3
    setPosition(new Vector3(x, y, z));
    motX = motionX;
    motZ = motionZ;
    if (linkedEntity != null) {
        motX *= 0.75D;
        motZ *= 0.75D;
    }
    motX = NukkitMath.clamp(motX, -getMaxSpeed(), getMaxSpeed());
    motZ = NukkitMath.clamp(motZ, -getMaxSpeed(), getMaxSpeed());
    move(motX, 0, motZ);
    if (facing[0][1] != 0 && MathHelper.floor(x) - dx == facing[0][0] && MathHelper.floor(z) - dz == facing[0][2]) {
        setPosition(new Vector3(x, y + (double) facing[0][1], z));
    } else if (facing[1][1] != 0 && MathHelper.floor(x) - dx == facing[1][0] && MathHelper.floor(z) - dz == facing[1][2]) {
        setPosition(new Vector3(x, y + (double) facing[1][1], z));
    }
    applyDrag();
    Vector3 vector1 = getNextRail(x, y, z);
    if (vector1 != null && vector != null) {
        double d14 = (vector.y - vector1.y) * 0.05D;
        squareOfFame = Math.sqrt(motionX * motionX + motionZ * motionZ);
        if (squareOfFame > 0) {
            motionX = motionX / squareOfFame * (squareOfFame + d14);
            motionZ = motionZ / squareOfFame * (squareOfFame + d14);
        }
        setPosition(new Vector3(x, vector1.y, z));
    }
    int floorX = MathHelper.floor(x);
    int floorZ = MathHelper.floor(z);
    if (floorX != dx || floorZ != dz) {
        squareOfFame = Math.sqrt(motionX * motionX + motionZ * motionZ);
        motionX = squareOfFame * (double) (floorX - dx);
        motionZ = squareOfFame * (double) (floorZ - dz);
    }
    if (isPowered) {
        double newMovie = Math.sqrt(motionX * motionX + motionZ * motionZ);
        if (newMovie > 0.01D) {
            double nextMovie = 0.06D;
            motionX += motionX / newMovie * nextMovie;
            motionZ += motionZ / newMovie * nextMovie;
        } else if (block.getOrientation() == Orientation.STRAIGHT_NORTH_SOUTH) {
            if (level.getBlock(new Vector3(dx - 1, dy, dz)).isNormalBlock()) {
                motionX = 0.02D;
            } else if (level.getBlock(new Vector3(dx + 1, dy, dz)).isNormalBlock()) {
                motionX = -0.02D;
            }
        } else if (block.getOrientation() == Orientation.STRAIGHT_EAST_WEST) {
            if (level.getBlock(new Vector3(dx, dy, dz - 1)).isNormalBlock()) {
                motionZ = 0.02D;
            } else if (level.getBlock(new Vector3(dx, dy, dz + 1)).isNormalBlock()) {
                motionZ = -0.02D;
            }
        }
    }
}
Also used : EntityLiving(cn.nukkit.entity.EntityLiving) BlockRailPowered(cn.nukkit.block.BlockRailPowered) Vector3(cn.nukkit.math.Vector3)

Aggregations

BlockRailPowered (cn.nukkit.block.BlockRailPowered)1 EntityLiving (cn.nukkit.entity.EntityLiving)1 Vector3 (cn.nukkit.math.Vector3)1