update
This commit is contained in:
@@ -1,192 +1,218 @@
|
||||
import { _decorator, Component, Node, Prefab, instantiate, SpriteFrame, director } from 'cc';
|
||||
import { ActorManager } from '../Entity/Actor/ActorManager';
|
||||
import DataManager from '../Global/DataManager';
|
||||
import { JoyStickManager } from '../UI/JoyStickManager';
|
||||
import { ResourceManager } from '../Global/ResourceManager';
|
||||
import { EventEnum, PrefabPathEnum, SceneEnum, TexturePathEnum } from '../Enum';
|
||||
import NetworkManager from '../Global/NetworkManager';
|
||||
import ObjectPoolManager from '../Global/ObjectPoolManager';
|
||||
import { BulletManager } from '../Entity/Bullet/BulletManager';
|
||||
import { ApiMsgEnum, EntityTypeEnum, IMsgServerSync, InputTypeEnum } from '../Common';
|
||||
import EventManager from '../Global/EventManager';
|
||||
import { _decorator, Component, Node, Prefab, instantiate, SpriteFrame, director } from "cc";
|
||||
import { ActorManager } from "../Entity/Actor/ActorManager";
|
||||
import DataManager from "../Global/DataManager";
|
||||
import { JoyStickManager } from "../UI/JoyStickManager";
|
||||
import { ResourceManager } from "../Global/ResourceManager";
|
||||
import { EventEnum, PrefabPathEnum, SceneEnum, TexturePathEnum } from "../Enum";
|
||||
import NetworkManager from "../Global/NetworkManager";
|
||||
import ObjectPoolManager from "../Global/ObjectPoolManager";
|
||||
import { BulletManager } from "../Entity/Bullet/BulletManager";
|
||||
import { ApiMsgEnum, EntityTypeEnum, IClientInput, IMsgServerSync, InputTypeEnum, toFixed } from "../Common";
|
||||
import EventManager from "../Global/EventManager";
|
||||
import { deepClone } from "../Utils";
|
||||
|
||||
const { ccclass } = _decorator;
|
||||
|
||||
@ccclass('BattleManager')
|
||||
@ccclass("BattleManager")
|
||||
export class BattleManager extends Component {
|
||||
private stage: Node
|
||||
private ui: Node
|
||||
private shouldUpdate = false
|
||||
private stage: Node;
|
||||
private ui: Node;
|
||||
private shouldUpdate = false;
|
||||
private pendingMsg = [];
|
||||
|
||||
async start() {
|
||||
//清空
|
||||
this.clearGame()
|
||||
async start() {
|
||||
//清空
|
||||
this.clearGame();
|
||||
|
||||
//资源加载和网络连接同步执行
|
||||
await Promise.all([this.loadRes(), this.connectServer()])
|
||||
//资源加载和网络连接同步执行
|
||||
await Promise.all([this.loadRes(), this.connectServer()]);
|
||||
|
||||
this.initGame()
|
||||
this.initGame();
|
||||
|
||||
// 在场景初始化完毕之前,卡主别的玩家,准备好以后再告知服务器,等所有玩家都准备好以后才开始,这里就不做了
|
||||
NetworkManager.Instance.listenMsg(ApiMsgEnum.MsgServerSync, this.handleSync, this);
|
||||
NetworkManager.Instance.listenMsg(ApiMsgEnum.MsgGameEnd, this.leaveGame, this);
|
||||
EventManager.Instance.on(EventEnum.GameEnd, this.handleGameEnd, this)
|
||||
// 在场景初始化完毕之前,卡主别的玩家,准备好以后再告知服务器,等所有玩家都准备好以后才开始,这里就不做了
|
||||
NetworkManager.Instance.listenMsg(ApiMsgEnum.MsgServerSync, this.listenServerSync, this);
|
||||
NetworkManager.Instance.listenMsg(ApiMsgEnum.MsgGameEnd, this.listenGameEnd, this);
|
||||
EventManager.Instance.on(EventEnum.ClientSync, this.handleClientSync, this);
|
||||
EventManager.Instance.on(EventEnum.GameEnd, this.handleGameEnd, this);
|
||||
}
|
||||
|
||||
clearGame() {
|
||||
//事件
|
||||
NetworkManager.Instance.unlistenMsg(ApiMsgEnum.MsgServerSync, this.listenServerSync, this);
|
||||
NetworkManager.Instance.unlistenMsg(ApiMsgEnum.MsgGameEnd, this.listenGameEnd, this);
|
||||
EventManager.Instance.off(EventEnum.ClientSync, this.handleClientSync, this);
|
||||
EventManager.Instance.off(EventEnum.GameEnd, this.handleGameEnd, this);
|
||||
|
||||
//数据
|
||||
this.shouldUpdate = false;
|
||||
ObjectPoolManager.Instance.reset();
|
||||
DataManager.Instance.reset();
|
||||
|
||||
//节点
|
||||
this.stage = DataManager.Instance.stage = this.node.getChildByName("Stage");
|
||||
this.ui = this.node.getChildByName("UI");
|
||||
this.stage.destroyAllChildren();
|
||||
this.ui.destroyAllChildren();
|
||||
}
|
||||
|
||||
async loadRes() {
|
||||
const list = [];
|
||||
for (const type in PrefabPathEnum) {
|
||||
const p = ResourceManager.Instance.loadRes(PrefabPathEnum[type], Prefab).then((prefab) => {
|
||||
DataManager.Instance.prefabMap.set(type, prefab);
|
||||
});
|
||||
list.push(p);
|
||||
}
|
||||
|
||||
clearGame() {
|
||||
//监听
|
||||
NetworkManager.Instance.unlistenMsg(ApiMsgEnum.MsgServerSync, this.handleSync, this);
|
||||
NetworkManager.Instance.unlistenMsg(ApiMsgEnum.MsgGameEnd, this.leaveGame, this);
|
||||
EventManager.Instance.off(EventEnum.GameEnd, this.handleGameEnd, this)
|
||||
|
||||
//数据
|
||||
this.shouldUpdate = false
|
||||
ObjectPoolManager.Instance.reset()
|
||||
DataManager.Instance.reset()
|
||||
|
||||
//节点
|
||||
this.stage = DataManager.Instance.stage = this.node.getChildByName("Stage")
|
||||
this.ui = this.node.getChildByName("UI")
|
||||
this.stage.destroyAllChildren()
|
||||
this.ui.destroyAllChildren()
|
||||
for (const type in TexturePathEnum) {
|
||||
const p = ResourceManager.Instance.loadDir(TexturePathEnum[type], SpriteFrame).then((spriteFrames) => {
|
||||
DataManager.Instance.textureMap.set(type, spriteFrames);
|
||||
});
|
||||
list.push(p);
|
||||
}
|
||||
await Promise.all(list);
|
||||
}
|
||||
|
||||
async loadRes() {
|
||||
const list = []
|
||||
for (const type in PrefabPathEnum) {
|
||||
const p = ResourceManager.Instance.loadRes(PrefabPathEnum[type], Prefab).then((prefab) => {
|
||||
DataManager.Instance.prefabMap.set(type, prefab)
|
||||
})
|
||||
list.push(p)
|
||||
}
|
||||
for (const type in TexturePathEnum) {
|
||||
const p = ResourceManager.Instance.loadDir(TexturePathEnum[type], SpriteFrame).then((spriteFrames) => {
|
||||
DataManager.Instance.textureMap.set(type, spriteFrames)
|
||||
})
|
||||
list.push(p)
|
||||
}
|
||||
await Promise.all(list)
|
||||
async connectServer() {
|
||||
if (!(await NetworkManager.Instance.connect().catch(() => false))) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 1000));
|
||||
await this.connectServer();
|
||||
}
|
||||
}
|
||||
|
||||
async connectServer() {
|
||||
if (!await NetworkManager.Instance.connect().catch(() => false)) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 1000))
|
||||
await this.connectServer()
|
||||
}
|
||||
async initGame() {
|
||||
this.initJoyStick();
|
||||
this.initShoot();
|
||||
this.initMap();
|
||||
this.shouldUpdate = true;
|
||||
}
|
||||
|
||||
initJoyStick() {
|
||||
const prefab = DataManager.Instance.prefabMap.get(EntityTypeEnum.JoyStick);
|
||||
const joySitck = instantiate(prefab);
|
||||
joySitck.setParent(this.ui);
|
||||
const jm = (DataManager.Instance.jm = joySitck.getComponent(JoyStickManager));
|
||||
jm.init();
|
||||
}
|
||||
|
||||
initShoot() {
|
||||
const prefab = DataManager.Instance.prefabMap.get(EntityTypeEnum.Shoot);
|
||||
const shoot = instantiate(prefab);
|
||||
shoot.setParent(this.ui);
|
||||
}
|
||||
|
||||
initMap() {
|
||||
const prefab = DataManager.Instance.prefabMap.get(EntityTypeEnum.Map1);
|
||||
const map = instantiate(prefab);
|
||||
map.setParent(this.stage);
|
||||
}
|
||||
|
||||
update(dt: number) {
|
||||
if (!this.shouldUpdate) {
|
||||
return;
|
||||
}
|
||||
this.render();
|
||||
this.tick(dt);
|
||||
}
|
||||
|
||||
leaveGame() {
|
||||
this.clearGame()
|
||||
director.loadScene(SceneEnum.Hall);
|
||||
tick(dt: number) {
|
||||
this.tickPlayer(dt);
|
||||
// this.tickGlobal(dt)
|
||||
}
|
||||
|
||||
tickPlayer(dt: number) {
|
||||
for (const p of DataManager.Instance.state.actors) {
|
||||
const playerManager = DataManager.Instance.actorMap.get(p.id);
|
||||
if (!playerManager) {
|
||||
return;
|
||||
}
|
||||
playerManager.tick(dt);
|
||||
}
|
||||
}
|
||||
|
||||
async handleGameEnd() {
|
||||
const { success, res, error } = await NetworkManager.Instance.callApi(ApiMsgEnum.ApiGameEnd, { rid: DataManager.Instance.roomInfo.id })
|
||||
if (!success) {
|
||||
console.log(error)
|
||||
return;
|
||||
}
|
||||
// tickGlobal(dt: number) {
|
||||
// DataManager.Instance.applyInput({
|
||||
// type: InputTypeEnum.TimePast,
|
||||
// dt: toFixed(dt),
|
||||
// })
|
||||
// }
|
||||
|
||||
render() {
|
||||
this.renderPlayer();
|
||||
this.renderBullet();
|
||||
}
|
||||
|
||||
renderPlayer() {
|
||||
for (const p of DataManager.Instance.state.actors) {
|
||||
let actorManager = DataManager.Instance.actorMap.get(p.id);
|
||||
if (!actorManager) {
|
||||
const playerPrefab = DataManager.Instance.prefabMap.get(p.type);
|
||||
const player = instantiate(playerPrefab);
|
||||
player.setParent(this.stage);
|
||||
actorManager = player.addComponent(ActorManager);
|
||||
DataManager.Instance.actorMap.set(p.id, actorManager);
|
||||
actorManager.init(p);
|
||||
} else {
|
||||
actorManager.render(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async initGame() {
|
||||
this.initJoyStick()
|
||||
this.initShoot()
|
||||
this.initMap()
|
||||
this.shouldUpdate = true
|
||||
renderBullet() {
|
||||
for (const b of DataManager.Instance.state.bullets) {
|
||||
let bulletManager = DataManager.Instance.bulletMap.get(b.id);
|
||||
if (!bulletManager) {
|
||||
const bullet = ObjectPoolManager.Instance.get(b.type);
|
||||
bulletManager = bullet.getComponent(BulletManager) || bullet.addComponent(BulletManager);
|
||||
DataManager.Instance.bulletMap.set(b.id, bulletManager);
|
||||
bulletManager.init(b);
|
||||
} else {
|
||||
bulletManager.render(b);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
initJoyStick() {
|
||||
const prefab = DataManager.Instance.prefabMap.get(EntityTypeEnum.JoyStick)
|
||||
const joySitck = instantiate(prefab)
|
||||
joySitck.setParent(this.ui)
|
||||
const jm = DataManager.Instance.jm = joySitck.getComponent(JoyStickManager)
|
||||
jm.init()
|
||||
handleClientSync(input: IClientInput) {
|
||||
const msg = {
|
||||
frameId: DataManager.Instance.frameId++,
|
||||
input,
|
||||
};
|
||||
NetworkManager.Instance.sendMsg(ApiMsgEnum.MsgClientSync, msg);
|
||||
|
||||
//移动才做预测,射击不做
|
||||
if (input.type === InputTypeEnum.ActorMove) {
|
||||
DataManager.Instance.applyInput(input);
|
||||
this.pendingMsg.push(msg);
|
||||
}
|
||||
}
|
||||
|
||||
initShoot() {
|
||||
const prefab = DataManager.Instance.prefabMap.get(EntityTypeEnum.Shoot)
|
||||
const shoot = instantiate(prefab)
|
||||
shoot.setParent(this.ui)
|
||||
listenServerSync({ lastFrameId, inputs }: IMsgServerSync) {
|
||||
//回滚上次服务器状态
|
||||
DataManager.Instance.state = DataManager.Instance.lastState;
|
||||
//应用服务器输入
|
||||
for (const input of inputs) {
|
||||
DataManager.Instance.applyInput(input);
|
||||
}
|
||||
//记录最新的服务器状态
|
||||
DataManager.Instance.lastState = deepClone(DataManager.Instance.state);
|
||||
|
||||
initMap() {
|
||||
const prefab = DataManager.Instance.prefabMap.get(EntityTypeEnum.Map1)
|
||||
const map = instantiate(prefab)
|
||||
map.setParent(this.stage)
|
||||
//过滤本地输入
|
||||
this.pendingMsg = this.pendingMsg.filter((msg) => msg.frameId > lastFrameId);
|
||||
//应用本地输入
|
||||
for (const msg of this.pendingMsg) {
|
||||
DataManager.Instance.applyInput(msg.input);
|
||||
}
|
||||
}
|
||||
|
||||
update(dt: number) {
|
||||
if (!this.shouldUpdate) {
|
||||
return
|
||||
}
|
||||
this.render()
|
||||
this.tick(dt)
|
||||
}
|
||||
|
||||
tick(dt: number) {
|
||||
this.tickPlayer(dt)
|
||||
// this.tickGlobal(dt)
|
||||
}
|
||||
|
||||
tickPlayer(dt: number) {
|
||||
for (const p of DataManager.Instance.state.players) {
|
||||
const playerManager = DataManager.Instance.actorMap.get(p.id)
|
||||
if (!playerManager) {
|
||||
return
|
||||
}
|
||||
playerManager.tick(dt)
|
||||
}
|
||||
}
|
||||
|
||||
// tickGlobal(dt: number) {
|
||||
// NetworkManager.Instance.sendMsg(ApiMsgEnum.MsgClientSync, {
|
||||
// input: {
|
||||
// type: InputTypeEnum.TimePast,
|
||||
// dt: toFixed(dt),
|
||||
// }
|
||||
// })
|
||||
// }
|
||||
|
||||
render() {
|
||||
this.renderPlayer()
|
||||
this.renderBullet()
|
||||
}
|
||||
|
||||
renderPlayer() {
|
||||
for (const p of DataManager.Instance.state.players) {
|
||||
let playerManager = DataManager.Instance.actorMap.get(p.id)
|
||||
if (!playerManager) {
|
||||
const playerPrefab = DataManager.Instance.prefabMap.get(p.type)
|
||||
const player = instantiate(playerPrefab)
|
||||
player.setParent(this.stage)
|
||||
playerManager = player.addComponent(ActorManager)
|
||||
DataManager.Instance.actorMap.set(p.id, playerManager)
|
||||
playerManager.init(p)
|
||||
} else {
|
||||
playerManager.render(p)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
renderBullet() {
|
||||
for (const b of DataManager.Instance.state.bullets) {
|
||||
let bulletManager = DataManager.Instance.bulletMap.get(b.id)
|
||||
if (!bulletManager) {
|
||||
const bullet = ObjectPoolManager.Instance.get(b.type)
|
||||
bulletManager = bullet.getComponent(BulletManager) || bullet.addComponent(BulletManager)
|
||||
DataManager.Instance.bulletMap.set(b.id, bulletManager)
|
||||
bulletManager.init(b)
|
||||
} else {
|
||||
bulletManager.render(b)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
handleSync(inputs: IMsgServerSync) {
|
||||
for (const input of inputs) {
|
||||
DataManager.Instance.applyInput(input)
|
||||
}
|
||||
async handleGameEnd() {
|
||||
const { success, res, error } = await NetworkManager.Instance.callApi(ApiMsgEnum.ApiGameEnd, { rid: DataManager.Instance.roomInfo.id });
|
||||
if (!success) {
|
||||
console.log(error);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
listenGameEnd() {
|
||||
this.clearGame();
|
||||
director.loadScene(SceneEnum.Hall);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,121 +1,122 @@
|
||||
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';
|
||||
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";
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass('HallManager')
|
||||
@ccclass("HallManager")
|
||||
export class HallManager extends Component {
|
||||
@property(Node)
|
||||
playerContainer: Node = null;
|
||||
@property(Node)
|
||||
playerContainer: Node = null;
|
||||
|
||||
@property(Prefab)
|
||||
playerPrefab: Prefab = null;
|
||||
@property(Prefab)
|
||||
playerPrefab: Prefab = null;
|
||||
|
||||
@property(Node)
|
||||
roomContainer: Node = null;
|
||||
@property(Node)
|
||||
roomContainer: Node = null;
|
||||
|
||||
@property(Prefab)
|
||||
roomPrefab: Prefab = null;
|
||||
@property(Prefab)
|
||||
roomPrefab: Prefab = null;
|
||||
|
||||
onLoad() {
|
||||
director.preloadScene(SceneEnum.Room);
|
||||
EventManager.Instance.on(EventEnum.RoomJoin, this.joinRoom, this)
|
||||
NetworkManager.Instance.listenMsg(ApiMsgEnum.MsgPlayerList, this.renderPlayers, this);
|
||||
NetworkManager.Instance.listenMsg(ApiMsgEnum.MsgRoomList, this.renderRooms, this);
|
||||
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;
|
||||
}
|
||||
|
||||
onDestroy() {
|
||||
EventManager.Instance.off(EventEnum.RoomJoin, this.joinRoom, this)
|
||||
NetworkManager.Instance.unlistenMsg(ApiMsgEnum.MsgPlayerList, this.renderPlayers, this);
|
||||
NetworkManager.Instance.unlistenMsg(ApiMsgEnum.MsgRoomList, this.renderRooms, this);
|
||||
this.renderPlayers(res);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
start() {
|
||||
this.getPlayers()
|
||||
this.getRooms()
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
async getRooms() {
|
||||
const { success, res, error } = await NetworkManager.Instance.callApi(ApiMsgEnum.ApiRoomList, {});
|
||||
if (!success) {
|
||||
console.log(error);
|
||||
return;
|
||||
}
|
||||
|
||||
async getPlayers() {
|
||||
const { success, res, error } = await NetworkManager.Instance.callApi(ApiMsgEnum.ApiPlayerList, {});
|
||||
if (!success) {
|
||||
console.log(error)
|
||||
return;
|
||||
}
|
||||
this.renderRooms(res);
|
||||
}
|
||||
|
||||
this.renderPlayers(res)
|
||||
renderRooms = ({ list }: IApiRoomListRes | IMsgRoomList) => {
|
||||
for (const item of this.roomContainer.children) {
|
||||
item.active = false;
|
||||
}
|
||||
while (this.roomContainer.children.length < list.length) {
|
||||
const roomItem = instantiate(this.roomPrefab);
|
||||
roomItem.active = false;
|
||||
roomItem.setParent(this.roomContainer);
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
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);
|
||||
}
|
||||
};
|
||||
|
||||
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)
|
||||
}
|
||||
async handleCreateRoom() {
|
||||
const { success, res, error } = await NetworkManager.Instance.callApi(ApiMsgEnum.ApiRoomCreate, {});
|
||||
if (!success) {
|
||||
console.log(error);
|
||||
return;
|
||||
}
|
||||
|
||||
async getRooms() {
|
||||
const { success, res, error } = await NetworkManager.Instance.callApi(ApiMsgEnum.ApiRoomList, {});
|
||||
if (!success) {
|
||||
console.log(error)
|
||||
return;
|
||||
}
|
||||
DataManager.Instance.roomInfo = res.room;
|
||||
director.loadScene(SceneEnum.Room);
|
||||
}
|
||||
|
||||
this.renderRooms(res)
|
||||
async handleJoinRoom(rid: number) {
|
||||
const { success, res, error } = await NetworkManager.Instance.callApi(ApiMsgEnum.ApiRoomJoin, { rid });
|
||||
if (!success) {
|
||||
console.log(error);
|
||||
return;
|
||||
}
|
||||
|
||||
renderRooms = ({ list }: IApiRoomListRes | IMsgRoomList) => {
|
||||
for (const item of this.roomContainer.children) {
|
||||
item.active = false
|
||||
}
|
||||
while (this.roomContainer.children.length < list.length) {
|
||||
const roomItem = instantiate(this.roomPrefab);
|
||||
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(RoomManager)
|
||||
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.room
|
||||
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.room
|
||||
director.loadScene(SceneEnum.Room);
|
||||
}
|
||||
DataManager.Instance.roomInfo = res.room;
|
||||
director.loadScene(SceneEnum.Room);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,45 +1,44 @@
|
||||
import { _decorator, Component, EditBox, director } from 'cc';
|
||||
import { ApiMsgEnum } from '../Common';
|
||||
import { SceneEnum } from '../Enum';
|
||||
import DataManager from '../Global/DataManager';
|
||||
import NetworkManager from '../Global/NetworkManager';
|
||||
import { _decorator, Component, EditBox, director } from "cc";
|
||||
import { ApiMsgEnum } from "../Common";
|
||||
import { SceneEnum } from "../Enum";
|
||||
import DataManager from "../Global/DataManager";
|
||||
import NetworkManager from "../Global/NetworkManager";
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass('LoginManager')
|
||||
@ccclass("LoginManager")
|
||||
export class LoginManager extends Component {
|
||||
input: EditBox
|
||||
input: EditBox;
|
||||
|
||||
onLoad() {
|
||||
this.input = this.node.getChildByName('Input').getComponent(EditBox)
|
||||
director.preloadScene(SceneEnum.Hall);
|
||||
onLoad() {
|
||||
this.input = this.node.getChildByName("Input").getComponent(EditBox);
|
||||
director.preloadScene(SceneEnum.Hall);
|
||||
}
|
||||
|
||||
async start() {
|
||||
await NetworkManager.Instance.connect();
|
||||
console.log("服务连接成功!");
|
||||
}
|
||||
|
||||
async handleClick() {
|
||||
if (!NetworkManager.Instance.isConnected) {
|
||||
console.log("未连接!");
|
||||
await NetworkManager.Instance.connect();
|
||||
}
|
||||
const nickname = this.input.string;
|
||||
if (!nickname) {
|
||||
console.log("请输入昵称!");
|
||||
return;
|
||||
}
|
||||
let { success, res, error } = await NetworkManager.Instance.callApi(ApiMsgEnum.ApiPlayerJoin, {
|
||||
nickname,
|
||||
});
|
||||
|
||||
if (!success) {
|
||||
console.log(error);
|
||||
return;
|
||||
}
|
||||
|
||||
async start() {
|
||||
await NetworkManager.Instance.connect();
|
||||
console.log("服务连接成功!");
|
||||
}
|
||||
|
||||
async handleClick() {
|
||||
if (!NetworkManager.Instance.isConnected) {
|
||||
console.log("未连接!");
|
||||
await NetworkManager.Instance.connect();
|
||||
}
|
||||
const nickname = this.input.string;
|
||||
if (!nickname) {
|
||||
console.log("请输入昵称!")
|
||||
return;
|
||||
}
|
||||
let { success, res, error } = await NetworkManager.Instance.callApi(ApiMsgEnum.ApiPlayerJoin, {
|
||||
nickname,
|
||||
});
|
||||
|
||||
if (!success) {
|
||||
console.log(error);
|
||||
return;
|
||||
}
|
||||
|
||||
DataManager.Instance.myPlayerId = res.player.id;
|
||||
director.loadScene(SceneEnum.Hall);
|
||||
}
|
||||
DataManager.Instance.myPlayerId = res.player.id;
|
||||
director.loadScene(SceneEnum.Hall);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,76 +1,77 @@
|
||||
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 { _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";
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass('RoomManager')
|
||||
@ccclass("RoomManager")
|
||||
export class RoomManager extends Component {
|
||||
@property(Node)
|
||||
playerContainer: Node = null;
|
||||
@property(Node)
|
||||
playerContainer: Node = null;
|
||||
|
||||
@property(Prefab)
|
||||
playerPrefab: Prefab = null;
|
||||
@property(Prefab)
|
||||
playerPrefab: Prefab = null;
|
||||
|
||||
onLoad() {
|
||||
director.preloadScene(SceneEnum.Battle);
|
||||
NetworkManager.Instance.listenMsg(ApiMsgEnum.MsgRoom, this.renderPlayers, this);
|
||||
NetworkManager.Instance.listenMsg(ApiMsgEnum.MsgGameStart, this.handleGameStart, this);
|
||||
onLoad() {
|
||||
director.preloadScene(SceneEnum.Battle);
|
||||
NetworkManager.Instance.listenMsg(ApiMsgEnum.MsgRoom, this.renderPlayers, this);
|
||||
NetworkManager.Instance.listenMsg(ApiMsgEnum.MsgGameStart, this.handleGameStart, this);
|
||||
}
|
||||
|
||||
onDestroy() {
|
||||
NetworkManager.Instance.unlistenMsg(ApiMsgEnum.MsgRoom, this.renderPlayers, this);
|
||||
NetworkManager.Instance.unlistenMsg(ApiMsgEnum.MsgGameStart, this.handleGameStart, this);
|
||||
}
|
||||
|
||||
async start() {
|
||||
this.renderPlayers({
|
||||
room: DataManager.Instance.roomInfo,
|
||||
});
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
onDestroy() {
|
||||
NetworkManager.Instance.unlistenMsg(ApiMsgEnum.MsgRoom, this.renderPlayers, this);
|
||||
NetworkManager.Instance.unlistenMsg(ApiMsgEnum.MsgGameStart, this.handleGameStart, this);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
async handleLeave() {
|
||||
const { success, res, error } = await NetworkManager.Instance.callApi(ApiMsgEnum.ApiRoomLeave, {});
|
||||
if (!success) {
|
||||
console.log(error);
|
||||
return;
|
||||
}
|
||||
|
||||
async start() {
|
||||
this.renderPlayers({
|
||||
room: DataManager.Instance.roomInfo
|
||||
})
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
async handleStart() {
|
||||
const { success, res, error } = await NetworkManager.Instance.callApi(ApiMsgEnum.ApiGameStart, { rid: DataManager.Instance.roomInfo.id });
|
||||
if (!success) {
|
||||
console.log(error)
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
handleGameStart({ state }: IMsgGameStart) {
|
||||
DataManager.Instance.state = state
|
||||
director.loadScene(SceneEnum.Battle);
|
||||
}
|
||||
handleGameStart({ state }: IMsgGameStart) {
|
||||
DataManager.Instance.state = state;
|
||||
DataManager.Instance.lastState = deepClone(DataManager.Instance.state);
|
||||
director.loadScene(SceneEnum.Battle);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user