无人机应用鼠标操控

This commit is contained in:
Atsuishio 2025-06-02 14:20:50 +08:00 committed by Light_Quanta
parent cacc1ef879
commit 32d029ca65
No known key found for this signature in database
GPG key ID: 11A39A1B8C890959
6 changed files with 84 additions and 20 deletions

View file

@ -87,7 +87,7 @@ public class DroneHudOverlay implements LayeredDraw.Layer {
boolean lookAtEntity = false;
double distance = player.distanceTo(entity);
BlockHitResult result = entity.level().clip(new ClipContext(cameraPos, cameraPos.add(player.getViewVector(1).scale(512)),
BlockHitResult result = entity.level().clip(new ClipContext(cameraPos, cameraPos.add(entity.getViewVector(1).scale(512)),
ClipContext.Block.OUTLINE, ClipContext.Fluid.NONE, entity));
Vec3 hitPos = result.getLocation();
@ -95,7 +95,7 @@ public class DroneHudOverlay implements LayeredDraw.Layer {
double entityRange = 0;
Entity lookingEntity = TraceTool.camerafFindLookingEntity(player, cameraPos, 512, deltaTracker.getRealtimeDeltaTicks());
Entity lookingEntity = TraceTool.droneFindLookingEntity(entity, cameraPos, 512, deltaTracker.getGameTimeDeltaPartialTick(true));
if (lookingEntity != null) {
lookAtEntity = true;
entityRange = entity.distanceTo(lookingEntity);

View file

@ -2,7 +2,9 @@ package com.atsuishio.superbwarfare.client.overlay;
import com.atsuishio.superbwarfare.Mod;
import com.atsuishio.superbwarfare.entity.vehicle.base.VehicleEntity;
import com.atsuishio.superbwarfare.init.ModItems;
import com.atsuishio.superbwarfare.tools.FormatTool;
import com.atsuishio.superbwarfare.tools.NBTTool;
import com.atsuishio.superbwarfare.tools.TraceTool;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.Camera;
@ -14,6 +16,7 @@ import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.phys.Vec3;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.api.distmarker.OnlyIn;
@ -37,6 +40,10 @@ public class VehicleTeamOverlay implements LayeredDraw.Layer {
PoseStack poseStack = guiGraphics.pose();
if (player == null) return;
ItemStack stack = player.getMainHandItem();
var tag = NBTTool.getTag(stack);
if (stack.is(ModItems.MONITOR.get()) && tag.getBoolean("Using") && tag.getBoolean("Linked")) return;
boolean lookAtEntity = false;
double entityRange = 0;

View file

@ -107,6 +107,7 @@ public class DroneEntity extends MobileVehicleEntity implements GeoEntity {
public boolean playHitSoundOnHurt() {
return false;
}
@Override
protected void defineSynchedData(SynchedEntityData.Builder builder) {
super.defineSynchedData(builder);
@ -175,15 +176,12 @@ public class DroneEntity extends MobileVehicleEntity implements GeoEntity {
Player controller = EntityFindUtil.findPlayer(this.level(), this.entityData.get(CONTROLLER));
if (!this.onGround()) {
if (controller != null) {
ItemStack stack = controller.getMainHandItem();
var tag = NBTTool.getTag(stack);
if (stack.is(ModItems.MONITOR.get()) && tag.getBoolean("Using")) {
// if (controller.level().isClientSide) {
// controller.playSound(ModSounds.DRONE_SOUND.get(), 114, 1);
// }
} else {
if (!stack.is(ModItems.MONITOR.get()) || !tag.getBoolean("Using")) {
upInputDown = false;
downInputDown = false;
forwardInputDown = false;
@ -363,8 +361,6 @@ public class DroneEntity extends MobileVehicleEntity implements GeoEntity {
@Override
public void travel() {
float diffX;
float diffY;
if (!this.onGround()) {
// left and right
if (rightInputDown) {
@ -406,7 +402,7 @@ public class DroneEntity extends MobileVehicleEntity implements GeoEntity {
if (up) {
holdTickY++;
this.entityData.set(POWER, Math.min(this.entityData.get(POWER) + 0.02f * Math.min(holdTickY, 5), 0.4f));
this.entityData.set(POWER, Math.min(this.entityData.get(POWER) + 0.01f * Math.min(holdTickY, 5), 0.2f));
} else if (down) {
holdTickY++;
this.entityData.set(POWER, Math.max(this.entityData.get(POWER) - 0.02f * Math.min(holdTickY, 5), this.onGround() ? 0 : 0.01f));
@ -418,7 +414,7 @@ public class DroneEntity extends MobileVehicleEntity implements GeoEntity {
if (this.getDeltaMovement().y() < 0) {
this.entityData.set(POWER, Math.min(this.entityData.get(POWER) + 0.01f, 0.4f));
} else {
this.entityData.set(POWER, Math.max(this.entityData.get(POWER) - 0.01f, 0f));
this.entityData.set(POWER, Math.max(this.entityData.get(POWER) - 0.01f, this.onGround() ? 0 : 0.01f));
}
}
@ -440,11 +436,10 @@ public class DroneEntity extends MobileVehicleEntity implements GeoEntity {
Player controller = EntityFindUtil.findPlayer(this.level(), this.entityData.get(CONTROLLER));
if (controller != null) {
ItemStack stack = controller.getMainHandItem();
if (stack.is(ModItems.MONITOR.get()) && NBTTool.getTag(stack).getBoolean("Using")) {
diffY = Math.clamp(-90f, 90f, Mth.wrapDegrees(controller.getYHeadRot() - this.getYRot()));
diffX = Math.clamp(-60f, 60f, Mth.wrapDegrees(controller.getXRot() - this.getXRot()));
this.setYRot(this.getYRot() + 0.5f * diffY);
this.setXRot(Mth.clamp(this.getXRot() + 0.5f * diffX, -10, 90));
var tag = NBTTool.getTag(stack);
if (stack.is(ModItems.MONITOR.get()) && tag.getBoolean("Using")) {
this.setYRot(this.getYRot() + 0.5f * entityData.get(MOUSE_SPEED_X));
this.setXRot(Mth.clamp(this.getXRot() + 0.5f * entityData.get(MOUSE_SPEED_Y), -10, 90));
}
}
@ -490,6 +485,11 @@ public class DroneEntity extends MobileVehicleEntity implements GeoEntity {
}
}
@Override
public boolean engineRunning() {
return !onGround();
}
@Override
public SoundEvent getEngineSound() {
return ModSounds.DRONE_SOUND.get();
@ -497,7 +497,7 @@ public class DroneEntity extends MobileVehicleEntity implements GeoEntity {
@Override
public float getEngineSoundVolume() {
return onGround() ? 0 : 0.1f;
return entityData.get(POWER) * 2f;
}
@Override

View file

@ -3,12 +3,14 @@ package com.atsuishio.superbwarfare.event;
import com.atsuishio.superbwarfare.client.MouseMovementHandler;
import com.atsuishio.superbwarfare.config.client.VehicleControlConfig;
import com.atsuishio.superbwarfare.data.gun.GunData;
import com.atsuishio.superbwarfare.entity.vehicle.DroneEntity;
import com.atsuishio.superbwarfare.entity.vehicle.base.AirEntity;
import com.atsuishio.superbwarfare.entity.vehicle.base.VehicleEntity;
import com.atsuishio.superbwarfare.init.ModItems;
import com.atsuishio.superbwarfare.init.ModMobEffects;
import com.atsuishio.superbwarfare.item.gun.GunItem;
import com.atsuishio.superbwarfare.network.message.send.MouseMoveMessage;
import com.atsuishio.superbwarfare.tools.EntityFindUtil;
import com.atsuishio.superbwarfare.tools.NBTTool;
import net.minecraft.client.CameraType;
import net.minecraft.client.Minecraft;
@ -25,7 +27,6 @@ import net.neoforged.neoforge.client.event.ClientTickEvent;
import net.neoforged.neoforge.client.event.ViewportEvent;
import net.neoforged.neoforge.network.PacketDistributor;
import static com.atsuishio.superbwarfare.event.ClientEventHandler.droneFovLerp;
import static com.atsuishio.superbwarfare.event.ClientEventHandler.isFreeCam;
@EventBusSubscriber(bus = EventBusSubscriber.Bus.GAME, value = Dist.CLIENT)
@ -59,7 +60,24 @@ public class ClientMouseHandler {
posO = posN;
posN = MouseMovementHandler.getMousePos();
if (!notInGame() && player.getVehicle() instanceof VehicleEntity vehicle) {
ItemStack stack = player.getMainHandItem();
var tag = NBTTool.getTag(stack);
if (stack.is(ModItems.MONITOR.get()) && tag.getBoolean("Using") && tag.getBoolean("Linked")) {
DroneEntity drone = EntityFindUtil.findDrone(player.level(), tag.getString("LinkedDrone"));
if (drone != null) {
speedX = drone.getMouseSensitivity() * (posN.x - posO.x);
speedY = drone.getMouseSensitivity() * (posN.y - posO.y);
lerpSpeedX = Mth.lerp(drone.getMouseSpeedX(), lerpSpeedX, speedX);
lerpSpeedY = Mth.lerp(drone.getMouseSpeedY(), lerpSpeedY, speedY);
PacketDistributor.sendToServer(new MouseMoveMessage(lerpSpeedX, lerpSpeedY));
}
return;
}
if (!notInGame() && player.getVehicle() instanceof VehicleEntity vehicle && player == vehicle.getFirstPassenger()) {
speedX = vehicle.getMouseSensitivity() * (posN.x - posO.x);
speedY = vehicle.getMouseSensitivity() * (posN.y - posO.y);
@ -164,7 +182,7 @@ public class ClientMouseHandler {
}
if (stack.is(ModItems.MONITOR.get()) && NBTTool.getTag(stack).getBoolean("Using") && NBTTool.getTag(stack).getBoolean("Linked")) {
return 0.33 / (1 + 0.08 * (droneFovLerp - 1));
return 0;
}
if (isFreeCam(player)) {

View file

@ -1,11 +1,16 @@
package com.atsuishio.superbwarfare.network.message.send;
import com.atsuishio.superbwarfare.Mod;
import com.atsuishio.superbwarfare.entity.vehicle.DroneEntity;
import com.atsuishio.superbwarfare.entity.vehicle.base.VehicleEntity;
import com.atsuishio.superbwarfare.init.ModItems;
import com.atsuishio.superbwarfare.tools.EntityFindUtil;
import com.atsuishio.superbwarfare.tools.NBTTool;
import io.netty.buffer.ByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.world.item.ItemStack;
import net.neoforged.neoforge.network.handling.IPayloadContext;
import org.jetbrains.annotations.NotNull;
@ -27,6 +32,16 @@ public record MouseMoveMessage(double speedX, double speedY) implements CustomPa
if (entity instanceof VehicleEntity vehicle) {
vehicle.mouseInput(message.speedX, message.speedY);
}
ItemStack stack = player.getMainHandItem();
var tag = NBTTool.getTag(stack);
if (stack.is(ModItems.MONITOR.get()) && tag.getBoolean("Using") && tag.getBoolean("Linked")) {
DroneEntity drone = EntityFindUtil.findDrone(player.level(), tag.getString("LinkedDrone"));
if (drone != null) {
drone.mouseInput(message.speedX, message.speedY);
}
}
}
@Override

View file

@ -120,6 +120,30 @@ public class TraceTool {
return null;
}
public static Entity droneFindLookingEntity(Entity entity, Vec3 pos, double entityReach, float ticks) {
double distance = entityReach * entityReach;
HitResult hitResult = entity.pick(entityReach, 1.0f, false);
Vec3 viewVec = entity.getViewVector(ticks);
Vec3 toVec = pos.add(viewVec.x * entityReach, viewVec.y * entityReach, viewVec.z * entityReach);
AABB aabb = entity.getBoundingBox().expandTowards(viewVec.scale(entityReach)).inflate(1.0D, 1.0D, 1.0D);
EntityHitResult entityhitresult = ProjectileUtil.getEntityHitResult(entity, pos, toVec, aabb, p -> !p.isSpectator()
&& p.isAlive()
&& !(p instanceof Projectile)
&& SeekTool.baseFilter(p)
&& !(p instanceof DecoyEntity) && smokeFilter(p)
&& p != entity
&& p != entity.getVehicle(), distance);
if (entityhitresult != null) {
hitResult = entityhitresult;
}
if (hitResult.getType() == HitResult.Type.ENTITY) {
return ((EntityHitResult) hitResult).getEntity();
}
return null;
}
public static Entity camerafFindLookingEntity(Player player, Vec3 pos, double entityReach, float ticks) {
double distance = entityReach * entityReach;
HitResult hitResult = player.pick(entityReach, 1.0f, false);