ResLogin增加房间内其他玩家数据用于初始化

This commit is contained in:
janing
2025-12-18 16:04:11 +08:00
parent 4b3b94a450
commit 03276fe1f6
5 changed files with 36 additions and 6 deletions

View File

@@ -3,13 +3,13 @@
*/
export const WorldConfig = {
/** 世界宽度 */
WORLD_WIDTH: 100,
WORLD_WIDTH: 1440,
/** 世界高度 */
WORLD_HEIGHT: 100,
WORLD_HEIGHT: 1440,
/** 出生区域半径(距离中心点) */
SPAWN_RADIUS: 20,
SPAWN_RADIUS: 10,
/** 世界中心点 X 坐标 */
get CENTER_X() {

View File

@@ -50,6 +50,20 @@ export default async function (call: MsgCall<MsgReqLogin>) {
lastLoginAt: player.lastLoginAt
};
// 获取房间内其他在线玩家信息
const otherOnlinePlayers = playerManager.getOnlinePlayers().filter(p => p.id !== playerId);
const otherPlayersInfo: PlayerInfo[] = otherOnlinePlayers.map(p => ({
id: p.id,
name: p.name,
position: gameToClientPosition(p.position),
spawnPoint: gameToClientPosition(p.spawnPoint),
hp: p.hp,
maxHp: p.maxHp,
isAlive: p.isAlive,
createdAt: p.createdAt,
lastLoginAt: p.lastLoginAt
}));
// 广播玩家加入消息给其他在线玩家
const { broadcastToAll } = await import('../utils/broadcast');
@@ -66,7 +80,8 @@ export default async function (call: MsgCall<MsgReqLogin>) {
success: true,
message: isNewPlayer ? '创建角色成功,欢迎来到游戏世界!' : '登录成功,欢迎回来!',
player: playerInfo,
isNewPlayer
isNewPlayer,
otherPlayers: otherPlayersInfo
};
call.conn.sendMsg('ResLogin', response);

View File

@@ -86,7 +86,7 @@ export default async function (call: MsgCall<MsgReqMove>) {
timestamp: Date.now()
};
broadcastToAll('MsgPlayerMove', moveMsg, call.conn.id);
broadcastToAll('PlayerMove', moveMsg, call.conn.id);
// 返回成功结果
call.conn.sendMsg('ResMove', {

View File

@@ -47,4 +47,7 @@ export interface MsgResLogin {
/** 是否新玩家 */
isNewPlayer?: boolean;
/** 房间内其他在线玩家信息 */
otherPlayers?: PlayerInfo[];
}

View File

@@ -27,7 +27,7 @@ export interface ServiceType {
}
export const serviceProto: ServiceProto<ServiceType> = {
"version": 5,
"version": 6,
"services": [
{
"id": 0,
@@ -273,6 +273,18 @@ export const serviceProto: ServiceProto<ServiceType> = {
"type": "Boolean"
},
"optional": true
},
{
"id": 4,
"name": "otherPlayers",
"type": {
"type": "Array",
"elementType": {
"type": "Reference",
"target": "MsgResLogin/PlayerInfo"
}
},
"optional": true
}
]
},