Made some progress on gamemodes.

This commit is contained in:
Andrew Lalis 2022-07-30 11:35:51 +02:00
parent 7e7ae0b7c1
commit 1465da7506
11 changed files with 65 additions and 21 deletions

View File

@ -8,7 +8,6 @@ import nl.andrewl.aos2_client.model.OtherPlayer;
import nl.andrewl.aos2_client.render.GameRenderer; import nl.andrewl.aos2_client.render.GameRenderer;
import nl.andrewl.aos2_client.sound.SoundManager; import nl.andrewl.aos2_client.sound.SoundManager;
import nl.andrewl.aos_core.config.Config; import nl.andrewl.aos_core.config.Config;
import nl.andrewl.aos_core.model.Player;
import nl.andrewl.aos_core.model.Projectile; import nl.andrewl.aos_core.model.Projectile;
import nl.andrewl.aos_core.model.Team; import nl.andrewl.aos_core.model.Team;
import nl.andrewl.aos_core.net.client.*; import nl.andrewl.aos_core.net.client.*;
@ -149,22 +148,11 @@ public class Client implements Runnable {
} }
} else if (msg instanceof PlayerJoinMessage joinMessage) { } else if (msg instanceof PlayerJoinMessage joinMessage) {
runLater(() -> { runLater(() -> {
Player p = joinMessage.toPlayer(); OtherPlayer op = OtherPlayer.fromJoinMessage(joinMessage, this);
OtherPlayer op = new OtherPlayer(p.getId(), p.getUsername());
if (joinMessage.teamId() != -1) {
op.setTeam(teams.get(joinMessage.teamId()));
}
op.getPosition().set(p.getPosition());
op.getVelocity().set(p.getVelocity());
op.getOrientation().set(p.getOrientation());
op.setHeldItemId(joinMessage.selectedItemId());
op.setSelectedBlockValue(joinMessage.selectedBlockValue());
players.put(op.getId(), op); players.put(op.getId(), op);
}); });
} else if (msg instanceof PlayerLeaveMessage leaveMessage) { } else if (msg instanceof PlayerLeaveMessage leaveMessage) {
runLater(() -> { runLater(() -> players.remove(leaveMessage.id()));
players.remove(leaveMessage.id());
});
} else if (msg instanceof SoundMessage soundMessage) { } else if (msg instanceof SoundMessage soundMessage) {
if (soundManager != null) { if (soundManager != null) {
soundManager.play( soundManager.play(

View File

@ -3,6 +3,7 @@ package nl.andrewl.aos2_client;
import nl.andrewl.aos2_client.model.ClientPlayer; import nl.andrewl.aos2_client.model.ClientPlayer;
import nl.andrewl.aos2_client.model.OtherPlayer; import nl.andrewl.aos2_client.model.OtherPlayer;
import nl.andrewl.aos_core.Net; import nl.andrewl.aos_core.Net;
import nl.andrewl.aos_core.model.PlayerMode;
import nl.andrewl.aos_core.model.Team; import nl.andrewl.aos_core.model.Team;
import nl.andrewl.aos_core.model.item.ItemStack; import nl.andrewl.aos_core.model.item.ItemStack;
import nl.andrewl.aos_core.model.world.World; import nl.andrewl.aos_core.model.world.World;
@ -174,6 +175,7 @@ public class CommunicationHandler {
player.setCrouching(in.readBoolean()); player.setCrouching(in.readBoolean());
player.setHeldItemId(in.readInt()); player.setHeldItemId(in.readInt());
player.setSelectedBlockValue(in.readByte()); player.setSelectedBlockValue(in.readByte());
player.setMode(PlayerMode.values()[in.readInt()]);
client.getPlayers().put(player.getId(), player); client.getPlayers().put(player.getId(), player);
} }
@ -189,5 +191,6 @@ public class CommunicationHandler {
int teamId = in.readInt(); int teamId = in.readInt();
if (teamId != -1) client.getMyPlayer().setTeam(client.getTeams().get(teamId)); if (teamId != -1) client.getMyPlayer().setTeam(client.getTeams().get(teamId));
client.getMyPlayer().getPosition().set(in.readFloat(), in.readFloat(), in.readFloat()); client.getMyPlayer().getPosition().set(in.readFloat(), in.readFloat(), in.readFloat());
client.getMyPlayer().setMode(PlayerMode.values()[in.readInt()]);
} }
} }

View File

@ -1,8 +1,10 @@
package nl.andrewl.aos2_client.model; package nl.andrewl.aos2_client.model;
import nl.andrewl.aos2_client.Camera; import nl.andrewl.aos2_client.Camera;
import nl.andrewl.aos2_client.Client;
import nl.andrewl.aos_core.model.Player; import nl.andrewl.aos_core.model.Player;
import nl.andrewl.aos_core.model.item.ItemTypes; import nl.andrewl.aos_core.model.item.ItemTypes;
import nl.andrewl.aos_core.net.client.PlayerJoinMessage;
import org.joml.Matrix3f; import org.joml.Matrix3f;
import org.joml.Matrix4f; import org.joml.Matrix4f;
import org.joml.Vector3f; import org.joml.Vector3f;
@ -99,4 +101,18 @@ public class OtherPlayer extends Player {
public float[] getHeldItemNormalTransformData() { public float[] getHeldItemNormalTransformData() {
return heldItemNormalTransformData; return heldItemNormalTransformData;
} }
public static OtherPlayer fromJoinMessage(PlayerJoinMessage msg, Client client) {
OtherPlayer op = new OtherPlayer(msg.id(), msg.username());
if (msg.teamId() != -1 && client.getTeams().containsKey(msg.teamId())) {
op.setTeam(client.getTeams().get(msg.teamId()));
}
op.getPosition().set(msg.px(), msg.py(), msg.pz());
op.getVelocity().set(msg.vx(), msg.vy(), msg.vz());
op.getOrientation().set(msg.ox(), msg.oy());
op.setHeldItemId(msg.selectedItemId());
op.setSelectedBlockValue(msg.selectedBlockValue());
op.setMode(msg.mode());
return op;
}
} }

