From 492b9f66aa140ec8ac9b985d89891649db4b9b6c Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Thu, 28 Jul 2022 23:06:18 +0200 Subject: [PATCH] Cleaned up recoil mechanic to be applied with a delta value instead of absolute. --- client/src/main/java/nl/andrewl/aos2_client/Client.java | 7 ++++--- .../aos2_client/control/PlayerInputKeyCallback.java | 6 ++++-- .../aos2_client/control/PlayerViewCursorCallback.java | 6 +----- .../nl/andrewl/aos2_client/render/gui/GuiRenderer.java | 4 ++-- core/src/main/java/nl/andrewl/aos_core/Net.java | 2 +- .../aos_core/net/client/ClientOrientationState.java | 7 ++++++- ...entationUpdateMessage.java => ClientRecoilMessage.java} | 6 +++--- .../java/nl/andrewl/aos2_server/cli/PlayersCommand.java | 2 +- .../nl/andrewl/aos2_server/logic/PlayerActionManager.java | 7 +------ 9 files changed, 23 insertions(+), 24 deletions(-) rename core/src/main/java/nl/andrewl/aos_core/net/client/{ClientOrientationUpdateMessage.java => ClientRecoilMessage.java} (62%) diff --git a/client/src/main/java/nl/andrewl/aos2_client/Client.java b/client/src/main/java/nl/andrewl/aos2_client/Client.java index a756b2d..e127b72 100644 --- a/client/src/main/java/nl/andrewl/aos2_client/Client.java +++ b/client/src/main/java/nl/andrewl/aos2_client/Client.java @@ -1,7 +1,7 @@ package nl.andrewl.aos2_client; import nl.andrewl.aos2_client.config.ClientConfig; -import nl.andrewl.aos2_client.control.*; +import nl.andrewl.aos2_client.control.InputHandler; import nl.andrewl.aos2_client.model.Chat; import nl.andrewl.aos2_client.model.ClientPlayer; import nl.andrewl.aos2_client.model.OtherPlayer; @@ -200,12 +200,13 @@ public class Client implements Runnable { if (soundManager != null) { soundManager.play("chat", 1, myPlayer.getEyePosition(), myPlayer.getVelocity()); } - } else if (msg instanceof ClientOrientationUpdateMessage orientationUpdateMessage) { + } else if (msg instanceof ClientRecoilMessage recoil) { runLater(() -> { - myPlayer.setOrientation(orientationUpdateMessage.x(), orientationUpdateMessage.y()); + myPlayer.setOrientation(myPlayer.getOrientation().x + recoil.dx(), myPlayer.getOrientation().y + recoil.dy()); if (gameRenderer != null) { gameRenderer.getCamera().setOrientationToPlayer(myPlayer); } + communicationHandler.sendDatagramPacket(ClientOrientationState.fromPlayer(myPlayer)); }); } } diff --git a/client/src/main/java/nl/andrewl/aos2_client/control/PlayerInputKeyCallback.java b/client/src/main/java/nl/andrewl/aos2_client/control/PlayerInputKeyCallback.java index adccdbf..c18d35b 100644 --- a/client/src/main/java/nl/andrewl/aos2_client/control/PlayerInputKeyCallback.java +++ b/client/src/main/java/nl/andrewl/aos2_client/control/PlayerInputKeyCallback.java @@ -55,8 +55,10 @@ public class PlayerInputKeyCallback implements GLFWKeyCallbackI { case GLFW_KEY_T -> inputHandler.enableChatting(); case GLFW_KEY_SLASH -> { - inputHandler.enableChatting(); - inputHandler.appendToChat("/"); + if (!inputHandler.isChatting()) { + inputHandler.enableChatting(); + inputHandler.appendToChat("/"); + } } } } else if (action == GLFW_REPEAT) { diff --git a/client/src/main/java/nl/andrewl/aos2_client/control/PlayerViewCursorCallback.java b/client/src/main/java/nl/andrewl/aos2_client/control/PlayerViewCursorCallback.java index e18e833..17ff234 100644 --- a/client/src/main/java/nl/andrewl/aos2_client/control/PlayerViewCursorCallback.java +++ b/client/src/main/java/nl/andrewl/aos2_client/control/PlayerViewCursorCallback.java @@ -55,11 +55,7 @@ public class PlayerViewCursorCallback implements GLFWCursorPosCallbackI { camera.setOrientationToPlayer(client.getMyPlayer()); long now = System.currentTimeMillis(); if (lastOrientationUpdateSentAt + ORIENTATION_UPDATE_LIMIT < now) { - ForkJoinPool.commonPool().submit(() -> comm.sendDatagramPacket(new ClientOrientationState( - client.getMyPlayer().getId(), - client.getMyPlayer().getOrientation().x, - client.getMyPlayer().getOrientation().y - ))); + ForkJoinPool.commonPool().submit(() -> comm.sendDatagramPacket(ClientOrientationState.fromPlayer(client.getMyPlayer()))); lastOrientationUpdateSentAt = now; } } diff --git a/client/src/main/java/nl/andrewl/aos2_client/render/gui/GuiRenderer.java b/client/src/main/java/nl/andrewl/aos2_client/render/gui/GuiRenderer.java index b0d6c84..68d6cdf 100644 --- a/client/src/main/java/nl/andrewl/aos2_client/render/gui/GuiRenderer.java +++ b/client/src/main/java/nl/andrewl/aos2_client/render/gui/GuiRenderer.java @@ -369,10 +369,10 @@ public class GuiRenderer { nvgTextAlign(vgId, NVG_ALIGN_LEFT | NVG_ALIGN_TOP); nvgFillColor(vgId, GuiUtils.rgba(1, 1, 1, 1, colorA)); var pos = client.getMyPlayer().getPosition(); - nvgText(vgId, 5, y, String.format("Pos: x=%.3f, y=%.3f, z=%.3f", pos.x, pos.y, pos.z)); + nvgText(vgId, 5, y, String.format("Pos: dx=%.3f, dy=%.3f, z=%.3f", pos.x, pos.y, pos.z)); y += 12; var vel = client.getMyPlayer().getVelocity(); - nvgText(vgId, 5, y, String.format("Vel: x=%.3f, y=%.3f, z=%.3f, speed=%.3f", vel.x, vel.y, vel.z, vel.length())); + nvgText(vgId, 5, y, String.format("Vel: dx=%.3f, dy=%.3f, z=%.3f, speed=%.3f", vel.x, vel.y, vel.z, vel.length())); y += 12; var view = client.getMyPlayer().getOrientation(); nvgText(vgId, 5, y, String.format("View: horizontal=%.3f, vertical=%.3f", Math.toDegrees(view.x), Math.toDegrees(view.y))); diff --git a/core/src/main/java/nl/andrewl/aos_core/Net.java b/core/src/main/java/nl/andrewl/aos_core/Net.java index 2a9f836..17b34a6 100644 --- a/core/src/main/java/nl/andrewl/aos_core/Net.java +++ b/core/src/main/java/nl/andrewl/aos_core/Net.java @@ -48,7 +48,7 @@ public final class Net { serializer.registerType(19, BlockColorMessage.class); serializer.registerType(20, ChatMessage.class); serializer.registerType(21, ChatWrittenMessage.class); - serializer.registerType(22, ClientOrientationUpdateMessage.class); + serializer.registerType(22, ClientRecoilMessage.class); } public static ExtendedDataInputStream getInputStream(InputStream in) { diff --git a/core/src/main/java/nl/andrewl/aos_core/net/client/ClientOrientationState.java b/core/src/main/java/nl/andrewl/aos_core/net/client/ClientOrientationState.java index 4f55085..b0cca5c 100644 --- a/core/src/main/java/nl/andrewl/aos_core/net/client/ClientOrientationState.java +++ b/core/src/main/java/nl/andrewl/aos_core/net/client/ClientOrientationState.java @@ -1,5 +1,6 @@ package nl.andrewl.aos_core.net.client; +import nl.andrewl.aos_core.model.Player; import nl.andrewl.record_net.Message; /** @@ -11,4 +12,8 @@ import nl.andrewl.record_net.Message; public record ClientOrientationState( int clientId, float x, float y -) implements Message {} +) implements Message { + public static ClientOrientationState fromPlayer(Player player) { + return new ClientOrientationState(player.getId(), player.getOrientation().x, player.getOrientation().y); + } +} diff --git a/core/src/main/java/nl/andrewl/aos_core/net/client/ClientOrientationUpdateMessage.java b/core/src/main/java/nl/andrewl/aos_core/net/client/ClientRecoilMessage.java similarity index 62% rename from core/src/main/java/nl/andrewl/aos_core/net/client/ClientOrientationUpdateMessage.java rename to core/src/main/java/nl/andrewl/aos_core/net/client/ClientRecoilMessage.java index cbb0042..7b77f4d 100644 --- a/core/src/main/java/nl/andrewl/aos_core/net/client/ClientOrientationUpdateMessage.java +++ b/core/src/main/java/nl/andrewl/aos_core/net/client/ClientRecoilMessage.java @@ -4,8 +4,8 @@ import nl.andrewl.record_net.Message; /** * A message that the server sends to clients, to tell them to update their - * player's orientation to the specified values. + * player's orientation according to a recoil event. */ -public record ClientOrientationUpdateMessage( - float x, float y +public record ClientRecoilMessage( + float dx, float dy ) implements Message {} diff --git a/server/src/main/java/nl/andrewl/aos2_server/cli/PlayersCommand.java b/server/src/main/java/nl/andrewl/aos2_server/cli/PlayersCommand.java index d61123d..a1f7c4a 100644 --- a/server/src/main/java/nl/andrewl/aos2_server/cli/PlayersCommand.java +++ b/server/src/main/java/nl/andrewl/aos2_server/cli/PlayersCommand.java @@ -39,7 +39,7 @@ public class PlayersCommand { } } - @CommandLine.Command(name = "list", description = "Kicks a player from the server.") + @CommandLine.Command(name = "kick", description = "Kicks a player from the server.") public void kick( @CommandLine.Parameters(paramLabel = "player", description = "The id or name of the player to kick.") String playerIdent ) { diff --git a/server/src/main/java/nl/andrewl/aos2_server/logic/PlayerActionManager.java b/server/src/main/java/nl/andrewl/aos2_server/logic/PlayerActionManager.java index 8dbcb3f..cd4628b 100644 --- a/server/src/main/java/nl/andrewl/aos2_server/logic/PlayerActionManager.java +++ b/server/src/main/java/nl/andrewl/aos2_server/logic/PlayerActionManager.java @@ -114,12 +114,7 @@ public class PlayerActionManager { // Apply recoil! float recoilFactor = 10f; // Maximum number of degrees to recoil. float recoil = recoilFactor * gun.getRecoil() + (float) ThreadLocalRandom.current().nextGaussian(0, 0.01); - player.getOrientation().y += Math.toRadians(recoil); - server.getPlayerManager().getHandler(player.getId()).sendDatagramPacket(new ClientOrientationUpdateMessage( - player.getOrientation().x(), - player.getOrientation().y() - )); - server.getPlayerManager().broadcastUdpMessageToAllBut(player.getUpdateMessage(now), player); + server.getPlayerManager().getHandler(player.getId()).sendDatagramPacket(new ClientRecoilMessage(0, Math.toRadians(recoil))); // Play sound! String shotSound = null; if (gun instanceof Rifle) {