From e3f1fbf4e93d02b20b6df9bc056700b3fcc65f1a Mon Sep 17 00:00:00 2001 From: 17146 <1714673995@qq.com> Date: Tue, 3 Dec 2024 22:19:07 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E6=BF=80=E5=85=89=E5=87=BB?= =?UTF-8?q?=E4=B8=AD=E6=95=88=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../entity/projectile/LaserEntity.java | 64 ++++++++++++++----- 1 file changed, 49 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/projectile/LaserEntity.java b/src/main/java/com/atsuishio/superbwarfare/entity/projectile/LaserEntity.java index bd14d34cd..a74526c24 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/projectile/LaserEntity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/projectile/LaserEntity.java @@ -7,8 +7,13 @@ import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.player.Player; import net.minecraft.world.level.ClipContext; import net.minecraft.world.level.Level; +import net.minecraft.world.phys.AABB; import net.minecraft.world.phys.Vec3; +import java.util.Comparator; +import java.util.List; +import java.util.Optional; + /** * Code based on @BobMowzie's MowziesMobs, @EEEAB's EEEABsMobs and @Mercurows's DreamaticVoyage */ @@ -51,21 +56,7 @@ public class LaserEntity extends AbstractLaserEntity { if (this.tickCount >= this.getCountDown()) { this.calculateEndPos(RADIUS); - CustomHitResult result = new CustomHitResult(); - result.setBlockHit(this.level().clip(new ClipContext(new Vec3(getX(), getY(), getZ()), new Vec3(endPosX, endPosY, endPosZ), - ClipContext.Block.COLLIDER, ClipContext.Fluid.NONE, this))); - if (result.getBlockHit() != null) { - Vec3 hitVec = result.getBlockHit().getLocation(); - collidePosX = hitVec.x; - collidePosY = hitVec.y; - collidePosZ = hitVec.z; - blockSide = result.getBlockHit().getDirection(); - } else { - collidePosX = endPosX; - collidePosY = endPosY; - collidePosZ = endPosZ; - blockSide = null; - } + raytraceEntities(this.level(), new Vec3(getX(), getY(), getZ()), new Vec3(endPosX, endPosY, endPosZ)); if (this.blockSide != null) { this.spawnExplosionParticles(); @@ -73,6 +64,49 @@ public class LaserEntity extends AbstractLaserEntity { } } + @Override + public CustomHitResult raytraceEntities(Level world, Vec3 from, Vec3 to) { + CustomHitResult result = new CustomHitResult(); + result.setBlockHit(this.level().clip(new ClipContext(new Vec3(getX(), getY(), getZ()), new Vec3(endPosX, endPosY, endPosZ), + ClipContext.Block.COLLIDER, ClipContext.Fluid.NONE, this))); + if (result.getBlockHit() != null) { + Vec3 hitVec = result.getBlockHit().getLocation(); + collidePosX = hitVec.x; + collidePosY = hitVec.y; + collidePosZ = hitVec.z; + blockSide = result.getBlockHit().getDirection(); + } + + List entities = world.getEntitiesOfClass(LivingEntity.class, new AABB(Math.min(getX(), collidePosX), Math.min(getY(), collidePosY), Math.min(getZ(), collidePosZ), Math.max(getX(), collidePosX), Math.max(getY(), collidePosY), Math.max(getZ(), collidePosZ)).inflate(1, 1, 1)); + for (LivingEntity entity : entities) { + if (entity == this.caster) { + continue; + } + float pad = entity.getPickRadius() + getBaseScale(); + AABB aabb = entity.getBoundingBox().inflate(pad, pad, pad); + Optional hit = aabb.clip(from, to); + if (aabb.contains(from)) { + result.addEntityHit(entity); + } else if (hit.isPresent()) { + result.addEntityHit(entity); + } + } + + var target = result.getEntities().stream().min(Comparator.comparingDouble(e -> e.distanceToSqr(this.caster))); + if (target.isPresent()) { + collidePosX = target.get().getX(); + collidePosY = target.get().getY(); + collidePosZ = target.get().getZ(); + } else { + collidePosX = endPosX; + collidePosY = endPosY; + collidePosZ = endPosZ; + blockSide = null; + } + + return result; + } + public void spawnExplosionParticles() { }