From 0bcbedd6056156df823b1cbb82eaeeeae6e230c1 Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Tue, 26 Jul 2022 21:59:57 +0200 Subject: [PATCH] Added block color sync. --- .../java/nl/andrewl/aos2_client/Client.java | 16 +++++++++++++++- .../aos2_client/CommunicationHandler.java | 1 + .../aos2_client/config/ClientConfig.java | 4 ++-- .../aos2_client/control/InputHandler.java | 12 ++++++++++++ .../andrewl/aos2_client/model/OtherPlayer.java | 8 ++++++++ .../aos2_client/render/GameRenderer.java | 12 ++++++++---- .../andrewl/aos2_client/sound/SoundManager.java | 2 +- .../andrewl/aos_core/model/item/Inventory.java | 17 +++++++++++++++++ .../aos_core/model/world/ColorPalette.java | 2 +- .../nl/andrewl/aos_core/model/world/World.java | 2 +- .../aos_core/net/client/PlayerJoinMessage.java | 14 ++------------ .../aos2_server/ClientCommunicationHandler.java | 1 + .../nl/andrewl/aos2_server/PlayerManager.java | 15 ++++++++++----- 13 files changed, 79 insertions(+), 27 deletions(-) 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 8c09b00..d09fae1 100644 --- a/client/src/main/java/nl/andrewl/aos2_client/Client.java +++ b/client/src/main/java/nl/andrewl/aos2_client/Client.java @@ -100,6 +100,7 @@ public class Client implements Runnable { gameRenderer.draw(); lastFrameAt = now; } + soundManager.free(); gameRenderer.freeWindow(); communicationHandler.shutdown(); } @@ -139,6 +140,11 @@ public class Client implements Runnable { myPlayer.getInventory().setSelectedIndex(selectedStackMessage.index()); } else if (msg instanceof ItemStackMessage itemStackMessage) { myPlayer.getInventory().getItemStacks().set(itemStackMessage.index(), itemStackMessage.stack()); + } else if (msg instanceof BlockColorMessage blockColorMessage) { + OtherPlayer player = players.get(blockColorMessage.clientId()); + if (player != null) { + player.setSelectedBlockValue(blockColorMessage.block()); + } } else if (msg instanceof PlayerJoinMessage joinMessage) { Player p = joinMessage.toPlayer(); OtherPlayer op = new OtherPlayer(p.getId(), p.getUsername()); @@ -149,11 +155,19 @@ public class Client implements Runnable { op.getVelocity().set(p.getVelocity()); op.getOrientation().set(p.getOrientation()); op.setHeldItemId(joinMessage.selectedItemId()); + op.setSelectedBlockValue(joinMessage.selectedBlockValue()); players.put(op.getId(), op); } else if (msg instanceof PlayerLeaveMessage leaveMessage) { players.remove(leaveMessage.id()); } else if (msg instanceof SoundMessage soundMessage) { - soundManager.play(soundMessage.name(), soundMessage.gain(), new Vector3f(soundMessage.px(), soundMessage.py(), soundMessage.pz())); + if (soundManager != null) { + soundManager.play( + soundMessage.name(), + soundMessage.gain(), + new Vector3f(soundMessage.px(), soundMessage.py(), soundMessage.pz()), + new Vector3f(soundMessage.vx(), soundMessage.vy(), soundMessage.vz()) + ); + } } else if (msg instanceof ProjectileMessage pm) { Projectile p = projectiles.get(pm.id()); if (p == null && !pm.destroyed()) { diff --git a/client/src/main/java/nl/andrewl/aos2_client/CommunicationHandler.java b/client/src/main/java/nl/andrewl/aos2_client/CommunicationHandler.java index afac7d2..c44631f 100644 --- a/client/src/main/java/nl/andrewl/aos2_client/CommunicationHandler.java +++ b/client/src/main/java/nl/andrewl/aos2_client/CommunicationHandler.java @@ -181,6 +181,7 @@ public class CommunicationHandler { player.getOrientation().set(in.readFloat(), in.readFloat()); player.setCrouching(in.readBoolean()); player.setHeldItemId(in.readInt()); + player.setSelectedBlockValue(in.readByte()); client.getPlayers().put(player.getId(), player); } diff --git a/client/src/main/java/nl/andrewl/aos2_client/config/ClientConfig.java b/client/src/main/java/nl/andrewl/aos2_client/config/ClientConfig.java index b4021ee..16b1b0b 100644 --- a/client/src/main/java/nl/andrewl/aos2_client/config/ClientConfig.java +++ b/client/src/main/java/nl/andrewl/aos2_client/config/ClientConfig.java @@ -12,8 +12,8 @@ public class ClientConfig { } public static class DisplayConfig { - public boolean fullscreen = false; - public boolean captureCursor = false; + public boolean fullscreen = true; + public boolean captureCursor = true; public float fov = 70; } } diff --git a/client/src/main/java/nl/andrewl/aos2_client/control/InputHandler.java b/client/src/main/java/nl/andrewl/aos2_client/control/InputHandler.java index 5a89ba5..ec230c3 100644 --- a/client/src/main/java/nl/andrewl/aos2_client/control/InputHandler.java +++ b/client/src/main/java/nl/andrewl/aos2_client/control/InputHandler.java @@ -19,6 +19,18 @@ public class InputHandler { private ClientInputState lastInputState = null; + private boolean forward; + private boolean backward; + private boolean left; + private boolean right; + private boolean jumping; + private boolean crouching; + private boolean sprinting; + private boolean hitting; + private boolean interacting; + private boolean reloading; + + public InputHandler(Client client, CommunicationHandler comm) { this.client = client; this.comm = comm; diff --git a/client/src/main/java/nl/andrewl/aos2_client/model/OtherPlayer.java b/client/src/main/java/nl/andrewl/aos2_client/model/OtherPlayer.java index f155773..c975772 100644 --- a/client/src/main/java/nl/andrewl/aos2_client/model/OtherPlayer.java +++ b/client/src/main/java/nl/andrewl/aos2_client/model/OtherPlayer.java @@ -46,6 +46,14 @@ public class OtherPlayer extends Player { this.heldItemId = heldItemId; } + public byte getSelectedBlockValue() { + return selectedBlockValue; + } + + public void setSelectedBlockValue(byte selectedBlockValue) { + this.selectedBlockValue = selectedBlockValue; + } + @Override public void setPosition(Vector3f position) { super.setPosition(position); diff --git a/client/src/main/java/nl/andrewl/aos2_client/render/GameRenderer.java b/client/src/main/java/nl/andrewl/aos2_client/render/GameRenderer.java index 31b4683..60f7361 100644 --- a/client/src/main/java/nl/andrewl/aos2_client/render/GameRenderer.java +++ b/client/src/main/java/nl/andrewl/aos2_client/render/GameRenderer.java @@ -18,7 +18,6 @@ import org.joml.Matrix4f; import org.joml.Vector3f; import org.lwjgl.glfw.*; import org.lwjgl.opengl.GL; -import org.lwjgl.opengl.GLUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -232,16 +231,16 @@ public class GameRenderer { } } smgModel.unbind(); - blockModel.bind(); - if (client.getMyPlayer().getInventory().getSelectedItemStack().getType().getId() == ItemTypes.BLOCK.getId()) { - BlockItemStack stack = (BlockItemStack) client.getMyPlayer().getInventory().getSelectedItemStack(); + if (myPlayer.getInventory().getSelectedItemStack().getType().getId() == ItemTypes.BLOCK.getId()) { + BlockItemStack stack = (BlockItemStack) myPlayer.getInventory().getSelectedItemStack(); modelRenderer.setAspectColor(client.getWorld().getPalette().getColor(stack.getSelectedValue())); modelRenderer.render(blockModel, myPlayer.getHeldItemTransformData(), myPlayer.getHeldItemNormalTransformData()); } modelRenderer.setAspectColor(new Vector3f(0.5f, 0.5f, 0.5f)); for (var player : client.getPlayers().values()) { if (player.getHeldItemId() == ItemTypes.BLOCK.getId()) { + modelRenderer.setAspectColor(client.getWorld().getPalette().getColor(player.getSelectedBlockValue())); modelRenderer.render(blockModel, player.getHeldItemTransformData(), player.getHeldItemNormalTransformData()); } } @@ -319,7 +318,12 @@ public class GameRenderer { } public void freeWindow() { + if (rifleModel != null) rifleModel.free(); + if (smgModel != null) smgModel.free(); + if (flagModel != null) flagModel.free(); + if (bulletModel != null) bulletModel.free(); if (playerModel != null) playerModel.free(); + if (blockModel != null) blockModel.free(); if (modelRenderer != null) modelRenderer.free(); if (guiRenderer != null) guiRenderer.free(); if (chunkRenderer != null) chunkRenderer.free(); diff --git a/client/src/main/java/nl/andrewl/aos2_client/sound/SoundManager.java b/client/src/main/java/nl/andrewl/aos2_client/sound/SoundManager.java index f079eaf..dea7f47 100644 --- a/client/src/main/java/nl/andrewl/aos2_client/sound/SoundManager.java +++ b/client/src/main/java/nl/andrewl/aos2_client/sound/SoundManager.java @@ -26,7 +26,7 @@ import static org.lwjgl.openal.ALC10.*; public class SoundManager { private static final Logger log = LoggerFactory.getLogger(SoundManager.class); - private static final int SOURCE_COUNT = 16; + private static final int SOURCE_COUNT = 32; private final long alContext; private final Map audioBuffers = new HashMap<>(); diff --git a/core/src/main/java/nl/andrewl/aos_core/model/item/Inventory.java b/core/src/main/java/nl/andrewl/aos_core/model/item/Inventory.java index cb20212..90ea7ff 100644 --- a/core/src/main/java/nl/andrewl/aos_core/model/item/Inventory.java +++ b/core/src/main/java/nl/andrewl/aos_core/model/item/Inventory.java @@ -1,6 +1,7 @@ package nl.andrewl.aos_core.model.item; import java.util.List; +import java.util.Optional; /** * Represents the contents and current state of a player's inventory. @@ -38,4 +39,20 @@ public class Inventory { if (newIndex > itemStacks.size() - 1) newIndex = itemStacks.size() - 1; this.selectedIndex = newIndex; } + + public Optional getItemStack(Item itemType) { + for (var stack : itemStacks) { + if (stack.getType().equals(itemType)) return Optional.of(stack); + } + return Optional.empty(); + } + + public byte getSelectedBlockValue() { + for (var stack : itemStacks) { + if (stack instanceof BlockItemStack b) { + return b.getSelectedValue(); + } + } + return 1; + } } diff --git a/core/src/main/java/nl/andrewl/aos_core/model/world/ColorPalette.java b/core/src/main/java/nl/andrewl/aos_core/model/world/ColorPalette.java index a7c4e25..6b348ab 100644 --- a/core/src/main/java/nl/andrewl/aos_core/model/world/ColorPalette.java +++ b/core/src/main/java/nl/andrewl/aos_core/model/world/ColorPalette.java @@ -19,7 +19,7 @@ public class ColorPalette { } public Vector3f getColor(byte value) { - if (value < 0) return null; + if (value <= 0) return null; return colors[value - 1]; } diff --git a/core/src/main/java/nl/andrewl/aos_core/model/world/World.java b/core/src/main/java/nl/andrewl/aos_core/model/world/World.java index 398877b..80d97bf 100644 --- a/core/src/main/java/nl/andrewl/aos_core/model/world/World.java +++ b/core/src/main/java/nl/andrewl/aos_core/model/world/World.java @@ -17,7 +17,7 @@ import java.util.Map; * that players can interact in. */ public class World { - private static final float DELTA = 0.0001f; + private static final float DELTA = 0.01f; protected final Map chunkMap = new HashMap<>(); protected ColorPalette palette; diff --git a/core/src/main/java/nl/andrewl/aos_core/net/client/PlayerJoinMessage.java b/core/src/main/java/nl/andrewl/aos_core/net/client/PlayerJoinMessage.java index 2d239cf..d3e9480 100644 --- a/core/src/main/java/nl/andrewl/aos_core/net/client/PlayerJoinMessage.java +++ b/core/src/main/java/nl/andrewl/aos_core/net/client/PlayerJoinMessage.java @@ -14,19 +14,9 @@ public record PlayerJoinMessage( float vx, float vy, float vz, float ox, float oy, boolean crouching, - int selectedItemId + int selectedItemId, + byte selectedBlockValue ) implements Message { - public PlayerJoinMessage(Player player) { - this( - player.getId(), player.getUsername(), player.getTeam() == null ? -1 : player.getTeam().getId(), - player.getPosition().x, player.getPosition().y, player.getPosition().z, - player.getVelocity().x, player.getVelocity().y, player.getVelocity().z, - player.getOrientation().x, player.getOrientation().y, - player.isCrouching(), - ItemTypes.BLOCK.getId() - ); - } - public Player toPlayer() { Player p = new Player(id, username); p.getPosition().set(px, py, pz); diff --git a/server/src/main/java/nl/andrewl/aos2_server/ClientCommunicationHandler.java b/server/src/main/java/nl/andrewl/aos2_server/ClientCommunicationHandler.java index 567c886..393b4e3 100644 --- a/server/src/main/java/nl/andrewl/aos2_server/ClientCommunicationHandler.java +++ b/server/src/main/java/nl/andrewl/aos2_server/ClientCommunicationHandler.java @@ -220,6 +220,7 @@ public class ClientCommunicationHandler { out.writeBoolean(player.isCrouching()); out.writeInt(player.getInventory().getSelectedItemStack().getType().getId()); + out.writeByte(player.getInventory().getSelectedBlockValue()); } // Send the player's own inventory data. diff --git a/server/src/main/java/nl/andrewl/aos2_server/PlayerManager.java b/server/src/main/java/nl/andrewl/aos2_server/PlayerManager.java index 87301ae..fd6541e 100644 --- a/server/src/main/java/nl/andrewl/aos2_server/PlayerManager.java +++ b/server/src/main/java/nl/andrewl/aos2_server/PlayerManager.java @@ -3,10 +3,7 @@ package nl.andrewl.aos2_server; import nl.andrewl.aos2_server.model.ServerPlayer; import nl.andrewl.aos_core.Net; import nl.andrewl.aos_core.model.Team; -import nl.andrewl.aos_core.model.item.BlockItemStack; -import nl.andrewl.aos_core.model.item.Gun; -import nl.andrewl.aos_core.model.item.GunItemStack; -import nl.andrewl.aos_core.model.item.ItemStack; +import nl.andrewl.aos_core.model.item.*; import nl.andrewl.aos_core.net.client.*; import nl.andrewl.aos_core.net.connect.DatagramInit; import nl.andrewl.record_net.Message; @@ -47,7 +44,15 @@ public class PlayerManager { } player.setPosition(getBestSpawnPoint(player)); // Tell all other players that this one has joined. - broadcastTcpMessageToAllBut(new PlayerJoinMessage(player), player); + broadcastTcpMessageToAllBut(new PlayerJoinMessage( + player.getId(), player.getUsername(), player.getTeam() == null ? -1 : player.getTeam().getId(), + player.getPosition().x(), player.getPosition().y(), player.getPosition().z(), + player.getVelocity().x(), player.getVelocity().y(), player.getVelocity().z(), + player.getOrientation().x(), player.getOrientation().y(), + player.isCrouching(), + player.getInventory().getSelectedItemStack().getType().getId(), + player.getInventory().getSelectedBlockValue() + ), player); return player; }