Search in sources :

Example 1 with TraceCollision

use of me.deecaad.weaponmechanics.weapon.explode.raytrace.TraceCollision in project MechanicsMain by WeaponMechanics.

the class ExplosionExposure method canSee.

/**
 * Determines if the given entity can see the given <code>Location</code>.
 *
 * @param originLoc The point to check if the entity can see
 * @param entity The entity to check against
 * @param fov The field of view of the entity
 * @return true if the entity can see the origin
 */
default boolean canSee(@Nonnull Location originLoc, @Nonnull LivingEntity entity, double fov) {
    // Get the vector between the entity and origin, and the player's eye
    // vector, and determine the angle between the 2 vectors.
    Vector origin = originLoc.toVector();
    Vector end = entity.getEyeLocation().toVector();
    Vector direction = entity.getLocation().getDirection();
    Vector between = origin.clone().subtract(end);
    double angle = VectorUtil.getAngleBetween(direction, between);
    // then we need to do more calculations
    if (angle > fov) {
        return false;
    }
    Ray ray = new Ray(originLoc.getWorld(), origin, end, between.length());
    TraceCollision collision = new TraceCollision(BLOCK_OR_ENTITY) {

        @Override
        public boolean canHit(Block block) {
            String name = block.getType().name();
            // GLASS
            if (name.endsWith("GLASS") || name.endsWith("PANE")) {
                return false;
            } else // LEAVES
            if (name.endsWith("LEAVES")) {
                return false;
            } else // OAK_FENCE
            if (name.endsWith("FENCE") || name.endsWith("FENCE_GATE")) {
                return false;
            } else if (name.equals("SLIME_BLOCK")) {
                return false;
            } else {
                return WeaponCompatibilityAPI.getWeaponCompatibility().getHitBox(block) != null;
            }
        }

        @Override
        public boolean canHit(Entity entity1) {
            return !entity1.equals(entity);
        }
    };
    TraceResult result = ray.trace(collision, 0.2);
    // origin, than the entity can see the explosion
    return result.isEmpty();
}
Also used : Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) TraceCollision(me.deecaad.weaponmechanics.weapon.explode.raytrace.TraceCollision) Block(org.bukkit.block.Block) Ray(me.deecaad.weaponmechanics.weapon.explode.raytrace.Ray) TraceResult(me.deecaad.weaponmechanics.weapon.explode.raytrace.TraceResult) Vector(org.bukkit.util.Vector)

Aggregations

Ray (me.deecaad.weaponmechanics.weapon.explode.raytrace.Ray)1 TraceCollision (me.deecaad.weaponmechanics.weapon.explode.raytrace.TraceCollision)1 TraceResult (me.deecaad.weaponmechanics.weapon.explode.raytrace.TraceResult)1 Block (org.bukkit.block.Block)1 Entity (org.bukkit.entity.Entity)1 LivingEntity (org.bukkit.entity.LivingEntity)1 Vector (org.bukkit.util.Vector)1