152 lines
4.6 KiB
TypeScript
Raw Normal View History

2022-12-01 22:26:41 +08:00
import { _decorator, Component, Node, Prefab, instantiate, SpriteFrame } from 'cc';
2022-11-27 23:23:47 +08:00
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';
2022-12-01 22:26:41 +08:00
const { ccclass } = _decorator;
2022-11-27 23:23:47 +08:00
@ccclass('BattleManager')
export class BattleManager extends Component {
stage: Node
ui: Node
isInited = false
onLoad() {
2022-12-01 22:26:41 +08:00
this.stage = DataManager.Instance.stage = this.node.getChildByName("Stage")
2022-11-27 23:23:47 +08:00
this.ui = this.node.getChildByName("UI")
}
async start() {
this.clearGame()
await this.loadRes()
2022-12-01 22:26:41 +08:00
this.initScene()
await this.connectServer()
2022-11-27 23:23:47 +08:00
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)
}
2022-12-01 22:26:41 +08:00
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()
}
}
2022-11-27 23:23:47 +08:00
2022-12-01 22:26:41 +08:00
initJoyStick() {
const prefab = DataManager.Instance.prefabMap.get(EntityTypeEnum.JoyStick)
const joySitck = instantiate(prefab)
2022-11-27 23:23:47 +08:00
joySitck.setParent(this.ui)
const jm = DataManager.Instance.jm = joySitck.getComponent(JoyStickManager)
2022-12-01 22:26:41 +08:00
jm.init()
2022-11-27 23:23:47 +08:00
}
2022-12-01 22:26:41 +08:00
initShoot() {
const prefab = DataManager.Instance.prefabMap.get(EntityTypeEnum.Shoot)
const shoot = instantiate(prefab)
shoot.setParent(this.ui)
2022-11-27 23:23:47 +08:00
}
2022-12-01 22:26:41 +08:00
initMap() {
const prefab = DataManager.Instance.prefabMap.get(EntityTypeEnum.Map1)
const map = instantiate(prefab)
2022-11-27 23:23:47 +08:00
map.setParent(this.stage)
}
update(dt: number) {
if (!this.isInited) {
return
}
this.render()
this.tick(dt)
}
tick(dt: number) {
this.tickPlayer(dt)
2022-12-01 22:26:41 +08:00
this.tickGlobal(dt)
2022-11-27 23:23:47 +08:00
}
tickPlayer(dt: number) {
2022-12-01 22:26:41 +08:00
for (const p of DataManager.Instance.state.players) {
2022-11-27 23:23:47 +08:00
const playerManager = DataManager.Instance.playerMap.get(p.id)
if (!playerManager) {
return
}
playerManager.tick(dt)
}
}
2022-12-01 22:26:41 +08:00
tickGlobal(dt: number) {
DataManager.Instance.applyInput({
type: InputType.TimePast,
dt
})
}
2022-11-27 23:23:47 +08:00
render() {
this.renderPlayer()
this.renderBullet()
}
renderPlayer() {
2022-12-01 22:26:41 +08:00
for (const p of DataManager.Instance.state.players) {
2022-11-27 23:23:47 +08:00
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() {
2022-12-01 22:26:41 +08:00
for (const b of DataManager.Instance.state.bullets) {
2022-11-27 23:23:47 +08:00
let bulletManager = DataManager.Instance.bulletMap.get(b.id)
if (!bulletManager) {
2022-12-01 22:26:41 +08:00
const bullet = ObjectPoolManager.Instance.get(b.type)
2022-11-27 23:23:47 +08:00
bulletManager = bullet.getComponent(BulletManager) || bullet.addComponent(BulletManager)
DataManager.Instance.bulletMap.set(b.id, bulletManager)
bulletManager.init(b)
} else {
bulletManager.render(b)
}
}
}
}