update
This commit is contained in:
151
apps/client/assets/Scripts/Scene/BattleManager.ts
Normal file
151
apps/client/assets/Scripts/Scene/BattleManager.ts
Normal file
@@ -0,0 +1,151 @@
|
||||
import { _decorator, Component, Node, Prefab, instantiate, SpriteFrame } from 'cc';
|
||||
import { PlayerManager } from '../Entity/Player/PlayerManager';
|
||||
import DataManager from '../Global/DataManager';
|
||||
import { JoyStickManager } from '../UI/JoyStickManager';
|
||||
import { ResourceManager } from '../Global/ResourceManager';
|
||||
import { EntityTypeEnum, InputType, PrefabPathEnum, TexturePathEnum } from '../Enum';
|
||||
import NetworkManager from '../Global/NetworkManager';
|
||||
import ObjectPoolManager from '../Global/ObjectPoolManager';
|
||||
import { BulletManager } from '../Entity/Bullet/BulletManager';
|
||||
|
||||
const { ccclass } = _decorator;
|
||||
|
||||
@ccclass('BattleManager')
|
||||
export class BattleManager extends Component {
|
||||
stage: Node
|
||||
ui: Node
|
||||
isInited = false
|
||||
onLoad() {
|
||||
this.stage = DataManager.Instance.stage = this.node.getChildByName("Stage")
|
||||
this.ui = this.node.getChildByName("UI")
|
||||
}
|
||||
|
||||
async start() {
|
||||
this.clearGame()
|
||||
await this.loadRes()
|
||||
this.initScene()
|
||||
await this.connectServer()
|
||||
this.isInited = true
|
||||
}
|
||||
|
||||
clearGame() {
|
||||
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)
|
||||
}
|
||||
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 initScene() {
|
||||
this.initJoyStick()
|
||||
this.initShoot()
|
||||
this.initMap()
|
||||
}
|
||||
|
||||
async connectServer() {
|
||||
if (!await NetworkManager.Instance.connect().catch(() => false)) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 1000))
|
||||
await this.connectServer()
|
||||
}
|
||||
}
|
||||
|
||||
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.isInited) {
|
||||
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.playerMap.get(p.id)
|
||||
if (!playerManager) {
|
||||
return
|
||||
}
|
||||
playerManager.tick(dt)
|
||||
}
|
||||
}
|
||||
|
||||
tickGlobal(dt: number) {
|
||||
DataManager.Instance.applyInput({
|
||||
type: InputType.TimePast,
|
||||
dt
|
||||
})
|
||||
}
|
||||
|
||||
render() {
|
||||
this.renderPlayer()
|
||||
this.renderBullet()
|
||||
}
|
||||
|
||||
renderPlayer() {
|
||||
for (const p of DataManager.Instance.state.players) {
|
||||
let playerManager = DataManager.Instance.playerMap.get(p.id)
|
||||
if (!playerManager) {
|
||||
const playerPrefab = DataManager.Instance.prefabMap.get(p.type)
|
||||
const player = instantiate(playerPrefab)
|
||||
player.setParent(this.stage)
|
||||
playerManager = player.addComponent(PlayerManager)
|
||||
DataManager.Instance.playerMap.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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
9
apps/client/assets/Scripts/Scene/BattleManager.ts.meta
Normal file
9
apps/client/assets/Scripts/Scene/BattleManager.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "d8c46424-e6ab-4d22-af07-293ce98d8b5b",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
122
apps/client/assets/Scripts/Scene/HallManager.ts
Normal file
122
apps/client/assets/Scripts/Scene/HallManager.ts
Normal file
@@ -0,0 +1,122 @@
|
||||
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';
|
||||
import { RoomItemManager } from '../UI/RoomItemManager';
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass('HallManager')
|
||||
export class HallManager extends Component {
|
||||
@property(Node)
|
||||
playerContainer: Node = null;
|
||||
|
||||
@property(Prefab)
|
||||
playerItem: Prefab = null;
|
||||
|
||||
@property(Node)
|
||||
roomContainer: Node = null;
|
||||
|
||||
@property(Prefab)
|
||||
roomItem: Prefab = null;
|
||||
|
||||
onLoad() {
|
||||
director.preloadScene(SceneEnum.Room);
|
||||
EventManager.Instance.on(EventEnum.RoomJoin, this.joinRoom, this)
|
||||
NetworkManager.Instance.listenMsg(ApiMsgEnum.MsgPlayerList, this.renderPlayer);
|
||||
NetworkManager.Instance.listenMsg(ApiMsgEnum.MsgRoomList, this.renderRooms);
|
||||
}
|
||||
|
||||
onDestroy() {
|
||||
EventManager.Instance.off(EventEnum.RoomJoin, this.joinRoom, this)
|
||||
NetworkManager.Instance.unlistenMsg(ApiMsgEnum.MsgPlayerList, this.renderPlayer);
|
||||
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;
|
||||
}
|
||||
|
||||
this.renderPlayer(res)
|
||||
}
|
||||
|
||||
renderPlayer = ({ list }: IData) => {
|
||||
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 getRooms() {
|
||||
const { success, res, error } = await NetworkManager.Instance.callApi(ApiMsgEnum.ApiRoomList, {});
|
||||
if (!success) {
|
||||
console.log(error)
|
||||
return;
|
||||
}
|
||||
|
||||
this.renderRooms(res)
|
||||
}
|
||||
|
||||
renderRooms = ({ list }: IData) => {
|
||||
for (const item of this.roomContainer.children) {
|
||||
item.active = false
|
||||
}
|
||||
while (this.roomContainer.children.length < list.length) {
|
||||
const roomItem = instantiate(this.roomItem);
|
||||
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(RoomItemManager)
|
||||
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 }
|
||||
|
||||
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 }
|
||||
|
||||
director.loadScene(SceneEnum.Room);
|
||||
}
|
||||
}
|
||||
|
9
apps/client/assets/Scripts/Scene/HallManager.ts.meta
Normal file
9
apps/client/assets/Scripts/Scene/HallManager.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "a14b4d33-c5f8-455c-a935-dbfd7f3ab62b",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
45
apps/client/assets/Scripts/Scene/LoginManager.ts
Normal file
45
apps/client/assets/Scripts/Scene/LoginManager.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { _decorator, Component, Node, EditBox, Button, director } from 'cc';
|
||||
import { SceneEnum } from '../Enum';
|
||||
import DataManager from '../Global/DataManager';
|
||||
import NetworkManager, { ApiMsgEnum } from '../Global/NetworkManager';
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass('LoginManager')
|
||||
export class LoginManager extends Component {
|
||||
input: EditBox
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
DataManager.Instance.myPlayerId = res.id;
|
||||
director.loadScene(SceneEnum.Hall);
|
||||
}
|
||||
}
|
||||
|
9
apps/client/assets/Scripts/Scene/LoginManager.ts.meta
Normal file
9
apps/client/assets/Scripts/Scene/LoginManager.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "3be66806-c736-419c-b6cc-0dab1e13cbda",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
64
apps/client/assets/Scripts/Scene/RoomManager.ts
Normal file
64
apps/client/assets/Scripts/Scene/RoomManager.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
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");
|
||||
|
||||
}
|
||||
}
|
||||
|
9
apps/client/assets/Scripts/Scene/RoomManager.ts.meta
Normal file
9
apps/client/assets/Scripts/Scene/RoomManager.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "8ee1bd73-5b3b-48ad-a0a0-5a1d6a08da7d",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
Reference in New Issue
Block a user