View File

@ -8,6 +8,7 @@ import nl.andrewl.aos2_client.model.ClientPlayer;
import nl.andrewl.aos2_client.render.chunk.ChunkRenderer; import nl.andrewl.aos2_client.render.chunk.ChunkRenderer;
import nl.andrewl.aos2_client.render.gui.GuiRenderer; import nl.andrewl.aos2_client.render.gui.GuiRenderer;
import nl.andrewl.aos2_client.render.model.Model; import nl.andrewl.aos2_client.render.model.Model;
import nl.andrewl.aos_core.model.PlayerMode;
import nl.andrewl.aos_core.model.Team; import nl.andrewl.aos_core.model.Team;
import nl.andrewl.aos_core.model.item.BlockItemStack; import nl.andrewl.aos_core.model.item.BlockItemStack;
import nl.andrewl.aos_core.model.item.ItemTypes; import nl.andrewl.aos_core.model.item.ItemTypes;
@ -181,6 +182,7 @@ public class GameRenderer {
playerModel.bind(); playerModel.bind();
for (var player : client.getPlayers().values()) { for (var player : client.getPlayers().values()) {
if (player.getMode() == PlayerMode.SPECTATOR) continue;
if (player.getTeam() != null) { if (player.getTeam() != null) {
modelRenderer.setAspectColor(player.getTeam().getColor()); modelRenderer.setAspectColor(player.getTeam().getColor());
} else { } else {
@ -196,6 +198,7 @@ public class GameRenderer {
modelRenderer.render(rifleModel, myPlayer.getHeldItemTransformData(), myPlayer.getHeldItemNormalTransformData()); modelRenderer.render(rifleModel, myPlayer.getHeldItemTransformData(), myPlayer.getHeldItemNormalTransformData());
} }
for (var player : client.getPlayers().values()) { for (var player : client.getPlayers().values()) {
if (player.getMode() == PlayerMode.SPECTATOR) continue;
if (player.getHeldItemId() == ItemTypes.RIFLE.getId()) { if (player.getHeldItemId() == ItemTypes.RIFLE.getId()) {
modelRenderer.render(rifleModel, player.getHeldItemTransformData(), player.getHeldItemNormalTransformData()); modelRenderer.render(rifleModel, player.getHeldItemTransformData(), player.getHeldItemNormalTransformData());
} }
@ -206,6 +209,7 @@ public class GameRenderer {
modelRenderer.render(smgModel, myPlayer.getHeldItemTransformData(), myPlayer.getHeldItemNormalTransformData()); modelRenderer.render(smgModel, myPlayer.getHeldItemTransformData(), myPlayer.getHeldItemNormalTransformData());
} }
for (var player : client.getPlayers().values()) { for (var player : client.getPlayers().values()) {
if (player.getMode() == PlayerMode.SPECTATOR) continue;
if (player.getHeldItemId() == ItemTypes.AK_47.getId()) { if (player.getHeldItemId() == ItemTypes.AK_47.getId()) {
modelRenderer.render(smgModel, player.getHeldItemTransformData(), player.getHeldItemNormalTransformData()); modelRenderer.render(smgModel, player.getHeldItemTransformData(), player.getHeldItemNormalTransformData());
} }
@ -216,6 +220,7 @@ public class GameRenderer {
modelRenderer.render(shotgunModel, myPlayer.getHeldItemTransformData(), myPlayer.getHeldItemNormalTransformData()); modelRenderer.render(shotgunModel, myPlayer.getHeldItemTransformData(), myPlayer.getHeldItemNormalTransformData());
} }
for (var player : client.getPlayers().values()) { for (var player : client.getPlayers().values()) {
if (player.getMode() == PlayerMode.SPECTATOR) continue;
if (player.getHeldItemId() == ItemTypes.WINCHESTER.getId()) { if (player.getHeldItemId() == ItemTypes.WINCHESTER.getId()) {
modelRenderer.render(shotgunModel, player.getHeldItemTransformData(), player.getHeldItemNormalTransformData()); modelRenderer.render(shotgunModel, player.getHeldItemTransformData(), player.getHeldItemNormalTransformData());
} }
@ -230,6 +235,7 @@ public class GameRenderer {
} }
modelRenderer.setAspectColor(new Vector3f(0.5f, 0.5f, 0.5f)); modelRenderer.setAspectColor(new Vector3f(0.5f, 0.5f, 0.5f));
for (var player : client.getPlayers().values()) { for (var player : client.getPlayers().values()) {
if (player.getMode() == PlayerMode.SPECTATOR) continue;
if (player.getHeldItemId() == ItemTypes.BLOCK.getId()) { if (player.getHeldItemId() == ItemTypes.BLOCK.getId()) {
modelRenderer.setAspectColor(client.getWorld().getPalette().getColor(player.getSelectedBlockValue())); modelRenderer.setAspectColor(client.getWorld().getPalette().getColor(player.getSelectedBlockValue()));
modelRenderer.render(blockModel, player.getHeldItemTransformData(), player.getHeldItemNormalTransformData()); modelRenderer.render(blockModel, player.getHeldItemTransformData(), player.getHeldItemNormalTransformData());

View File

@ -74,7 +74,13 @@ public class Player {
*/ */
protected Team team; protected Team team;
public Player(int id, String username, Team team) { /**
* The mode that the player is in, which dictates how they can move and/or
* interact with the world.
*/
protected PlayerMode mode;
public Player(int id, String username, Team team, PlayerMode mode) {
this.position = new Vector3f(); this.position = new Vector3f();
this.velocity = new Vector3f(); this.velocity = new Vector3f();
this.orientation = new Vector2f(); this.orientation = new Vector2f();
@ -82,10 +88,11 @@ public class Player {
this.id = id; this.id = id;
this.username = username; this.username = username;
this.team = team; this.team = team;
this.mode = mode;
} }
public Player(int id, String username) { public Player(int id, String username) {
this(id, username, null); this(id, username, null, PlayerMode.NORMAL);
} }
public Vector3f getPosition() { public Vector3f getPosition() {
@ -138,6 +145,14 @@ public class Player {
this.team = team; this.team = team;
} }
public PlayerMode getMode() {
return mode;
}
public void setMode(PlayerMode mode) {
this.mode = mode;
}
public Vector3f getViewVector() { public Vector3f getViewVector() {
return viewVector; return viewVector;
} }

View File

@ -1,6 +1,7 @@
package nl.andrewl.aos_core.net.client; package nl.andrewl.aos_core.net.client;
import nl.andrewl.aos_core.model.Player; import nl.andrewl.aos_core.model.Player;
import nl.andrewl.aos_core.model.PlayerMode;
import nl.andrewl.aos_core.model.item.ItemTypes; import nl.andrewl.aos_core.model.item.ItemTypes;
import nl.andrewl.record_net.Message; import nl.andrewl.record_net.Message;
@ -15,13 +16,15 @@ public record PlayerJoinMessage(
float ox, float oy, float ox, float oy,
boolean crouching, boolean crouching,
int selectedItemId, int selectedItemId,
byte selectedBlockValue byte selectedBlockValue,
PlayerMode mode
) implements Message { ) implements Message {
public Player toPlayer() { public Player toPlayer() {
Player p = new Player(id, username); Player p = new Player(id, username);
p.getPosition().set(px, py, pz); p.getPosition().set(px, py, pz);
p.getVelocity().set(vx, vy, vz); p.getVelocity().set(vx, vy, vz);
p.getOrientation().set(ox, oy); p.getOrientation().set(ox, oy);
p.setMode(mode);
return p; return p;
} }
} }

View File

@ -1,6 +1,7 @@
package nl.andrewl.aos_core.net.client; package nl.andrewl.aos_core.net.client;
import nl.andrewl.aos_core.model.Player; import nl.andrewl.aos_core.model.Player;
import nl.andrewl.aos_core.model.PlayerMode;
import nl.andrewl.record_net.Message; import nl.andrewl.record_net.Message;
/** /**
@ -16,7 +17,8 @@ public record PlayerUpdateMessage(
float vx, float vy, float vz, float vx, float vy, float vz,
float ox, float oy, float ox, float oy,
boolean crouching, boolean crouching,
int selectedItemId int selectedItemId,
PlayerMode mode
) implements Message { ) implements Message {
public void apply(Player p) { public void apply(Player p) {
@ -24,5 +26,6 @@ public record PlayerUpdateMessage(
p.getVelocity().set(vx, vy, vz); p.getVelocity().set(vx, vy, vz);
p.getOrientation().set(ox, oy); p.getOrientation().set(ox, oy);
p.setCrouching(crouching); p.setCrouching(crouching);
p.setMode(mode);
} }
} }

View File

@ -233,6 +233,7 @@ public class ClientCommunicationHandler {
out.writeBoolean(player.isCrouching()); out.writeBoolean(player.isCrouching());
out.writeInt(player.getInventory().getSelectedItemStack().getType().getId()); out.writeInt(player.getInventory().getSelectedItemStack().getType().getId());
out.writeByte(player.getInventory().getSelectedBlockValue()); out.writeByte(player.getInventory().getSelectedBlockValue());
out.writeInt(player.getMode().ordinal());
} }
// Send the player's own inventory data. // Send the player's own inventory data.
@ -251,5 +252,7 @@ public class ClientCommunicationHandler {
out.writeFloat(player.getPosition().x()); out.writeFloat(player.getPosition().x());
out.writeFloat(player.getPosition().y()); out.writeFloat(player.getPosition().y());
out.writeFloat(player.getPosition().z()); out.writeFloat(player.getPosition().z());
out.writeInt(player.getMode().ordinal());
} }
} }

View File

@ -2,6 +2,7 @@ package nl.andrewl.aos2_server;
import nl.andrewl.aos2_server.model.ServerPlayer; import nl.andrewl.aos2_server.model.ServerPlayer;
import nl.andrewl.aos_core.Net; import nl.andrewl.aos_core.Net;
import nl.andrewl.aos_core.model.PlayerMode;
import nl.andrewl.aos_core.model.Team; import nl.andrewl.aos_core.model.Team;
import nl.andrewl.aos_core.model.item.BlockItemStack; import nl.andrewl.aos_core.model.item.BlockItemStack;
import nl.andrewl.aos_core.model.item.Gun; import nl.andrewl.aos_core.model.item.Gun;
@ -46,6 +47,7 @@ public class PlayerManager {
joinMessage = username + " joined the game."; joinMessage = username + " joined the game.";
} }
player.setPosition(getBestSpawnPoint(player)); player.setPosition(getBestSpawnPoint(player));
player.setMode(PlayerMode.NORMAL);
// Tell all other players that this one has joined. // Tell all other players that this one has joined.
broadcastTcpMessageToAllBut(new PlayerJoinMessage( broadcastTcpMessageToAllBut(new PlayerJoinMessage(
player.getId(), player.getUsername(), player.getTeam() == null ? -1 : player.getTeam().getId(), player.getId(), player.getUsername(), player.getTeam() == null ? -1 : player.getTeam().getId(),
@ -54,7 +56,8 @@ public class PlayerManager {
player.getOrientation().x(), player.getOrientation().y(), player.getOrientation().x(), player.getOrientation().y(),
player.isCrouching(), player.isCrouching(),
player.getInventory().getSelectedItemStack().getType().getId(), player.getInventory().getSelectedItemStack().getType().getId(),
player.getInventory().getSelectedBlockValue() player.getInventory().getSelectedBlockValue(),
player.getMode()
), player); ), player);
broadcastTcpMessageToAllBut(ChatMessage.announce(joinMessage), player); broadcastTcpMessageToAllBut(ChatMessage.announce(joinMessage), player);
return player; return player;

View File

@ -31,9 +31,11 @@ public class WorldUpdater implements Runnable {
final long nsPerTick = (long) Math.floor(secondsPerTick * 1_000_000_000); final long nsPerTick = (long) Math.floor(secondsPerTick * 1_000_000_000);
System.out.printf("Running world updater at %d ms/tick, or %d ns/tick.%n", msPerTick, nsPerTick); System.out.printf("Running world updater at %d ms/tick, or %d ns/tick.%n", msPerTick, nsPerTick);
running = true; running = true;
while (running) { while (running) {
long start = System.nanoTime(); long start = System.nanoTime();
tick(System.currentTimeMillis()); long now = System.currentTimeMillis();
tick(now);
long elapsedNs = System.nanoTime() - start; long elapsedNs = System.nanoTime() - start;
if (elapsedNs > nsPerTick) { if (elapsedNs > nsPerTick) {
System.err.printf("Took %d ns to do one tick, which is more than the desired %d ns per tick.%n", elapsedNs, nsPerTick); System.err.printf("Took %d ns to do one tick, which is more than the desired %d ns per tick.%n", elapsedNs, nsPerTick);

View File

@ -2,6 +2,7 @@ package nl.andrewl.aos2_server.model;
import nl.andrewl.aos2_server.logic.PlayerActionManager; import nl.andrewl.aos2_server.logic.PlayerActionManager;
import nl.andrewl.aos_core.model.Player; import nl.andrewl.aos_core.model.Player;
import nl.andrewl.aos_core.model.PlayerMode;
import nl.andrewl.aos_core.model.item.BlockItemStack; import nl.andrewl.aos_core.model.item.BlockItemStack;
import nl.andrewl.aos_core.model.item.GunItemStack; import nl.andrewl.aos_core.model.item.GunItemStack;
import nl.andrewl.aos_core.model.item.Inventory; import nl.andrewl.aos_core.model.item.Inventory;
@ -84,7 +85,8 @@ public class ServerPlayer extends Player {
velocity.x, velocity.y, velocity.z, velocity.x, velocity.y, velocity.z,
orientation.x, orientation.y, orientation.x, orientation.y,
actionManager.getInput().crouching(), actionManager.getInput().crouching(),
inventory.getSelectedItemStack().getType().getId() inventory.getSelectedItemStack().getType().getId(),
mode
); );
} }
} }