This commit is contained in:
2025-09-25 14:57:01 +08:00
parent 659827bcc3
commit 4947ebb29d
43 changed files with 781 additions and 496 deletions

View File

@@ -10,7 +10,6 @@ public class NetworkManager {
private ConnectServer server;
private ConnectClient client;
private boolean isHost = false;
private String localPlayerId;
private String localCharacter;
private final Map<String, float[]> playerPositions = new HashMap<>();
@@ -31,31 +30,31 @@ public class NetworkManager {
return localPlayerId;
}
public void createRoom() {
public void createRoom() {//创建房间
isHost = true;
server = new ConnectServer(11455);
new Thread(server).start();
Gdx.app.log("Network", "房主模式:服务器已启动");
}
public void joinRoom(String ip) {
public void joinRoom(String ip) {//加入房间
isHost = false;
client = new ConnectClient(ip, 11455);
Gdx.app.log("Network", "客户端模式:连接到房主 " + ip);
}
public void sendPosition(float x, float y) {
public void sendPosition(float x, float y) {//发送位置消息
String msg = "POS:" + localPlayerId + "," + x + "," + y;
Gdx.app.log("Network", "发送位置消息: " + msg);
if (isHost && server != null) {
server.broadcastToOthers(null, msg);
receiveMessage(msg); // 房主自己也处理
receiveMessage(msg);
} else if (client != null) {
client.sendMessage(msg);
}
}
public void sendCharacterSelection(String character) {
public void sendCharacterSelection(String character) {//发送角色选择消息
this.localCharacter = character;
String msg = "SELECT:" + localPlayerId + "," + character;
Gdx.app.log("Network", "发送角色选择消息: " + msg);
@@ -67,7 +66,7 @@ public class NetworkManager {
}
}
public void receiveMessage(String message) {
public void receiveMessage(String message) {//解析消息
Gdx.app.log("Network", "收到消息: " + message);
if (message.startsWith("POS:")) {