Search in sources :

Example 1 with AxisAlignedBB

use of cn.nukkit.math.AxisAlignedBB in project Nukkit by Nukkit.

the class EntityLightning method onUpdate.

@Override
public boolean onUpdate(int currentTick) {
    if (this.closed) {
        return false;
    }
    int tickDiff = currentTick - this.lastUpdate;
    if (tickDiff <= 0 && !this.justCreated) {
        return true;
    }
    this.lastUpdate = currentTick;
    this.entityBaseTick(tickDiff);
    if (this.state == 2) {
        this.level.addLevelSoundEvent(this, LevelSoundEventPacket.SOUND_THUNDER, 93, -1);
        this.level.addLevelSoundEvent(this, LevelSoundEventPacket.SOUND_EXPLODE, 93, -1);
    }
    this.state--;
    if (this.state < 0) {
        if (this.liveTime == 0) {
            this.close();
            return false;
        } else if (this.state < -ThreadLocalRandom.current().nextInt(10)) {
            this.liveTime--;
            this.state = 1;
            if (this.isEffect && this.level.gameRules.getBoolean(GameRule.DO_FIRE_TICK)) {
                Block block = this.getLevelBlock();
                if (block.getId() == Block.AIR || block.getId() == Block.TALL_GRASS) {
                    BlockIgniteEvent e = new BlockIgniteEvent(block, null, this, BlockIgniteEvent.BlockIgniteCause.LIGHTNING);
                    getServer().getPluginManager().callEvent(e);
                    if (!e.isCancelled()) {
                        Block fire = new BlockFire();
                        this.level.setBlock(block, fire);
                        this.getLevel().scheduleUpdate(fire, fire.tickRate());
                    }
                }
            }
        }
    }
    if (this.state >= 0) {
        if (this.isEffect) {
            AxisAlignedBB bb = getBoundingBox().grow(3, 3, 3);
            bb.setMaxX(bb.getMaxX() + 6);
            for (Entity entity : this.level.getCollidingEntities(bb, this)) {
                entity.onStruckByLightning(this);
            }
        }
    }
    return true;
}
Also used : AxisAlignedBB(cn.nukkit.math.AxisAlignedBB) Entity(cn.nukkit.entity.Entity) Block(cn.nukkit.block.Block) BlockIgniteEvent(cn.nukkit.event.block.BlockIgniteEvent) BlockFire(cn.nukkit.block.BlockFire)

Example 2 with AxisAlignedBB

use of cn.nukkit.math.AxisAlignedBB in project Nukkit by Nukkit.

the class BlockEntityPistonArm method pushEntities.

private void pushEntities() {
    float lastProgress = this.getExtendedProgress(this.lastProgress);
    double x = (double) (lastProgress * (float) this.facing.getXOffset());
    double y = (double) (lastProgress * (float) this.facing.getYOffset());
    double z = (double) (lastProgress * (float) this.facing.getZOffset());
    AxisAlignedBB bb = new SimpleAxisAlignedBB(x, y, z, x + 1.0D, y + 1.0D, z + 1.0D);
    Entity[] entities = this.level.getCollidingEntities(bb);
    if (entities.length != 0) {
        ;
    }
}
Also used : SimpleAxisAlignedBB(cn.nukkit.math.SimpleAxisAlignedBB) AxisAlignedBB(cn.nukkit.math.AxisAlignedBB) Entity(cn.nukkit.entity.Entity) SimpleAxisAlignedBB(cn.nukkit.math.SimpleAxisAlignedBB)

Example 3 with AxisAlignedBB

use of cn.nukkit.math.AxisAlignedBB in project Nukkit by Nukkit.

the class EntityProjectile method onUpdate.

