地图尺寸改小,同步位置放大1000倍取整
This commit is contained in:
@@ -2,6 +2,7 @@ import { MsgCall } from "tsrpc";
|
||||
import { playerManager } from "../managers/PlayerManager";
|
||||
import { MsgReqLogin } from "../shared/protocols/MsgReqLogin";
|
||||
import { MsgResLogin, PlayerInfo } from "../shared/protocols/MsgResLogin";
|
||||
import { gameToClientPosition } from "../utils/coordinate";
|
||||
|
||||
/**
|
||||
* 登录消息处理器
|
||||
@@ -36,12 +37,12 @@ export default async function (call: MsgCall<MsgReqLogin>) {
|
||||
// 保存玩家ID到连接对象,供其他消息处理器使用
|
||||
(call.conn as any).playerId = playerId;
|
||||
|
||||
// 转换为协议格式
|
||||
// 转换为协议格式(将服务器内部坐标转换为客户端坐标)
|
||||
const playerInfo: PlayerInfo = {
|
||||
id: player.id,
|
||||
name: player.name,
|
||||
position: { ...player.position },
|
||||
spawnPoint: { ...player.spawnPoint },
|
||||
position: gameToClientPosition(player.position),
|
||||
spawnPoint: gameToClientPosition(player.spawnPoint),
|
||||
hp: player.hp,
|
||||
maxHp: player.maxHp,
|
||||
isAlive: player.isAlive,
|
||||
@@ -55,7 +56,7 @@ export default async function (call: MsgCall<MsgReqLogin>) {
|
||||
broadcastToAll('PlayerJoin', {
|
||||
playerId: player.id,
|
||||
playerName: player.name,
|
||||
position: { ...player.position },
|
||||
position: gameToClientPosition(player.position),
|
||||
isNewPlayer,
|
||||
timestamp: Date.now()
|
||||
}, call.conn.id);
|
||||
|
||||
@@ -3,6 +3,7 @@ import { playerManager } from "../managers/PlayerManager";
|
||||
import { MsgPlayerMove } from "../shared/protocols/MsgPlayerMove";
|
||||
import { MsgReqMove } from "../shared/protocols/MsgReqMove";
|
||||
import { broadcastToAll } from "../utils/broadcast";
|
||||
import { clientToGamePosition, gameToClientPosition } from "../utils/coordinate";
|
||||
|
||||
/**
|
||||
* 移动消息处理器
|
||||
@@ -60,9 +61,12 @@ export default async function (call: MsgCall<MsgReqMove>) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 将客户端坐标(放大1000倍)转换为游戏内坐标
|
||||
const gamePosition = clientToGamePosition({ x, y });
|
||||
|
||||
try {
|
||||
// 尝试更新玩家位置
|
||||
const success = playerManager.updatePlayerPosition(playerId, x, y);
|
||||
// 尝试更新玩家位置(使用游戏内坐标)
|
||||
const success = playerManager.updatePlayerPosition(playerId, gamePosition.x, gamePosition.y);
|
||||
|
||||
if (!success) {
|
||||
call.conn.sendMsg('ResMove', {
|
||||
@@ -72,10 +76,9 @@ export default async function (call: MsgCall<MsgReqMove>) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 获取更新后的位置
|
||||
const newPosition = { x, y };
|
||||
|
||||
// 广播移动消息给所有其他玩家
|
||||
// 获取更新后的位置(转换为客户端坐标)
|
||||
const newPosition = gameToClientPosition(gamePosition);
|
||||
// 广播移动消息给所有其他玩家(使用客户端坐标)
|
||||
const moveMsg: MsgPlayerMove = {
|
||||
playerId: player.id,
|
||||
playerName: player.name,
|
||||
@@ -92,7 +95,7 @@ export default async function (call: MsgCall<MsgReqMove>) {
|
||||
position: newPosition
|
||||
});
|
||||
|
||||
console.log(`玩家 ${player.name} 移动到 (${x}, ${y})`);
|
||||
console.log(`玩家 ${player.name} 移动到游戏内坐标 (${gamePosition.x}, ${gamePosition.y}),客户端坐标 (${newPosition.x}, ${newPosition.y})`);
|
||||
|
||||
} catch (error) {
|
||||
console.error('移动失败:', error);
|
||||
|
||||
Reference in New Issue
Block a user