Added broadcast of player join/leave messages.

This commit is contained in:
Andrew Lalis 2022-07-09 16:23:33 +02:00
parent 4ef8e88e81
commit dd97f43c9a
2 changed files with 21 additions and 1 deletions

View File

@ -1,5 +1,6 @@
package nl.andrewl.aos_core.net; package nl.andrewl.aos_core.net;
import nl.andrewl.aos_core.model.Player;
import nl.andrewl.record_net.Message; import nl.andrewl.record_net.Message;
/** /**
@ -11,4 +12,13 @@ public record PlayerJoinMessage(
float px, float py, float pz, float px, float py, float pz,
float vx, float vy, float vz, float vx, float vy, float vz,
float ox, float oy float ox, float oy
) implements Message {} ) implements Message {
public PlayerJoinMessage(Player player) {
this(
player.getId(), player.getUsername(),
player.getPosition().x, player.getPosition().y, player.getPosition().z,
player.getVelocity().x, player.getVelocity().y, player.getVelocity().z,
player.getOrientation().x, player.getOrientation().y
);
}
}

View File

@ -1,6 +1,8 @@
package nl.andrewl.aos2_server; package nl.andrewl.aos2_server;
import nl.andrewl.aos_core.Net; import nl.andrewl.aos_core.Net;
import nl.andrewl.aos_core.net.PlayerJoinMessage;
import nl.andrewl.aos_core.net.PlayerLeaveMessage;
import nl.andrewl.aos_core.net.udp.DatagramInit; import nl.andrewl.aos_core.net.udp.DatagramInit;
import nl.andrewl.aos_core.net.udp.PlayerUpdateMessage; import nl.andrewl.aos_core.net.udp.PlayerUpdateMessage;
import nl.andrewl.record_net.Message; import nl.andrewl.record_net.Message;
@ -29,6 +31,7 @@ public class PlayerManager {
clientHandlers.put(player.getId(), handler); clientHandlers.put(player.getId(), handler);
log.info("Registered player \"{}\" with id {}", player.getUsername(), player.getId()); log.info("Registered player \"{}\" with id {}", player.getUsername(), player.getId());
player.setPosition(new Vector3f(0, 64, 0)); player.setPosition(new Vector3f(0, 64, 0));
broadcastTcpMessage(new PlayerJoinMessage(player));
broadcastUdpMessage(new PlayerUpdateMessage(player)); broadcastUdpMessage(new PlayerUpdateMessage(player));
return player; return player;
} }
@ -38,6 +41,7 @@ public class PlayerManager {
if (handler != null) handler.shutdown(); if (handler != null) handler.shutdown();
players.remove(player.getId()); players.remove(player.getId());
clientHandlers.remove(player.getId()); clientHandlers.remove(player.getId());
broadcastTcpMessage(new PlayerLeaveMessage(player.getId()));
log.info("Deregistered player \"{}\" with id {}", player.getUsername(), player.getId()); log.info("Deregistered player \"{}\" with id {}", player.getUsername(), player.getId());
} }
@ -73,6 +77,12 @@ public class PlayerManager {
} }
} }
public void broadcastTcpMessage(Message msg) {
for (var handler : getHandlers()) {
handler.sendTcpMessage(msg);
}
}
public void broadcastUdpMessage(Message msg) { public void broadcastUdpMessage(Message msg) {
try { try {
byte[] data = Net.write(msg); byte[] data = Net.write(msg);