diff --git a/src/main/java/com/atsuishio/superbwarfare/client/overlay/HelicopterHudOverlay.java b/src/main/java/com/atsuishio/superbwarfare/client/overlay/HelicopterHudOverlay.java index caadfa919..abf527d8b 100644 --- a/src/main/java/com/atsuishio/superbwarfare/client/overlay/HelicopterHudOverlay.java +++ b/src/main/java/com/atsuishio/superbwarfare/client/overlay/HelicopterHudOverlay.java @@ -77,7 +77,8 @@ public class HelicopterHudOverlay implements LayeredDraw.Layer { RenderSystem.blendFuncSeparate(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA, GlStateManager.SourceFactor.ONE, GlStateManager.DestFactor.ZERO); RenderSystem.setShaderColor(1, 1, 1, 1); - scopeScale = Mth.lerp(deltaTracker.getGameTimeDeltaPartialTick(true), scopeScale, 1F); + float partialTick = deltaTracker.getGameTimeDeltaPartialTick(true); + scopeScale = Mth.lerp(partialTick, scopeScale, 1F); float f = (float) Math.min(w, h); float f1 = Math.min((float) w / f, (float) h / f) * scopeScale; float i = Mth.floor(f * f1); @@ -87,18 +88,18 @@ public class HelicopterHudOverlay implements LayeredDraw.Layer { if (Minecraft.getInstance().options.getCameraType() == CameraType.FIRST_PERSON) { preciseBlit(guiGraphics, Mod.loc("textures/screens/helicopter/heli_base.png"), k, l, 0, 0.0F, i, j, i, j); - renderDriverAngle(guiGraphics, player, mobileVehicle, k, l, i, j, deltaTracker.getGameTimeDeltaPartialTick(true)); + renderDriverAngle(guiGraphics, player, mobileVehicle, k, l, i, j, partialTick); preciseBlit(guiGraphics, Mod.loc("textures/screens/compass.png"), (float) w / 2 - 128, (float) 6, 128 + ((float) 64 / 45 * mobileVehicle.getYRot()), 0, 256, 16, 512, 16); poseStack.pushPose(); - poseStack.rotateAround(Axis.ZP.rotationDegrees(-iHelicopterEntity.getRotZ(deltaTracker.getGameTimeDeltaPartialTick(true))), w / 2f, h / 2f, 0); - float pitch = iHelicopterEntity.getRotX(deltaTracker.getGameTimeDeltaPartialTick(true)); + poseStack.rotateAround(Axis.ZP.rotationDegrees(-iHelicopterEntity.getRotZ(partialTick)), w / 2f, h / 2f, 0); + float pitch = iHelicopterEntity.getRotX(partialTick); preciseBlit(guiGraphics, Mod.loc("textures/screens/helicopter/heli_line.png"), (float) w / 2 - 128, (float) h / 2 - 512 - 5.475f * pitch, 0, 0, 256, 1024, 256, 1024); poseStack.popPose(); poseStack.pushPose(); - poseStack.rotateAround(Axis.ZP.rotationDegrees(iHelicopterEntity.getRotZ(deltaTracker.getGameTimeDeltaPartialTick(true))), w / 2f, h / 2f - 56, 0); + poseStack.rotateAround(Axis.ZP.rotationDegrees(iHelicopterEntity.getRotZ(partialTick)), w / 2f, h / 2f - 56, 0); preciseBlit(guiGraphics, Mod.loc("textures/screens/helicopter/roll_ind.png"), (float) w / 2 - 8, (float) h / 2 - 88, 0, 0, 16, 16, 16, 16); poseStack.popPose(); @@ -110,9 +111,9 @@ public class HelicopterHudOverlay implements LayeredDraw.Layer { ClipContext.Block.OUTLINE, ClipContext.Fluid.ANY, mobileVehicle)).getBlockPos()))); float power = iHelicopterEntity.getPower(); - lerpPower = Mth.lerp(0.001f * deltaTracker.getGameTimeDeltaPartialTick(true), lerpPower, power); + lerpPower = Mth.lerp(0.001f * partialTick, lerpPower, power); preciseBlit(guiGraphics, Mod.loc("textures/screens/helicopter/heli_power.png"), (float) w / 2 + 130f, ((float) h / 2 - 64 + 124 - power * 980), 0, 0, 4, power * 980, 4, power * 980); - lerpVy = (float) Mth.lerp(0.021f * deltaTracker.getGameTimeDeltaPartialTick(true), lerpVy, mobileVehicle.getDeltaMovement().y()); + lerpVy = (float) Mth.lerp(0.021f * partialTick, lerpVy, mobileVehicle.getDeltaMovement().y()); preciseBlit(guiGraphics, Mod.loc("textures/screens/helicopter/heli_vy_move.png"), (float) w / 2 + 138, ((float) h / 2 - 3 - Math.max(lerpVy * 20, -24) * 2.5f), 0, 0, 8, 8, 8, 8); guiGraphics.drawString(Minecraft.getInstance().font, Component.literal(FormatTool.format0D(lerpVy * 20, "m/s")), w / 2 + 146, (int) (h / 2 - 3 - Math.max(lerpVy * 20, -24) * 2.5), (lerpVy * 20 < -24 || ((lerpVy * 20 < -10 || (lerpVy * 20 < -1 && length(mobileVehicle.getDeltaMovement().x, mobileVehicle.getDeltaMovement().y, mobileVehicle.getDeltaMovement().z) * 72 > 100)) && height < 36) || (length(mobileVehicle.getDeltaMovement().x, mobileVehicle.getDeltaMovement().y, mobileVehicle.getDeltaMovement().z) * 72 > 40 && blockInWay < 72) ? -65536 : 0x66FF00), false); @@ -153,7 +154,7 @@ public class HelicopterHudOverlay implements LayeredDraw.Layer { } - Matrix4f transform = getVehicleTransform(mobileVehicle, deltaTracker.getGameTimeDeltaPartialTick(true)); + Matrix4f transform = getVehicleTransform(mobileVehicle, partialTick); float x0 = 0f; float y0 = 0.65f; float z0 = 0.8f; @@ -163,12 +164,12 @@ public class HelicopterHudOverlay implements LayeredDraw.Layer { float fovAdjust2 = (float) (Minecraft.getInstance().options.fov().get() / 30) - 1; double zoom = 0.96 * 3 + 0.06 * fovAdjust2; - Vec3 pos = new Vec3(worldPosition.x, worldPosition.y, worldPosition.z).add(mobileVehicle.getViewVector(deltaTracker.getGameTimeDeltaPartialTick(true)).scale(192)); - Vec3 lookAngle = player.getLookAngle().normalize().scale(pos.distanceTo(cameraPos) * (1 - 1.0 / zoom)); + Vec3 pos = new Vec3(worldPosition.x, worldPosition.y, worldPosition.z).add(mobileVehicle.getViewVector(partialTick).scale(192)); + Vec3 lookAngle = mobileVehicle.getViewVector(partialTick).normalize().scale(pos.distanceTo(cameraPos) * (1 - 1.0 / zoom)); var cPos = cameraPos.add(lookAngle); - Vec3 p = RenderHelper.worldToScreen(new Vec3(worldPosition.x, worldPosition.y, worldPosition.z).add(mobileVehicle.getViewVector(deltaTracker.getGameTimeDeltaPartialTick(true)).scale(192)), ClientEventHandler.zoomVehicle ? cPos : cameraPos); + Vec3 p = RenderHelper.worldToScreen(new Vec3(worldPosition.x, worldPosition.y, worldPosition.z).add(mobileVehicle.getViewVector(partialTick).scale(192)), ClientEventHandler.zoomVehicle ? cPos : cameraPos); if (p != null) { poseStack.pushPose(); @@ -179,9 +180,9 @@ public class HelicopterHudOverlay implements LayeredDraw.Layer { if (Minecraft.getInstance().options.getCameraType() == CameraType.FIRST_PERSON) { preciseBlit(guiGraphics, Mod.loc("textures/screens/helicopter/crosshair_ind.png"), x - 8, y - 8, 0, 0, 16, 16, 16, 16); renderKillIndicator(guiGraphics, x - 7.5f + (float) (2 * (Math.random() - 0.5f)), y - 7.5f + (float) (2 * (Math.random() - 0.5f))); - } else if (Minecraft.getInstance().options.getCameraType() == CameraType.THIRD_PERSON_BACK) { + } else if (mc.options.getCameraType() != CameraType.FIRST_PERSON) { poseStack.pushPose(); - poseStack.rotateAround(Axis.ZP.rotationDegrees(iHelicopterEntity.getRotZ(deltaTracker.getGameTimeDeltaPartialTick(true))), x, y, 0); + poseStack.rotateAround(Axis.ZP.rotationDegrees(iHelicopterEntity.getRotZ(partialTick)), x, y, 0); preciseBlit(guiGraphics, Mod.loc("textures/screens/drone.png"), x - 8, y - 8, 0, 0, 16, 16, 16, 16); renderKillIndicator(guiGraphics, x - 7.5f + (float) (2 * (Math.random() - 0.5f)), y - 7.5f + (float) (2 * (Math.random() - 0.5f))); diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Ah6Entity.java b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Ah6Entity.java index 5c637e9e9..c196d56fa 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Ah6Entity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Ah6Entity.java @@ -38,6 +38,7 @@ import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.Explosion; import net.minecraft.world.level.Level; +import net.minecraft.world.phys.Vec2; import net.minecraft.world.phys.Vec3; import net.neoforged.api.distmarker.Dist; import net.neoforged.api.distmarker.OnlyIn; @@ -69,8 +70,14 @@ public class Ah6Entity extends ContainerMobileVehicleEntity implements GeoEntity public int holdPowerTick; public float destroyRot; - public Ah6Entity(EntityType pEntityType, Level pLevel) { - super(pEntityType, pLevel); + public float delta_xo; + public float delta_yo; + + public float delta_x; + public float delta_y; + + public Ah6Entity(EntityType type, Level world) { + super(type, world); } @Override @@ -154,6 +161,8 @@ public class Ah6Entity extends ContainerMobileVehicleEntity implements GeoEntity @Override public void baseTick() { + delta_xo = delta_x; + delta_yo = delta_y; super.baseTick(); if (this.level() instanceof ServerLevel) { @@ -234,8 +243,6 @@ public class Ah6Entity extends ContainerMobileVehicleEntity implements GeoEntity this.entityData.set(POWER, this.entityData.get(POWER) * 0.99f); } } else if (passenger instanceof Player) { - diffY = Math.clamp(-90f, 90f, Mth.wrapDegrees(passenger.getYHeadRot() - this.getYRot())); - diffX = Math.clamp(-60f, 60f, Mth.wrapDegrees(passenger.getXRot() - this.getXRot())); if (rightInputDown) { holdTick++; @@ -247,9 +254,12 @@ public class Ah6Entity extends ContainerMobileVehicleEntity implements GeoEntity holdTick = 0; } - this.setYRot(this.getYRot() + Mth.clamp((this.onGround() ? 0.1f : 2f) * diffY * this.entityData.get(PROPELLER_ROT), -10f, 10f)); - this.setXRot(Mth.clamp(this.getXRot() + ((this.onGround()) ? 0 : 1.5f) * diffX * this.entityData.get(PROPELLER_ROT), -80, 80)); - this.setZRot(this.getRoll() - this.entityData.get(DELTA_ROT) + (this.onGround() ? 0 : 0.25f) * diffY * this.entityData.get(PROPELLER_ROT)); + delta_x = ((this.onGround()) ? 0 : 1.5f) * entityData.get(MOUSE_SPEED_Y) * this.entityData.get(PROPELLER_ROT); + delta_y = Mth.clamp((this.onGround() ? 0.1f : 2f) * entityData.get(MOUSE_SPEED_X) * this.entityData.get(PROPELLER_ROT), -10f, 10f); + + this.setYRot(this.getYRot() + delta_y); + this.setXRot(this.getXRot() + delta_x); + this.setZRot(this.getRoll() - this.entityData.get(DELTA_ROT) + (this.onGround() ? 0 : 0.25f) * entityData.get(MOUSE_SPEED_X) * this.entityData.get(PROPELLER_ROT)); } if (this.level() instanceof ServerLevel) { @@ -352,7 +362,13 @@ public class Ah6Entity extends ContainerMobileVehicleEntity implements GeoEntity } protected void clampRotation(Entity entity) { - if (entity == getNthEntity(0) || entity == getNthEntity(1)) { + if (entity == getNthEntity(1)) { + float f2 = Mth.wrapDegrees(entity.getYRot() - this.getYRot()); + float f3 = Mth.clamp(f2, -80.0F, 80.0F); + entity.yRotO += f3 - f2; + entity.setYRot(entity.getYRot() + f3 - f2); + entity.setYBodyRot(this.getYRot()); + } else if (entity == getNthEntity(1)) { float f = Mth.wrapDegrees(entity.getXRot()); float f1 = Mth.clamp(f, -80.0F, 80F); entity.xRotO += f1 - f; @@ -424,7 +440,12 @@ public class Ah6Entity extends ContainerMobileVehicleEntity implements GeoEntity } public void copyEntityData(Entity entity) { - if (entity == getNthEntity(0) || entity == getNthEntity(1)) { + if (entity == getNthEntity(0)) { + entity.setXRot(entity.getXRot() + delta_x); + entity.setYHeadRot(entity.getYHeadRot() + delta_y); + entity.setYRot(entity.getYRot() + delta_y); + entity.setYBodyRot(this.getYRot()); + } else if (entity == getNthEntity(1)) { float f = Mth.wrapDegrees(entity.getYRot() - getYRot()); float g = Mth.clamp(f, -105.0f, 105.0f); entity.yRotO += g - f; @@ -651,7 +672,7 @@ public class Ah6Entity extends ContainerMobileVehicleEntity implements GeoEntity @Override public double getSensitivity(double original, boolean zoom, int seatIndex, boolean isOnGround) { - return seatIndex == 0 && !isOnGround ? 0.33 : original; + return seatIndex == 0 ? 0 : original; } @OnlyIn(Dist.CLIENT) @@ -664,4 +685,32 @@ public class Ah6Entity extends ContainerMobileVehicleEntity implements GeoEntity } return Pair.of(Axis.XP.rotationDegrees(-this.getViewXRot(tickDelta)), Axis.ZP.rotationDegrees(-this.getRoll(tickDelta))); } + + @OnlyIn(Dist.CLIENT) + @Override + public @Nullable Vec2 getCameraRotation(float partialTicks, Player player, boolean zoom, boolean isFirstPerson) { + if (this.getSeatIndex(player) == 0) { + return new Vec2(getRotY(partialTicks) + 0.4f * Mth.lerp(partialTicks, delta_yo, delta_y), getRotX(partialTicks) + 0.4f * Mth.lerp(partialTicks, delta_xo, delta_x)); + } + + return super.getCameraRotation(partialTicks, player, false, false); + } + + @OnlyIn(Dist.CLIENT) + @Override + public Vec3 getCameraPosition(float partialTicks, Player player, boolean zoom, boolean isFirstPerson) { + if (this.getSeatIndex(player) == 0) { + Matrix4f transform = getVehicleTransform(partialTicks); + + + Vector4f worldPosition = transformPosition(transform, -2.1f, 1, -10); + + if (isFirstPerson) { + return new Vec3(Mth.lerp(partialTicks, player.xo, player.getX()), Mth.lerp(partialTicks, player.yo + player.getEyeHeight(), player.getEyeY()), Mth.lerp(partialTicks, player.zo, player.getZ())); + } else { + return new Vec3(worldPosition.x, worldPosition.y, worldPosition.z); + } + } + return super.getCameraPosition(partialTicks, player, false, false); + } } diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/base/VehicleEntity.java b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/base/VehicleEntity.java index b5b6394d2..f07a178b4 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/base/VehicleEntity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/base/VehicleEntity.java @@ -80,6 +80,8 @@ public abstract class VehicleEntity extends Entity { public static final EntityDataAccessor LAST_ATTACKER_UUID = SynchedEntityData.defineId(VehicleEntity.class, EntityDataSerializers.STRING); public static final EntityDataAccessor LAST_DRIVER_UUID = SynchedEntityData.defineId(VehicleEntity.class, EntityDataSerializers.STRING); public static final EntityDataAccessor DELTA_ROT = SynchedEntityData.defineId(VehicleEntity.class, EntityDataSerializers.FLOAT); + public static final EntityDataAccessor MOUSE_SPEED_X = SynchedEntityData.defineId(VehicleEntity.class, EntityDataSerializers.FLOAT); + public static final EntityDataAccessor MOUSE_SPEED_Y = SynchedEntityData.defineId(VehicleEntity.class, EntityDataSerializers.FLOAT); public static final EntityDataAccessor> SELECTED_WEAPON = SynchedEntityData.defineId(VehicleEntity.class, ModSerializers.INT_LIST_SERIALIZER.get()); public static final EntityDataAccessor HEAT = SynchedEntityData.defineId(VehicleEntity.class, EntityDataSerializers.INT); @@ -110,6 +112,12 @@ public abstract class VehicleEntity extends Entity { public boolean cannotFire; + + public void mouseInput(double x, double y) { + entityData.set(MOUSE_SPEED_X, (float) x); + entityData.set(MOUSE_SPEED_Y, (float) y); + } + // 自定义骑乘 private final List orderedPassengers = generatePassengersList(); @@ -284,6 +292,8 @@ public abstract class VehicleEntity extends Entity { .define(LAST_ATTACKER_UUID, "undefined") .define(LAST_DRIVER_UUID, "undefined") .define(DELTA_ROT, 0f) + .define(MOUSE_SPEED_X, 0f) + .define(MOUSE_SPEED_Y, 0f) .define(HEAT, 0) .define(SELECTED_WEAPON, IntList.of(new int[this.getMaxPassengers()])); // 怎么还不给玩动态注册了(恼) @@ -597,6 +607,16 @@ public abstract class VehicleEntity extends Entity { yRotO = delta + getYRot(); } + float deltaX = Math.abs(getXRot() - xRotO); + while (getXRot() > 180F) { + setXRot(getXRot() - 360F); + xRotO = getXRot() - deltaX; + } + while (getXRot() <= -180F) { + setXRot(getXRot() + 360F); + xRotO = deltaX + getXRot(); + } + float deltaZ = Math.abs(getRoll() - prevRoll); while (getRoll() > 180F) { setZRot(getRoll() - 360F); diff --git a/src/main/java/com/atsuishio/superbwarfare/event/ClientMouseHandler.java b/src/main/java/com/atsuishio/superbwarfare/event/ClientMouseHandler.java index 8b468f454..3c57efe12 100644 --- a/src/main/java/com/atsuishio/superbwarfare/event/ClientMouseHandler.java +++ b/src/main/java/com/atsuishio/superbwarfare/event/ClientMouseHandler.java @@ -8,6 +8,7 @@ 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.NBTTool; import net.minecraft.client.CameraType; import net.minecraft.client.Minecraft; @@ -20,7 +21,8 @@ import net.neoforged.api.distmarker.Dist; import net.neoforged.bus.api.SubscribeEvent; import net.neoforged.fml.common.EventBusSubscriber; import net.neoforged.neoforge.client.event.CalculatePlayerTurnEvent; -import net.neoforged.neoforge.client.event.ViewportEvent; +import net.neoforged.neoforge.client.event.ClientTickEvent; +import net.neoforged.neoforge.network.PacketDistributor; import static com.atsuishio.superbwarfare.event.ClientEventHandler.droneFovLerp; import static com.atsuishio.superbwarfare.event.ClientEventHandler.isFreeCam; @@ -35,6 +37,10 @@ public class ClientMouseHandler { public static double PosY = 0; public static double lerpPosY = 0; + + public static double speedX = 0; + public static double speedY = 0; + public static double freeCameraPitch = 0; public static double freeCameraYaw = 0; @@ -48,43 +54,61 @@ public class ClientMouseHandler { } @SubscribeEvent - public static void handleClientTick(ViewportEvent.ComputeCameraAngles event) { + public static void handleClientTick(ClientTickEvent.Post event) { LocalPlayer player = Minecraft.getInstance().player; if (player == null) return; posO = posN; posN = MouseMovementHandler.getMousePos(); - if (!notInGame()) { - mousePos = posN.add(posO.scale(-1)); + if (!notInGame() && player.getVehicle() instanceof VehicleEntity vehicle) { + speedX = 0.1 * (posN.x - posO.x); + speedY = 0.1 * (posN.y - posO.y); - if (mousePos.x != 0) { - lerpPosX = Mth.lerp(0.1, PosX, mousePos.x); + lerpPosX = Mth.lerp(0.4, lerpPosX, speedX); + lerpPosY = Mth.lerp(0.4, lerpPosY, speedY); + + double i = 0; + + if (vehicle.getRoll() < 0) { + i = 1; + } else if (vehicle.getRoll() > 0) { + i = -1; } - if (mousePos.y != 0) { - lerpPosY = Mth.lerp(0.1, PosY, mousePos.y); + + if (Mth.abs(vehicle.getRoll()) > 90) { + i *= (1 - (Mth.abs(vehicle.getRoll()) - 90) / 90); + } + + if (Minecraft.getInstance().options.getCameraType() == CameraType.FIRST_PERSON) { + PacketDistributor.sendToServer(new MouseMoveMessage( + (1 - (Mth.abs(vehicle.getRoll()) / 90)) * lerpPosX + ((Mth.abs(vehicle.getRoll()) / 90)) * lerpPosY * i, + (1 - (Mth.abs(vehicle.getRoll()) / 90)) * lerpPosY + ((Mth.abs(vehicle.getRoll()) / 90)) * lerpPosX * (vehicle.getRoll() < 0 ? -1 : 1)) + ); + } else { + PacketDistributor.sendToServer(new MouseMoveMessage(lerpPosX, lerpPosY)); } } - lerpPosX = Mth.clamp(Mth.lerp(event.getPartialTick(), lerpPosX, 0), -1, 1); - lerpPosY = Mth.clamp(Mth.lerp(event.getPartialTick(), lerpPosY, 0), -1, 1); - - - if (isFreeCam(player)) { - freeCameraYaw = Mth.clamp(freeCameraYaw + 4 * lerpPosX, -100, 100); - freeCameraPitch = Mth.clamp(freeCameraPitch + 4 * lerpPosY, -90, 90); - } - - float yaw = event.getYaw(); - float pitch = event.getPitch(); - - event.setYaw((float) (yaw + freeCameraYaw)); - event.setPitch((float) (pitch + freeCameraPitch)); - - if (!isFreeCam(player)) { - freeCameraYaw *= 0.8; - freeCameraPitch *= 0.8; - } +// lerpPosX = Mth.clamp(Mth.lerp(event.getPartialTick(), lerpPosX, 0), -1, 1); +// lerpPosY = Mth.clamp(Mth.lerp(event.getPartialTick(), lerpPosY, 0), -1, 1); +// +// +// if (isFreeCam(player)) { +// freeCameraYaw = Mth.clamp(freeCameraYaw + 4 * lerpPosX, -100, 100); +// freeCameraPitch = Mth.clamp(freeCameraPitch + 4 * lerpPosY, -90, 90); +// } +// +// float yaw = event.getYaw(); +// float pitch = event.getPitch(); +// +// event.setYaw((float) (yaw + freeCameraYaw)); +// event.setPitch((float) (pitch + freeCameraPitch)); +// +// if (!isFreeCam(player)) { +// freeCameraYaw *= 0.8; +// freeCameraPitch *= 0.8; +// } } @SubscribeEvent diff --git a/src/main/java/com/atsuishio/superbwarfare/network/NetworkRegistry.java b/src/main/java/com/atsuishio/superbwarfare/network/NetworkRegistry.java index 0f4680455..fd6916bec 100644 --- a/src/main/java/com/atsuishio/superbwarfare/network/NetworkRegistry.java +++ b/src/main/java/com/atsuishio/superbwarfare/network/NetworkRegistry.java @@ -58,5 +58,6 @@ public class NetworkRegistry { registrar.playToServer(ShowChargingRangeMessage.TYPE, ShowChargingRangeMessage.STREAM_CODEC, ShowChargingRangeMessage::handler); registrar.playToServer(TacticalSprintMessage.TYPE, TacticalSprintMessage.STREAM_CODEC, TacticalSprintMessage::handler); registrar.playToServer(DogTagFinishEditMessage.TYPE, DogTagFinishEditMessage.STREAM_CODEC, DogTagFinishEditMessage::handler); + registrar.playToServer(MouseMoveMessage.TYPE, MouseMoveMessage.STREAM_CODEC, MouseMoveMessage::handler); } } diff --git a/src/main/java/com/atsuishio/superbwarfare/network/message/send/MouseMoveMessage.java b/src/main/java/com/atsuishio/superbwarfare/network/message/send/MouseMoveMessage.java new file mode 100644 index 000000000..c91786492 --- /dev/null +++ b/src/main/java/com/atsuishio/superbwarfare/network/message/send/MouseMoveMessage.java @@ -0,0 +1,36 @@ +package com.atsuishio.superbwarfare.network.message.send; + +import com.atsuishio.superbwarfare.Mod; +import com.atsuishio.superbwarfare.entity.vehicle.base.VehicleEntity; +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.neoforged.neoforge.network.handling.IPayloadContext; +import org.jetbrains.annotations.NotNull; + +public record MouseMoveMessage(double speedX, double speedY) implements CustomPacketPayload { + public static final Type TYPE = new Type<>(Mod.loc("mouse_move")); + + public static final StreamCodec STREAM_CODEC = StreamCodec.composite( + ByteBufCodecs.DOUBLE, + MouseMoveMessage::speedX, + ByteBufCodecs.DOUBLE, + MouseMoveMessage::speedY, + MouseMoveMessage::new + ); + + public static void handler(MouseMoveMessage message, final IPayloadContext context) { + var player = context.player(); + var entity = player.getVehicle(); + + if (entity instanceof VehicleEntity vehicle) { + vehicle.mouseInput(message.speedX, message.speedY); + } + } + + @Override + public @NotNull Type type() { + return TYPE; + } +}