Search in sources :

Example 1 with MovingObjectPosition

use of cn.nukkit.level.MovingObjectPosition 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)

Aggregations

Entity (cn.nukkit.entity.Entity)1 MovingObjectPosition (cn.nukkit.level.MovingObjectPosition)1 AxisAlignedBB (cn.nukkit.math.AxisAlignedBB)1 Vector3 (cn.nukkit.math.Vector3)1