修复一部分bug
This commit is contained in:
@@ -16,8 +16,12 @@ public class NetworkManager {
|
||||
private final Map<String, String> playerCharacters = new HashMap<>();
|
||||
// 存储远程玩家的攻击类型(attackType)
|
||||
private final Map<String, String> playerAttacks = new HashMap<>();
|
||||
// 攻击方向:playerId -> "R" 或 "L"
|
||||
private final Map<String, String> playerAttackDirs = new HashMap<>();
|
||||
// 伤害事件:targetId -> 累积伤害(本帧内可能多次)
|
||||
private final Map<String, Integer> damageEvents = new HashMap<>();
|
||||
// 伤害方向:targetId -> dirSign (-1 / 0 / 1)
|
||||
private final Map<String, Integer> damageDirs = new HashMap<>();
|
||||
private final Map<String, float[]> respawnEvents = new HashMap<>();
|
||||
|
||||
public static NetworkManager getInstance() {
|
||||
@@ -99,11 +103,14 @@ public class NetworkManager {
|
||||
Gdx.app.log("Network", "收到准备信号");
|
||||
} else if (message.startsWith("ATTACK:")) {
|
||||
String[] parts = message.substring(7).split(",");
|
||||
if (parts.length == 2) {
|
||||
// ATTACK:playerId,attackType,dir
|
||||
if (parts.length >= 2) {
|
||||
String playerId = parts[0];
|
||||
String attackType = parts[1];
|
||||
String dir = parts.length >= 3 ? parts[2] : "R"; // 兼容旧版本无方向
|
||||
playerAttacks.put(playerId, attackType);
|
||||
Gdx.app.log("Network", "攻击同步: " + playerId + " -> " + attackType);
|
||||
playerAttackDirs.put(playerId, dir);
|
||||
Gdx.app.log("Network", "攻击同步: " + playerId + " -> " + attackType + " dir=" + dir);
|
||||
} else {
|
||||
Gdx.app.error("Network", "攻击消息格式错误: " + message);
|
||||
}
|
||||
@@ -115,9 +122,15 @@ public class NetworkManager {
|
||||
try {
|
||||
int amount = Integer.parseInt(parts[1]);
|
||||
damageEvents.merge(targetId, amount, Integer::sum);
|
||||
// 第三个参数方向目前不存储,只为击退逻辑可扩展
|
||||
Gdx.app.log("Network",
|
||||
"收到伤害: " + targetId + " -" + amount + (parts.length >= 3 ? (" dir=" + parts[2]) : ""));
|
||||
int dir = 0;
|
||||
if (parts.length >= 3) {
|
||||
try {
|
||||
dir = Integer.parseInt(parts[2]);
|
||||
} catch (NumberFormatException ignore) {
|
||||
}
|
||||
damageDirs.put(targetId, dir);
|
||||
}
|
||||
Gdx.app.log("Network", "收到伤害: " + targetId + " -" + amount + (dir != 0 ? (" dir=" + dir) : ""));
|
||||
} catch (NumberFormatException e) {
|
||||
Gdx.app.error("Network", "伤害数值解析失败: " + message);
|
||||
}
|
||||
@@ -156,13 +169,17 @@ public class NetworkManager {
|
||||
return playerAttacks;
|
||||
}
|
||||
|
||||
public Map<String, String> getPlayerAttackDirs() {
|
||||
return playerAttackDirs;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送攻击消息给其他玩家
|
||||
*/
|
||||
public void sendAttack(String attackType) {
|
||||
public void sendAttack(String attackType, String dir) {
|
||||
if (localPlayerId == null)
|
||||
return;
|
||||
String msg = "ATTACK:" + localPlayerId + "," + attackType;
|
||||
String msg = "ATTACK:" + localPlayerId + "," + attackType + "," + dir; // 新格式包含方向
|
||||
Gdx.app.log("Network", "发送攻击消息: " + msg);
|
||||
if (isHost && server != null) {
|
||||
server.broadcastToOthers(null, msg);
|
||||
@@ -172,6 +189,11 @@ public class NetworkManager {
|
||||
}
|
||||
}
|
||||
|
||||
// 兼容旧代码(若仍有地方调用)默认使用 R
|
||||
public void sendAttack(String attackType) {
|
||||
sendAttack(attackType, "R");
|
||||
}
|
||||
|
||||
public Map<String, String> getPlayerCharacters() {
|
||||
return playerCharacters;
|
||||
}
|
||||
@@ -180,8 +202,12 @@ public class NetworkManager {
|
||||
return damageEvents;
|
||||
}
|
||||
|
||||
public void sendDamage(String targetId, int amount) {
|
||||
String msg = "DAMAGE:" + targetId + "," + amount; // 不含方向
|
||||
public Map<String, Integer> getDamageDirs() {
|
||||
return damageDirs;
|
||||
}
|
||||
|
||||
public void sendDamage(String targetId, int amount, int dirSign) {
|
||||
String msg = "DAMAGE:" + targetId + "," + amount + "," + dirSign; // 含方向
|
||||
if (isHost && server != null) {
|
||||
server.broadcastToOthers(null, msg);
|
||||
receiveMessage(msg); // 本地也应用
|
||||
@@ -190,6 +216,11 @@ public class NetworkManager {
|
||||
}
|
||||
}
|
||||
|
||||
// 兼容旧调用,不带方向(使用0表示沿用被击者朝向逻辑)
|
||||
public void sendDamage(String targetId, int amount) {
|
||||
sendDamage(targetId, amount, 0);
|
||||
}
|
||||
|
||||
public void sendRespawn(String playerId, float x, float y) {
|
||||
String msg = "RESPAWN:" + playerId + "," + x + "," + y;
|
||||
if (isHost && server != null) {
|
||||
@@ -221,6 +252,10 @@ public class NetworkManager {
|
||||
}
|
||||
playerPositions.clear();
|
||||
playerCharacters.clear();
|
||||
playerAttacks.clear();
|
||||
playerAttackDirs.clear();
|
||||
damageEvents.clear();
|
||||
damageDirs.clear();
|
||||
Gdx.app.log("Network", "已断开连接");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user