122 lines
3.9 KiB
TypeScript
Raw Normal View History

2022-12-01 22:26:41 +08:00
import { _decorator, Component, Node, Prefab, director, instantiate } from 'cc';
2022-12-03 20:06:57 +08:00
import { ApiMsgEnum, IApiPlayerListRes, IApiRoomListRes, IMsgPlayerList, IMsgRoomList } from '../Common';
2022-12-01 22:26:41 +08:00
import { EventEnum, SceneEnum } from '../Enum';
import DataManager from '../Global/DataManager';
import EventManager from '../Global/EventManager';
2022-12-03 20:06:57 +08:00
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;
@ccclass('HallManager')
export class HallManager extends Component {
@property(Node)
playerContainer: Node = null;
@property(Prefab)
2022-12-03 20:06:57 +08:00
playerPrefab: Prefab = null;
2022-12-01 22:26:41 +08:00
@property(Node)
roomContainer: Node = null;
@property(Prefab)
2022-12-03 20:06:57 +08:00
roomPrefab: Prefab = null;
2022-12-01 22:26:41 +08:00
onLoad() {
director.preloadScene(SceneEnum.Room);
EventManager.Instance.on(EventEnum.RoomJoin, this.joinRoom, this)
2022-12-03 20:06:57 +08:00
NetworkManager.Instance.listenMsg(ApiMsgEnum.MsgPlayerList, this.renderPlayers);
2022-12-01 22:26:41 +08:00
NetworkManager.Instance.listenMsg(ApiMsgEnum.MsgRoomList, this.renderRooms);
}
onDestroy() {
EventManager.Instance.off(EventEnum.RoomJoin, this.joinRoom, this)
2022-12-03 20:06:57 +08:00
NetworkManager.Instance.unlistenMsg(ApiMsgEnum.MsgPlayerList, this.renderPlayers);
2022-12-01 22:26:41 +08:00
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;
}
2022-12-03 20:06:57 +08:00
this.renderPlayers(res)
2022-12-01 22:26:41 +08:00
}
2022-12-03 20:06:57 +08:00
renderPlayers = ({ list }: IApiPlayerListRes | IMsgPlayerList) => {
2022-12-01 22:26:41 +08:00
for (const item of this.playerContainer.children) {
item.active = false
}
while (this.playerContainer.children.length < list.length) {
2022-12-03 20:06:57 +08:00
const playerItem = instantiate(this.playerPrefab);
2022-12-01 22:26:41 +08:00
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]
2022-12-03 20:06:57 +08:00
const playerItemManager = node.getComponent(PlayerManager)
2022-12-01 22:26:41 +08:00
playerItemManager.init(data)
}
}
async getRooms() {
const { success, res, error } = await NetworkManager.Instance.callApi(ApiMsgEnum.ApiRoomList, {});
if (!success) {
console.log(error)
return;
}
this.renderRooms(res)
}
2022-12-03 20:06:57 +08:00
renderRooms = ({ list }: IApiRoomListRes | IMsgRoomList) => {
2022-12-01 22:26:41 +08:00
for (const item of this.roomContainer.children) {
item.active = false
}
while (this.roomContainer.children.length < list.length) {
2022-12-03 20:06:57 +08:00
const roomItem = instantiate(this.roomPrefab);
2022-12-01 22:26:41 +08:00
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]
2022-12-03 20:06:57 +08:00
const roomItemManager = node.getComponent(RoomManager)
2022-12-01 22:26:41 +08:00
roomItemManager.init(data)
}
}
async createRoom() {
const { success, res, error } = await NetworkManager.Instance.callApi(ApiMsgEnum.ApiRoomCreate, {});
if (!success) {
console.log(error)
return;
}
2022-12-03 20:06:57 +08:00
DataManager.Instance.roomInfo = res.room
2022-12-01 22:26:41 +08:00
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;
}
2022-12-03 20:06:57 +08:00
DataManager.Instance.roomInfo = res.room
2022-12-01 22:26:41 +08:00
director.loadScene(SceneEnum.Room);
}
}