添加obb载具撞击实体的功能

This commit is contained in:
Atsuishio 2025-06-20 14:59:15 +08:00 committed by Light_Quanta
parent a96cbcdd63
commit 8ce0c5c9f8
No known key found for this signature in database
GPG key ID: 11A39A1B8C890959
2 changed files with 55 additions and 14 deletions

View file

@ -1,6 +1,7 @@
package com.atsuishio.superbwarfare.entity.vehicle.base; package com.atsuishio.superbwarfare.entity.vehicle.base;
import com.atsuishio.superbwarfare.config.server.VehicleConfig; import com.atsuishio.superbwarfare.config.server.VehicleConfig;
import com.atsuishio.superbwarfare.entity.OBBEntity;
import com.atsuishio.superbwarfare.entity.TargetEntity; import com.atsuishio.superbwarfare.entity.TargetEntity;
import com.atsuishio.superbwarfare.entity.projectile.FlareDecoyEntity; import com.atsuishio.superbwarfare.entity.projectile.FlareDecoyEntity;
import com.atsuishio.superbwarfare.entity.projectile.SmokeDecoyEntity; import com.atsuishio.superbwarfare.entity.projectile.SmokeDecoyEntity;
@ -9,6 +10,7 @@ import com.atsuishio.superbwarfare.init.ModDamageTypes;
import com.atsuishio.superbwarfare.init.ModSounds; import com.atsuishio.superbwarfare.init.ModSounds;
import com.atsuishio.superbwarfare.init.ModTags; import com.atsuishio.superbwarfare.init.ModTags;
import com.atsuishio.superbwarfare.tools.EntityFindUtil; import com.atsuishio.superbwarfare.tools.EntityFindUtil;
import com.atsuishio.superbwarfare.tools.OBB;
import com.mojang.math.Axis; import com.mojang.math.Axis;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction; import net.minecraft.core.Direction;
@ -661,23 +663,41 @@ public abstract class MobileVehicleEntity extends EnergyVehicleEntity implements
public void crushEntities(Vec3 velocity) { public void crushEntities(Vec3 velocity) {
if (level() instanceof ServerLevel) { if (level() instanceof ServerLevel) {
if (!this.canCrushEntities()) return; if (!this.canCrushEntities()) return;
if (velocity.horizontalDistance() < 0.25) return; // if (velocity.horizontalDistance() < 0.25) return;
if (isRemoved()) return; if (isRemoved()) return;
var frontBox = getBoundingBox().move(velocity);
var entities = level().getEntities(EntityTypeTest.forClass(Entity.class), frontBox, List<Entity> entities;
entity -> entity != this && entity != getFirstPassenger() && entity.getVehicle() == null)
.stream().filter(entity -> { if (this instanceof OBBEntity obbEntity) {
if (entity.isAlive()) { var frontBox = getBoundingBox().move(velocity).inflate(4);
var type = BuiltInRegistries.ENTITY_TYPE.getKey(entity.getType()); entities = level().getEntities(EntityTypeTest.forClass(Entity.class), frontBox,
return (entity instanceof VehicleEntity || entity instanceof Boat || entity instanceof Minecart entity -> entity != this && entity != getFirstPassenger() && entity.getVehicle() == null)
|| (entity instanceof LivingEntity living && !(living instanceof Player player && player.isSpectator()))) .stream().filter(entity -> {
|| VehicleConfig.COLLISION_ENTITY_WHITELIST.get().contains(type.toString()); if (entity.isAlive() && isInObb(obbEntity, entity, velocity)) {
var type = BuiltInRegistries.ENTITY_TYPE.getKey(entity.getType());
return (entity instanceof VehicleEntity || entity instanceof Boat || entity instanceof Minecart || (entity instanceof LivingEntity living && !(living instanceof Player player && player.isSpectator()))) || VehicleConfig.COLLISION_ENTITY_WHITELIST.get().contains(type.toString());
}
return false;
} }
return false; )
} .toList();
)
.toList(); } else {
var frontBox = getBoundingBox().move(velocity);
entities = level().getEntities(EntityTypeTest.forClass(Entity.class), frontBox,
entity -> entity != this && entity != getFirstPassenger() && entity.getVehicle() == null)
.stream().filter(entity -> {
if (entity.isAlive()) {
var type = BuiltInRegistries.ENTITY_TYPE.getKey(entity.getType());
return (entity instanceof VehicleEntity || entity instanceof Boat || entity instanceof Minecart
|| (entity instanceof LivingEntity living && !(living instanceof Player player && player.isSpectator())))
|| VehicleConfig.COLLISION_ENTITY_WHITELIST.get().contains(type.toString());
}
return false;
}
)
.toList();
}
for (var entity : entities) { for (var entity : entities) {
double entitySize = entity.getBoundingBox().getSize(); double entitySize = entity.getBoundingBox().getSize();
@ -730,6 +750,22 @@ public abstract class MobileVehicleEntity extends EnergyVehicleEntity implements
} }
} }
public boolean isInObb(OBBEntity obbEntity, Entity entity, Vec3 velocity) {
var obbList = obbEntity.getOBBs();
for (var obb : obbList) {
obb = obb.move(velocity);
if (entity instanceof OBBEntity obbEntity2) {
var obbList2 = obbEntity2.getOBBs();
for (var obb2 : obbList2) {
return OBB.isColliding(obb, obb2);
}
} else {
return OBB.isColliding(obb, entity.getBoundingBox());
}
}
return false;
}
public Vector3f getForwardDirection() { public Vector3f getForwardDirection() {
return new Vector3f( return new Vector3f(
Mth.sin(-getYRot() * ((float) Math.PI / 180)), Mth.sin(-getYRot() * ((float) Math.PI / 180)),

View file

@ -222,6 +222,11 @@ public record OBB(Vector3f center, Vector3f extents, Quaternionf rotation) {
return new OBB(center, newExtents, rotation); return new OBB(center, newExtents, rotation);
} }
public OBB move(Vec3 vec3) {
Vector3f newCenter = new Vector3f((float) (center.x + vec3.x), (float) (center.y + vec3.y), (float) (center.z + vec3.z));
return new OBB(newCenter, extents, rotation);
}
/** /**
* 检查点是否在OBB内部 * 检查点是否在OBB内部
* *