@Override
public boolean onUpdate(int currentTick) {
    if (this.closed) {
        return false;
    }
    int tickDiff = currentTick - this.lastUpdate;
    if (tickDiff <= 0 && !this.justCreated) {
        return true;
    }
    this.lastUpdate = currentTick;
    boolean hasUpdate = this.entityBaseTick(tickDiff);
    if (this.isAlive()) {
        MovingObjectPosition movingObjectPosition = null;
        if (!this.isCollided) {
            this.motionY -= this.getGravity();
        }
        Vector3 moveVector = new Vector3(this.x + this.motionX, this.y + this.motionY, this.z + this.motionZ);
        Entity[] list = this.getLevel().getCollidingEntities(this.boundingBox.addCoord(this.motionX, this.motionY, this.motionZ).expand(1, 1, 1), this);
        double nearDistance = Integer.MAX_VALUE;
        Entity nearEntity = null;
        for (Entity entity : list) {
            if (/*!entity.canCollideWith(this) or */
            (entity == this.shootingEntity && this.ticksLived < 5)) {
                continue;
            }
            AxisAlignedBB axisalignedbb = entity.boundingBox.grow(0.3, 0.3, 0.3);
            MovingObjectPosition ob = axisalignedbb.calculateIntercept(this, moveVector);
            if (ob == null) {
                continue;
            }
            double distance = this.distanceSquared(ob.hitVector);
            if (distance < nearDistance) {
                nearDistance = distance;
                nearEntity = entity;
            }
        }
        if (nearEntity != null) {
            movingObjectPosition = MovingObjectPosition.fromEntity(nearEntity);
        }
        if (movingObjectPosition != null) {
            if (movingObjectPosition.entityHit != null) {
                onCollideWithEntity(movingObjectPosition.entityHit);
                return true;
            }
        }
        this.move(this.motionX, this.motionY, this.motionZ);
        if (this.isCollided && !this.hadCollision) {
            // collide with block
            this.hadCollision = true;
            this.motionX = 0;
            this.motionY = 0;
            this.motionZ = 0;
            this.server.getPluginManager().callEvent(new ProjectileHitEvent(this, MovingObjectPosition.fromBlock(this.getFloorX(), this.getFloorY(), this.getFloorZ(), -1, this)));
            return false;
        } else if (!this.isCollided && this.hadCollision) {
            this.hadCollision = false;
        }
        if (!this.hadCollision || Math.abs(this.motionX) > 0.00001 || Math.abs(this.motionY) > 0.00001 || Math.abs(this.motionZ) > 0.00001) {
            double f = Math.sqrt((this.motionX * this.motionX) + (this.motionZ * this.motionZ));
            this.yaw = Math.atan2(this.motionX, this.motionZ) * 180 / Math.PI;
            this.pitch = Math.atan2(this.motionY, f) * 180 / Math.PI;
            hasUpdate = true;
        }
        this.updateMovement();
    }
    return hasUpdate;
}
Also used : AxisAlignedBB(cn.nukkit.math.AxisAlignedBB) Entity(cn.nukkit.entity.Entity) MovingObjectPosition(cn.nukkit.level.MovingObjectPosition) Vector3(cn.nukkit.math.Vector3)

Example 4 with AxisAlignedBB

use of cn.nukkit.math.AxisAlignedBB in project Nukkit by Nukkit.

the class Block method calculateIntercept.

