Commit 0d49d541 authored by 范雪寒's avatar 范雪寒

feat:

parent fe3c99e7
...@@ -77,27 +77,28 @@ export class NetworkHelper { ...@@ -77,27 +77,28 @@ export class NetworkHelper {
console.log('加入房间成功 room = ', room); console.log('加入房间成功 room = ', room);
const playerMe = room.playerList.find(player => player.isLocal); const playerMe = room.playerList.find(player => player.isLocal);
const masterPlayer = room.playerList.find(player => player.isMaster); const masterPlayer = room.playerList.find(player => player.isMaster);
const masterId = masterPlayer.userId const masterId = masterPlayer.userId;
const playerId = playerMe.userId; const playerId = playerMe.userId;
this.room = new FakeRoom(masterId); this.room = new FakeRoom(masterId);
this.room.players.push(this.room.player); this.room.players.push(this.room.player);
this.player = this.room.player; this.player = this.room.player;
this.client.on(globalThis.Play.Event.PLAYER_ROOM_LEFT, (event) => { this.client.on(globalThis.Play.Event.PLAYER_ROOM_LEFT, (event) => {
this.room.on('Leave', event); this.onLeaveRoom({ data: { leavePlayerId: event.leftPlayer.userId } });
}); });
this.client.on(globalThis.Play.Event.PLAYER_ROOM_JOINED, (event) => {
this.room.onJoin(this.onJoinRoom.bind(this)); this.onJoinRoom({ playerId: event.newPlayer.userId });
this.room.onLeave(this.onLeaveRoom.bind(this)); });
this.room.onStartFrameSync(this.onStartFrameSync.bind(this)); this.client.on(globalThis.Play.Event.CUSTOM_EVENT, (event) => {
this.room.onStopFrameSync(this.onStopFrameSync.bind(this)); this.onRecvFrame({ data: event.eventData });
this.room.onRecvFrame(this.onRecvFrame.bind(this)); });
this.room.onDisconnect(this.onDisconnect.bind(this)); // this.room.onDisconnect(this.onDisconnect.bind(this));
this.room.onDismiss(this.onDisconnect.bind(this)); // this.room.onDismiss(this.onDisconnect.bind(this));
return playerId; return playerId;
} }
async leaveRoom() { async leaveRoom() {
await this.client.close();
console.log("退房成功"); console.log("退房成功");
} }
...@@ -108,7 +109,11 @@ export class NetworkHelper { ...@@ -108,7 +109,11 @@ export class NetworkHelper {
} }
async closeRoom() { async closeRoom() {
// 设置房间不可见
await this.client.setRoomVisible(false);
console.log(this.client.room.visible);
} }
async startFrameSync() { async startFrameSync() {
console.log("开始帧同步成功"); console.log("开始帧同步成功");
} }
...@@ -118,14 +123,16 @@ export class NetworkHelper { ...@@ -118,14 +123,16 @@ export class NetworkHelper {
} }
sendFrame(data: any) { sendFrame(data: any) {
this.onRecvFrame({ data }); this.client.sendEvent(0, data, {
receiverGroup: globalThis.Play.ReceiverGroup.All,
});
} }
onJoinRoom(event) { onJoinRoom(event) {
console.log("新玩家加入", event);
if (this._eventListeners["playerJoin"]) { if (this._eventListeners["playerJoin"]) {
this._eventListeners["playerJoin"](event); this._eventListeners["playerJoin"](event);
} }
console.log("新玩家加入", JSON.stringify(event));
this.room.players.forEach(player => { this.room.players.forEach(player => {
console.log('player.playerId = ', player.playerId); console.log('player.playerId = ', player.playerId);
}); });
...@@ -224,50 +231,4 @@ class FakeRoom { ...@@ -224,50 +231,4 @@ class FakeRoom {
this.player = { playerId: playerId }; this.player = { playerId: playerId };
this.ownerId = playerId; this.ownerId = playerId;
} }
on(event, data) {
const eventNameList = [
'Leave',
'StartFrameSync',
'StopFrameSync',
'RecvFrame',
'Disconnect',
'Dismiss',
];
eventNameList.forEach(eventName => {
if (event == eventName) {
const func = this[`_on${event}`];
func && func(data);
}
});
}
_onJoin;
onJoin(func: Function) {
this._onJoin = func;
}
_onLeave;
onLeave(func: Function) {
this._onLeave = func;
}
_onStartFrameSync;
onStartFrameSync(func: Function) {
this._onStartFrameSync = func;
}
_onStopFrameSync;
onStopFrameSync(func: Function) {
this._onStopFrameSync = func;
}
_onRecvFrame;
onRecvFrame(func: Function) {
this._onRecvFrame = func;
}
_onDisconnect;
onDisconnect(func: Function) {
this._onDisconnect = func;
}
_onDismiss;
onDismiss(func: Function) {
this._onDismiss = func;
}
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment