65 lines
1.9 KiB
TypeScript
Raw Normal View History

2022-12-01 22:26:41 +08:00
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';
const { ccclass, property } = _decorator;
@ccclass('RoomManager')
export class RoomManager extends Component {
@property(Node)
playerContainer: Node = null;
@property(Prefab)
playerItem: Prefab = null;
onLoad() {
director.preloadScene(SceneEnum.Battle);
NetworkManager.Instance.listenMsg(ApiMsgEnum.MsgRoom, this.renderPlayer);
}
onDestroy() {
NetworkManager.Instance.unlistenMsg(ApiMsgEnum.MsgRoom, this.renderPlayer);
}
async start() {
this.renderPlayer(DataManager.Instance.roomInfo)
}
renderPlayer = ({ players: list }: any) => {
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 handleLeave() {
const { success, res, error } = await NetworkManager.Instance.callApi(ApiMsgEnum.ApiRoomLeave, {});
if (!success) {
console.log(error)
return;
}
DataManager.Instance.roomInfo = null
director.loadScene(SceneEnum.Hall);
}
handleGameStart() {
console.log("handleGameStart");
}
}