public MovingObjectPosition calculateIntercept(Vector3 pos1, Vector3 pos2) {
    AxisAlignedBB bb = this.getBoundingBox();
    if (bb == null) {
        return null;
    }
    Vector3 v1 = pos1.getIntermediateWithXValue(pos2, bb.getMinX());
    Vector3 v2 = pos1.getIntermediateWithXValue(pos2, bb.getMaxX());
    Vector3 v3 = pos1.getIntermediateWithYValue(pos2, bb.getMinY());
    Vector3 v4 = pos1.getIntermediateWithYValue(pos2, bb.getMaxY());
    Vector3 v5 = pos1.getIntermediateWithZValue(pos2, bb.getMinZ());
    Vector3 v6 = pos1.getIntermediateWithZValue(pos2, bb.getMaxZ());
    if (v1 != null && !bb.isVectorInYZ(v1)) {
        v1 = null;
    }
    if (v2 != null && !bb.isVectorInYZ(v2)) {
        v2 = null;
    }
    if (v3 != null && !bb.isVectorInXZ(v3)) {
        v3 = null;
    }
    if (v4 != null && !bb.isVectorInXZ(v4)) {
        v4 = null;
    }
    if (v5 != null && !bb.isVectorInXY(v5)) {
        v5 = null;
    }
    if (v6 != null && !bb.isVectorInXY(v6)) {
        v6 = null;
    }
    Vector3 vector = v1;
    if (v2 != null && (vector == null || pos1.distanceSquared(v2) < pos1.distanceSquared(vector))) {
        vector = v2;
    }
    if (v3 != null && (vector == null || pos1.distanceSquared(v3) < pos1.distanceSquared(vector))) {
        vector = v3;
    }
    if (v4 != null && (vector == null || pos1.distanceSquared(v4) < pos1.distanceSquared(vector))) {
        vector = v4;
    }
    if (v5 != null && (vector == null || pos1.distanceSquared(v5) < pos1.distanceSquared(vector))) {
        vector = v5;
    }
    if (v6 != null && (vector == null || pos1.distanceSquared(v6) < pos1.distanceSquared(vector))) {
        vector = v6;
    }
    if (vector == null) {
        return null;
    }
    int f = -1;
    if (vector == v1) {
        f = 4;
    } else if (vector == v2) {
        f = 5;
    } else if (vector == v3) {
        f = 0;
    } else if (vector == v4) {
        f = 1;
    } else if (vector == v5) {
        f = 2;
    } else if (vector == v6) {
        f = 3;
    }
    return MovingObjectPosition.fromBlock((int) this.x, (int) this.y, (int) this.z, f, vector.add(this.x, this.y, this.z));
}
Also used : AxisAlignedBB(cn.nukkit.math.AxisAlignedBB) Vector3(cn.nukkit.math.Vector3)

Example 5 with AxisAlignedBB

use of cn.nukkit.math.AxisAlignedBB in project Nukkit by Nukkit.

the class BlockCocoa method getRelativeBoundingBox.

private AxisAlignedBB getRelativeBoundingBox() {
    int damage = this.getDamage();
    if (damage > 11) {
        this.setDamage(damage = 11);
    }
    AxisAlignedBB boundingBox = ALL[damage];
    if (boundingBox != null)
        return boundingBox;
    AxisAlignedBB[] bbs;
    switch(getDamage()) {
        case 0:
        case 4:
        case 8:
            bbs = NORTH;
            break;
        case 1:
        case 5:
        case 9:
            bbs = EAST;
            break;
        case 2:
        case 6:
        case 10:
            bbs = SOUTH;
            break;
        case 3:
        case 7:
        case 11:
            bbs = WEST;
            break;
        default:
            bbs = NORTH;
            break;
    }
    return ALL[damage] = bbs[this.getDamage() >> 2];
}
Also used : SimpleAxisAlignedBB(cn.nukkit.math.SimpleAxisAlignedBB) AxisAlignedBB(cn.nukkit.math.AxisAlignedBB)

Aggregations

AxisAlignedBB (cn.nukkit.math.AxisAlignedBB)6 Entity (cn.nukkit.entity.Entity)3 SimpleAxisAlignedBB (cn.nukkit.math.SimpleAxisAlignedBB)3 Vector3 (cn.nukkit.math.Vector3)2 Block (cn.nukkit.block.Block)1 BlockFire (cn.nukkit.block.BlockFire)1 BlockIgniteEvent (cn.nukkit.event.block.BlockIgniteEvent)1 MovingObjectPosition (cn.nukkit.level.MovingObjectPosition)1