import { _decorator, Component, Node, Prefab, director, instantiate } from 'cc'; import { EventEnum, SceneEnum } from '../Enum'; import DataManager from '../Global/DataManager'; import EventManager from '../Global/EventManager'; import NetworkManager, { ApiMsgEnum, IData } from '../Global/NetworkManager'; import { PlayerItemManager } from '../UI/PlayerItemManager'; import { RoomItemManager } from '../UI/RoomItemManager'; const { ccclass, property } = _decorator; @ccclass('HallManager') export class HallManager extends Component { @property(Node) playerContainer: Node = null; @property(Prefab) playerItem: Prefab = null; @property(Node) roomContainer: Node = null; @property(Prefab) roomItem: Prefab = null; onLoad() { director.preloadScene(SceneEnum.Room); EventManager.Instance.on(EventEnum.RoomJoin, this.joinRoom, this) NetworkManager.Instance.listenMsg(ApiMsgEnum.MsgPlayerList, this.renderPlayer); NetworkManager.Instance.listenMsg(ApiMsgEnum.MsgRoomList, this.renderRooms); } onDestroy() { EventManager.Instance.off(EventEnum.RoomJoin, this.joinRoom, this) NetworkManager.Instance.unlistenMsg(ApiMsgEnum.MsgPlayerList, this.renderPlayer); NetworkManager.Instance.unlistenMsg(ApiMsgEnum.MsgRoomList, this.renderRooms); } start() { this.getPlayers() this.getRooms() } async getPlayers() { const { success, res, error } = await NetworkManager.Instance.callApi(ApiMsgEnum.ApiPlayerList, {}); if (!success) { console.log(error) return; } this.renderPlayer(res) } renderPlayer = ({ list }: IData) => { for (const item of this.playerContainer.children) { item.active = false } while (this.playerContainer.children.length < list.length) { const playerItem = instantiate(this.playerItem); playerItem.active = false playerItem.setParent(this.playerContainer) } for (let i = 0; i < list.length; i++) { const data = list[i]; const node = this.playerContainer.children[i] const playerItemManager = node.getComponent(PlayerItemManager) playerItemManager.init(data) } } async getRooms() { const { success, res, error } = await NetworkManager.Instance.callApi(ApiMsgEnum.ApiRoomList, {}); if (!success) { console.log(error) return; } this.renderRooms(res) } renderRooms = ({ list }: IData) => { for (const item of this.roomContainer.children) { item.active = false } while (this.roomContainer.children.length < list.length) { const roomItem = instantiate(this.roomItem); roomItem.active = false roomItem.setParent(this.roomContainer) } for (let i = 0; i < list.length; i++) { const data = list[i]; const node = this.roomContainer.children[i] const roomItemManager = node.getComponent(RoomItemManager) roomItemManager.init(data) } } async createRoom() { const { success, res, error } = await NetworkManager.Instance.callApi(ApiMsgEnum.ApiRoomCreate, {}); if (!success) { console.log(error) return; } DataManager.Instance.roomInfo = { ...res } director.loadScene(SceneEnum.Room); } async joinRoom(rid: number) { const { success, res, error } = await NetworkManager.Instance.callApi(ApiMsgEnum.ApiRoomJoin, { rid }); if (!success) { console.log(error) return; } DataManager.Instance.roomInfo = { ...res } director.loadScene(SceneEnum.Room); } }