同步位置放大1000倍取整

This commit is contained in:
janing
2025-12-18 13:43:28 +08:00
parent 637978357f
commit 4b3b94a450
3 changed files with 18 additions and 9 deletions

View File

@@ -41,7 +41,9 @@ export class PlayerController extends Component {
*/
public init(playerInfo: PlayerInfo): void {
this.playerInfo = playerInfo;
this.lastSentPosition.set(playerInfo.position.x, 0, playerInfo.position.y);
const x = playerInfo.position.x / 1000
const y = playerInfo.position.y / 1000
this.lastSentPosition.set(x, 0, y);
// 获取 RoleController 组件
this.roleController = this.node.getComponentInChildren(RoleController);
@@ -190,8 +192,10 @@ export class PlayerController extends Component {
// 服务器可能会修正位置,使用服务器返回的位置
if (result.position) {
const serverPos = result.position;
this.node.setPosition(serverPos.x, 0, serverPos.y);
this.lastSentPosition.set(serverPos.x, 0, serverPos.y);
const x = serverPos.x / 1000
const y = serverPos.y / 1000
this.node.setPosition(x, 0, y);
this.lastSentPosition.set(x, 0, y);
}
} catch (error) {
console.error('[PlayerController] 发送移动请求异常:', error);

View File

@@ -1,6 +1,6 @@
import { Node, Vec3, Tween, tween } from 'cc';
import { Position } from '../../Shared/protocols/base';
import { Node, Tween, tween, Vec3 } from 'cc';
import { RoleController } from '../../CC/RoleController';
import { Position } from '../../Shared/protocols/base';
/**
* RemotePlayer 远程玩家
@@ -38,8 +38,10 @@ export class RemotePlayer {
this.playerNode = playerNode;
this.playerId = playerId;
this.playerName = playerName;
this.currentPosition.set(position.x, 0, position.y);
this.targetPosition.set(position.x, 0, position.y);
const x = position.x / 1000
const y = position.y / 1000
this.currentPosition.set(x, 0, y);
this.targetPosition.set(x, 0, y);
// 获取 RoleController 组件
this.roleController = this.playerNode.getComponentInChildren(RoleController);
@@ -58,7 +60,7 @@ export class RemotePlayer {
* 收到服务器广播的移动消息时调用
*/
public updatePosition(position: Position): void {
this.targetPosition.set(position.x, 0, position.y);
this.targetPosition.set(position.x / 1000, 0, position.y / 1000);
// 计算移动方向和距离
const direction = this.targetPosition.clone().subtract(this.currentPosition);

View File

@@ -107,10 +107,13 @@ export class World {
return;
}
const x = this.localPlayer.position.x / 1000
const y = this.localPlayer.position.y / 1000
// 实例化玩家节点
this.localPlayerNode = instantiate(this.playerPrefab);
this.localPlayerNode.name = `Player_${this.localPlayer.id}_Local`;
this.localPlayerNode.setPosition(this.localPlayer.position.x, 0, this.localPlayer.position.y);
this.localPlayerNode.setPosition(x, 0, y);
this.worldRoot.addChild(this.localPlayerNode);
// 创建本地玩家控制器