78 lines
2.4 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, IMsgGameStart, IMsgRoom } from "../Common";
import { SceneEnum } from "../Enum";
import DataManager from "../Global/DataManager";
import NetworkManager from "../Global/NetworkManager";
import { PlayerManager } from "../UI/PlayerManager";
import { deepClone } from "../Utils";
2022-12-01 22:26:41 +08:00
const { ccclass, property } = _decorator;
2022-12-08 21:14:02 +08:00
@ccclass("RoomManager")
2022-12-01 22:26:41 +08:00
export class RoomManager extends Component {
2022-12-08 21:14:02 +08:00
@property(Node)
playerContainer: Node = null;
2022-12-01 22:26:41 +08:00
2022-12-08 21:14:02 +08:00
@property(Prefab)
playerPrefab: Prefab = null;
2022-12-01 22:26:41 +08:00
2022-12-08 21:14:02 +08:00
onLoad() {
director.preloadScene(SceneEnum.Battle);
NetworkManager.Instance.listenMsg(ApiMsgEnum.MsgRoom, this.renderPlayers, this);
NetworkManager.Instance.listenMsg(ApiMsgEnum.MsgGameStart, this.handleGameStart, this);
}
2022-12-01 22:26:41 +08:00
2022-12-08 21:14:02 +08:00
onDestroy() {
NetworkManager.Instance.unlistenMsg(ApiMsgEnum.MsgRoom, this.renderPlayers, this);
NetworkManager.Instance.unlistenMsg(ApiMsgEnum.MsgGameStart, this.handleGameStart, this);
}
2022-12-01 22:26:41 +08:00
2022-12-08 21:14:02 +08:00
async start() {
this.renderPlayers({
room: DataManager.Instance.roomInfo,
});
}
2022-12-01 22:26:41 +08:00
2022-12-08 21:14:02 +08:00
renderPlayers({ room: { players: list } }: IMsgRoom) {
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 playerItemManager = node.getComponent(PlayerManager);
playerItemManager.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 handleLeave() {
const { success, res, error } = await NetworkManager.Instance.callApi(ApiMsgEnum.ApiRoomLeave, {});
if (!success) {
console.log(error);
return;
2022-12-03 20:06:57 +08:00
}
2022-12-08 21:14:02 +08:00
DataManager.Instance.roomInfo = null;
director.loadScene(SceneEnum.Hall);
}
async handleStart() {
const { success, res, error } = await NetworkManager.Instance.callApi(ApiMsgEnum.ApiGameStart, { rid: DataManager.Instance.roomInfo.id });
if (!success) {
console.log(error);
return;
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
handleGameStart({ state }: IMsgGameStart) {
DataManager.Instance.state = state;
DataManager.Instance.lastState = deepClone(DataManager.Instance.state);
director.loadScene(SceneEnum.Battle);
}
}