Added block color sync.

This commit is contained in:
Andrew Lalis 2022-07-26 21:59:57 +02:00
parent 8e2537a532
commit 0bcbedd605
13 changed files with 79 additions and 27 deletions

View File

@ -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()) {

View File

@ -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);
}

View File

@ -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;
}
}

View File

@ -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;

View File

@ -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);

View File

@ -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();

View File

@ -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<String, Integer> audioBuffers = new HashMap<>();

View File

@ -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<ItemStack> 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;
}
}

View File

@ -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];
}

View File

@ -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<Vector3ic, Chunk> chunkMap = new HashMap<>();
protected ColorPalette palette;

View File

@ -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);

View File

@ -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.

View File

@ -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;
}