123 lines
3.7 KiB
TypeScript
Raw Normal View History

2022-12-08 21:14:02 +08:00
import { _decorator, Component, Node, Prefab, director, instantiate } from "cc";
import { ApiMsgEnum, IApiPlayerListRes, IApiRoomListRes, IMsgPlayerList, IMsgRoomList } from "../Common";
import { EventEnum, SceneEnum } from "../Enum";
import DataManager from "../Global/DataManager";
import EventManager from "../Global/EventManager";
import NetworkManager from "../Global/NetworkManager";
import { PlayerManager } from "../UI/PlayerManager";
import { RoomManager } from "../UI/RoomManager";
2022-12-01 22:26:41 +08:00
const { ccclass, property } = _decorator;
2022-12-08 21:14:02 +08:00
@ccclass("HallManager")
2022-12-01 22:26:41 +08:00
export class HallManager extends Component {
2022-12-08 21:14:02 +08:00
@property(Node)
playerContainer: Node = null;
@property(Prefab)
playerPrefab: Prefab = null;
@property(Node)
roomContainer: Node = null;
@property(Prefab)
roomPrefab: Prefab = null;
onLoad() {
director.preloadScene(SceneEnum.Room);
EventManager.Instance.on(EventEnum.RoomJoin, this.handleJoinRoom, this);
NetworkManager.Instance.listenMsg(ApiMsgEnum.MsgPlayerList, this.renderPlayers, this);
NetworkManager.Instance.listenMsg(ApiMsgEnum.MsgRoomList, this.renderRooms, this);
}
onDestroy() {
EventManager.Instance.off(EventEnum.RoomJoin, this.handleJoinRoom, this);
NetworkManager.Instance.unlistenMsg(ApiMsgEnum.MsgPlayerList, this.renderPlayers, this);
NetworkManager.Instance.unlistenMsg(ApiMsgEnum.MsgRoomList, this.renderRooms, this);
}
start() {
this.playerContainer.destroyAllChildren();
this.roomContainer.destroyAllChildren();
this.getPlayers();
this.getRooms();
}
async getPlayers() {
const { success, res, error } = await NetworkManager.Instance.callApi(ApiMsgEnum.ApiPlayerList, {});
if (!success) {
console.log(error);
return;
}
2022-12-01 22:26:41 +08:00
2022-12-08 21:14:02 +08:00
this.renderPlayers(res);
}
2022-12-01 22:26:41 +08:00
2022-12-08 21:14:02 +08:00
renderPlayers({ list }: IApiPlayerListRes | IMsgPlayerList) {
for (const item of this.playerContainer.children) {
item.active = false;
}
while (this.playerContainer.children.length < list.length) {
const playerItem = instantiate(this.playerPrefab);
playerItem.active = false;
playerItem.setParent(this.playerContainer);
2022-12-01 22:26:41 +08:00
}
2022-12-08 21:14:02 +08:00
for (let i = 0; i < list.length; i++) {
const data = list[i];
const node = this.playerContainer.children[i];
const playerManager = node.getComponent(PlayerManager);
playerManager.init(data);
2022-12-01 22:26:41 +08:00
}
2022-12-08 21:14:02 +08:00
}
2022-12-01 22:26:41 +08:00
2022-12-08 21:14:02 +08:00
async getRooms() {
const { success, res, error } = await NetworkManager.Instance.callApi(ApiMsgEnum.ApiRoomList, {});
if (!success) {
console.log(error);
return;
2022-12-01 22:26:41 +08:00
}
2022-12-08 21:14:02 +08:00
this.renderRooms(res);
}
2022-12-01 22:26:41 +08:00
2022-12-08 21:14:02 +08:00
renderRooms = ({ list }: IApiRoomListRes | IMsgRoomList) => {
for (const item of this.roomContainer.children) {
item.active = false;
2022-12-01 22:26:41 +08:00
}
2022-12-08 21:14:02 +08:00
while (this.roomContainer.children.length < list.length) {
const roomItem = instantiate(this.roomPrefab);
roomItem.active = false;
roomItem.setParent(this.roomContainer);
2022-12-01 22:26:41 +08:00
}
2022-12-08 21:14:02 +08:00
for (let i = 0; i < list.length; i++) {
const data = list[i];
const node = this.roomContainer.children[i];
const roomItemManager = node.getComponent(RoomManager);
roomItemManager.init(data);
2022-12-01 22:26:41 +08:00
}
2022-12-08 21:14:02 +08:00
};
2022-12-01 22:26:41 +08:00
2022-12-08 21:14:02 +08:00
async handleCreateRoom() {
const { success, res, error } = await NetworkManager.Instance.callApi(ApiMsgEnum.ApiRoomCreate, {});
if (!success) {
console.log(error);
return;
2022-12-01 22:26:41 +08:00
}
2022-12-08 21:14:02 +08:00
DataManager.Instance.roomInfo = res.room;
director.loadScene(SceneEnum.Room);
}
2022-12-01 22:26:41 +08:00
2022-12-08 21:14:02 +08:00
async handleJoinRoom(rid: number) {
const { success, res, error } = await NetworkManager.Instance.callApi(ApiMsgEnum.ApiRoomJoin, { rid });
if (!success) {
console.log(error);
return;
2022-12-01 22:26:41 +08:00
}
2022-12-08 21:14:02 +08:00
DataManager.Instance.roomInfo = res.room;
director.loadScene(SceneEnum.Room);
}
2022-12-01 22:26:41 +08:00
}