添加一个查找对面玩家的方法

This commit is contained in:
zhl 2020-12-18 19:36:24 +08:00
parent 8257a9b71f
commit 0078507ea4
2 changed files with 21 additions and 0 deletions

5
src/global.d.ts vendored
View File

@ -260,6 +260,11 @@ declare module "colyseus" {
*/
getAssistClient(sessionId: string): RobotClient;
/**
*
* @param srcPlayer
*/
getOppositePlayer(srcPlayer: string|Player): Player;
}
}

View File

@ -282,4 +282,20 @@ export class GeneralRoom extends Room {
getAssistClient(sessionId: string): RobotClient {
return this.assistMap.get(sessionId);
}
getOppositePlayer(srcPlayer: string | Player): Player {
let playerId;
if (typeof srcPlayer === 'string') {
playerId = srcPlayer as string;
} else {
playerId = srcPlayer.id;
}
if (!this.state.players.has(playerId)) {
return null;
}
const sessionIds = [...this.state.players.keys()];
let idx = sessionIds.indexOf(playerId);
let playerId2 = sessionIds[(2 + idx) % 4];
return this.state.players.get(playerId2);
}
}