地图尺寸改小,同步位置放大1000倍取整

This commit is contained in:
janing
2025-12-18 13:43:16 +08:00
parent e677b1e11b
commit 637978357f
11 changed files with 77 additions and 23 deletions

View File

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

View File

@@ -1,5 +1,5 @@
/**
* 位置坐标接口
* 位置坐标接口服务器内部使用实际坐标与客户端通信时需要乘以1000
*/
export interface Position {
x: number;

View File

@@ -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);

View File

@@ -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);

View File

@@ -11,7 +11,7 @@ export interface MsgPlayerJoin {
/** 玩家昵称 */
playerName: string;
/** 玩家位置 */
/** 玩家位置客户端坐标放大1000倍后的整数 */
position: Position;
/** 是否新玩家 */

View File

@@ -11,7 +11,7 @@ export interface MsgPlayerMove {
/** 玩家昵称 */
playerName: string;
/** 移动后的位置 */
/** 移动后的位置客户端坐标放大1000倍后的整数 */
position: Position;
/** 移动时间戳 */

View File

@@ -2,9 +2,9 @@
* 移动请求消息
*/
export interface MsgReqMove {
/** X坐标 */
/** X坐标放大1000倍后取整服务器内部除以1000作为实际坐标 */
x: number;
/** Y坐标 */
/** Y坐标放大1000倍后取整服务器内部除以1000作为实际坐标 */
y: number;
}

View File

@@ -10,10 +10,10 @@ export interface PlayerInfo {
/** 玩家昵称 */
name: string;
/** 当前位置 */
/** 当前位置客户端坐标放大1000倍后的整数 */
position: Position;
/** 出生点 */
/** 出生点客户端坐标放大1000倍后的整数 */
spawnPoint: Position;
/** 当前生命值 */

View File

@@ -10,6 +10,6 @@ export interface MsgResMove {
/** 消息 */
message: string;
/** 新位置(成功时返回) */
/** 新位置(成功时返回客户端坐标放大1000倍后的整数 */
position?: Position;
}

View File

@@ -15,7 +15,7 @@ export interface BaseMessage {
}
/**
* 位置坐标
* 位置坐标客户端使用放大1000倍后的整数坐标
*/
export interface Position {
x: number;

View File

@@ -0,0 +1,50 @@
import { Position } from '../models/Position';
/**
* 坐标转换工具
* 处理服务器内部坐标和客户端坐标之间的转换
* 客户端坐标 = 服务器坐标 * 1000取整
* 服务器坐标 = 客户端坐标 / 1000
*/
/**
* 将服务器内部坐标转换为客户端坐标
* @param gamePos 服务器内部坐标(实际坐标)
* @returns 客户端坐标放大1000倍后取整
*/
export function gameToClientPosition(gamePos: Position): Position {
return {
x: Math.round(gamePos.x * 1000),
y: Math.round(gamePos.y * 1000)
};
}
/**
* 将客户端坐标转换为服务器内部坐标
* @param clientPos 客户端坐标放大1000倍后的整数
* @returns 服务器内部坐标(实际坐标)
*/
export function clientToGamePosition(clientPos: Position): Position {
return {
x: clientPos.x / 1000,
y: clientPos.y / 1000
};
}
/**
* 批量转换服务器坐标为客户端坐标
* @param gamePositions 服务器内部坐标数组
* @returns 客户端坐标数组
*/
export function gameToClientPositions(gamePositions: Position[]): Position[] {
return gamePositions.map(gameToClientPosition);
}
/**
* 批量转换客户端坐标为服务器坐标
* @param clientPositions 客户端坐标数组
* @returns 服务器内部坐标数组
*/
export function clientToGamePositions(clientPositions: Position[]): Position[] {
return clientPositions.map(clientToGamePosition);
}