移除未使用的字符类及相关资源;更新二进制文件和项目结构。(完成联机和攻击框)
This commit is contained in:
@@ -14,6 +14,8 @@ public class NetworkManager {
|
||||
private String localCharacter;
|
||||
private final Map<String, float[]> playerPositions = new HashMap<>();
|
||||
private final Map<String, String> playerCharacters = new HashMap<>();
|
||||
// 存储远程玩家的攻击类型(attackType)
|
||||
private final Map<String, String> playerAttacks = new HashMap<>();
|
||||
|
||||
public static NetworkManager getInstance() {
|
||||
if (instance == null) {
|
||||
@@ -30,34 +32,21 @@ 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);
|
||||
} else if (client != null) {
|
||||
client.sendMessage(msg);
|
||||
}
|
||||
}
|
||||
|
||||
public void sendCharacterSelection(String character) {//发送角色选择消息
|
||||
this.localCharacter = character;
|
||||
String msg = "SELECT:" + localPlayerId + "," + character;
|
||||
Gdx.app.log("Network", "发送角色选择消息: " + msg);
|
||||
if (isHost && server != null) {
|
||||
server.broadcastToOthers(null, msg);
|
||||
receiveMessage(msg);
|
||||
@@ -66,8 +55,18 @@ public class NetworkManager {
|
||||
}
|
||||
}
|
||||
|
||||
public void receiveMessage(String message) {//解析消息
|
||||
Gdx.app.log("Network", "收到消息: " + message);
|
||||
public void sendCharacterSelection(String character) {// 发送角色选择消息
|
||||
this.localCharacter = character;
|
||||
String msg = "SELECT:" + localPlayerId + "," + character;
|
||||
if (isHost && server != null) {
|
||||
server.broadcastToOthers(null, msg);
|
||||
receiveMessage(msg);
|
||||
} else if (client != null) {
|
||||
client.sendMessage(msg);
|
||||
}
|
||||
}
|
||||
|
||||
public void receiveMessage(String message) {// 解析消息
|
||||
|
||||
if (message.startsWith("POS:")) {
|
||||
String[] parts = message.substring(4).split(",");
|
||||
@@ -76,8 +75,7 @@ public class NetworkManager {
|
||||
try {
|
||||
float x = Float.parseFloat(parts[1]);
|
||||
float y = Float.parseFloat(parts[2]);
|
||||
playerPositions.put(playerId, new float[]{x, y});
|
||||
Gdx.app.log("Network", "位置更新: " + playerId + " -> " + x + "," + y);
|
||||
playerPositions.put(playerId, new float[] { x, y });
|
||||
} catch (NumberFormatException e) {
|
||||
Gdx.app.error("Network", "位置解析失败: " + message);
|
||||
}
|
||||
@@ -96,6 +94,16 @@ public class NetworkManager {
|
||||
}
|
||||
} else if (message.equals("READY")) {
|
||||
Gdx.app.log("Network", "收到准备信号");
|
||||
} else if (message.startsWith("ATTACK:")) {
|
||||
String[] parts = message.substring(7).split(",");
|
||||
if (parts.length == 2) {
|
||||
String playerId = parts[0];
|
||||
String attackType = parts[1];
|
||||
playerAttacks.put(playerId, attackType);
|
||||
Gdx.app.log("Network", "攻击同步: " + playerId + " -> " + attackType);
|
||||
} else {
|
||||
Gdx.app.error("Network", "攻击消息格式错误: " + message);
|
||||
}
|
||||
} else {
|
||||
Gdx.app.log("Network", "未知消息类型: " + message);
|
||||
}
|
||||
@@ -105,6 +113,29 @@ public class NetworkManager {
|
||||
return playerPositions;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取远程玩家的未处理攻击类型映射
|
||||
*/
|
||||
public Map<String, String> getPlayerAttacks() {
|
||||
return playerAttacks;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送攻击消息给其他玩家
|
||||
*/
|
||||
public void sendAttack(String attackType) {
|
||||
if (localPlayerId == null)
|
||||
return;
|
||||
String msg = "ATTACK:" + localPlayerId + "," + attackType;
|
||||
Gdx.app.log("Network", "发送攻击消息: " + msg);
|
||||
if (isHost && server != null) {
|
||||
server.broadcastToOthers(null, msg);
|
||||
receiveMessage(msg);
|
||||
} else if (client != null) {
|
||||
client.sendMessage(msg);
|
||||
}
|
||||
}
|
||||
|
||||
public Map<String, String> getPlayerCharacters() {
|
||||
return playerCharacters;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user