84 lines
2.2 KiB
TypeScript
84 lines
2.2 KiB
TypeScript
|
|
import { initMessagePairs } from './MessagePairInit';
|
||
|
|
import { MsgManager } from './MsgManager';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 消息系统使用示例
|
||
|
|
*/
|
||
|
|
export class MsgExample {
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 初始化消息系统
|
||
|
|
* 应在游戏启动时调用
|
||
|
|
*/
|
||
|
|
static init(): void {
|
||
|
|
// 初始化消息对注册表
|
||
|
|
initMessagePairs();
|
||
|
|
|
||
|
|
console.log('[MsgExample] Message system initialized');
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 登录示例
|
||
|
|
*/
|
||
|
|
static async loginExample(): Promise<void> {
|
||
|
|
const msgMgr = MsgManager.getInstance();
|
||
|
|
|
||
|
|
try {
|
||
|
|
const response = await msgMgr.login({
|
||
|
|
playerId: 'player123',
|
||
|
|
playerName: 'TestPlayer'
|
||
|
|
});
|
||
|
|
|
||
|
|
if (response && response.success) {
|
||
|
|
console.log('登录成功:', response.player);
|
||
|
|
} else {
|
||
|
|
console.log('登录失败:', response?.message);
|
||
|
|
}
|
||
|
|
} catch (error) {
|
||
|
|
console.error('登录请求异常:', error);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 移动示例
|
||
|
|
*/
|
||
|
|
static async moveExample(): Promise<void> {
|
||
|
|
const msgMgr = MsgManager.getInstance();
|
||
|
|
|
||
|
|
try {
|
||
|
|
const response = await msgMgr.move({
|
||
|
|
x: 100,
|
||
|
|
y: 200
|
||
|
|
});
|
||
|
|
|
||
|
|
if (response && response.success) {
|
||
|
|
console.log('移动成功:', response.position);
|
||
|
|
} else {
|
||
|
|
console.log('移动失败:', response?.message);
|
||
|
|
}
|
||
|
|
} catch (error) {
|
||
|
|
console.error('移动请求异常:', error);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 发送消息示例
|
||
|
|
*/
|
||
|
|
static async sendMessageExample(): Promise<void> {
|
||
|
|
const msgMgr = MsgManager.getInstance();
|
||
|
|
|
||
|
|
try {
|
||
|
|
const response = await msgMgr.send({
|
||
|
|
content: 'Hello, World!'
|
||
|
|
});
|
||
|
|
|
||
|
|
if (response) {
|
||
|
|
console.log('消息发送成功:', response.time);
|
||
|
|
} else {
|
||
|
|
console.log('消息发送失败');
|
||
|
|
}
|
||
|
|
} catch (error) {
|
||
|
|
console.error('发送消息异常:', error);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|