157 lines
4.9 KiB
TypeScript
157 lines
4.9 KiB
TypeScript
|
import { _decorator, Component, Node, resources, Prefab, instantiate, Vec2, 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, property, requireComponent } = _decorator;
|
||
|
|
||
|
@ccclass('BattleManager')
|
||
|
export class BattleManager extends Component {
|
||
|
stage: Node
|
||
|
ui: Node
|
||
|
|
||
|
isInited = false
|
||
|
|
||
|
|
||
|
onLoad() {
|
||
|
DataManager.Instance.gm = this
|
||
|
this.stage = this.node.getChildByName("Stage")
|
||
|
this.ui = this.node.getChildByName("UI")
|
||
|
}
|
||
|
|
||
|
async start() {
|
||
|
this.clearGame()
|
||
|
await this.loadRes()
|
||
|
await this.initScene()
|
||
|
this.isInited = true
|
||
|
}
|
||
|
|
||
|
clearGame() {
|
||
|
this.stage.destroyAllChildren()
|
||
|
this.ui.destroyAllChildren()
|
||
|
}
|
||
|
|
||
|
async initScene() {
|
||
|
// await NetworkManager.Instance.connect()
|
||
|
// NetworkManager.Instance.sendMsg("我是客户端!!")
|
||
|
// const data = await NetworkManager.Instance.callApi("/join")
|
||
|
// console.log("data", data);
|
||
|
await Promise.all([
|
||
|
this.initJoyStick(),
|
||
|
this.initFire(),
|
||
|
this.initMap(),
|
||
|
])
|
||
|
|
||
|
}
|
||
|
|
||
|
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 initJoyStick() {
|
||
|
const joySitckPrefab = await ResourceManager.Instance.loadRes(PrefabPathEnum[EntityTypeEnum.JoyStick], Prefab)
|
||
|
const joySitck = instantiate(joySitckPrefab)
|
||
|
joySitck.setParent(this.ui)
|
||
|
const jm = DataManager.Instance.jm = joySitck.getComponent(JoyStickManager)
|
||
|
await jm.init()
|
||
|
}
|
||
|
|
||
|
async initFire() {
|
||
|
const firePrefab = await ResourceManager.Instance.loadRes(PrefabPathEnum[EntityTypeEnum.Shoot], Prefab)
|
||
|
const fire = instantiate(firePrefab)
|
||
|
fire.setParent(this.ui)
|
||
|
}
|
||
|
|
||
|
async initMap() {
|
||
|
const mapPrefab = await ResourceManager.Instance.loadRes(PrefabPathEnum[EntityTypeEnum.Map1], Prefab)
|
||
|
const map = instantiate(mapPrefab)
|
||
|
map.setParent(this.stage)
|
||
|
}
|
||
|
|
||
|
update(dt: number) {
|
||
|
if (!this.isInited) {
|
||
|
return
|
||
|
}
|
||
|
this.render()
|
||
|
this.tick(dt)
|
||
|
}
|
||
|
|
||
|
tick(dt: number) {
|
||
|
this.tickPlayer(dt)
|
||
|
|
||
|
DataManager.Instance.applyInput({
|
||
|
type: InputType.TimePast,
|
||
|
dt
|
||
|
})
|
||
|
}
|
||
|
|
||
|
tickPlayer(dt: number) {
|
||
|
const players = DataManager.Instance.state.players
|
||
|
|
||
|
for (const p of players) {
|
||
|
const playerManager = DataManager.Instance.playerMap.get(p.id)
|
||
|
if (!playerManager) {
|
||
|
return
|
||
|
}
|
||
|
playerManager.tick(dt)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
render() {
|
||
|
this.renderPlayer()
|
||
|
this.renderBullet()
|
||
|
}
|
||
|
|
||
|
renderPlayer() {
|
||
|
const players = DataManager.Instance.state.players
|
||
|
for (const p of 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() {
|
||
|
const bullets = DataManager.Instance.state.bullets
|
||
|
for (const b of bullets) {
|
||
|
let bulletManager = DataManager.Instance.bulletMap.get(b.id)
|
||
|
if (!bulletManager) {
|
||
|
const bullet = ObjectPoolManager.Instance.getPoolObject(b.type)
|
||
|
bulletManager = bullet.getComponent(BulletManager) || bullet.addComponent(BulletManager)
|
||
|
DataManager.Instance.bulletMap.set(b.id, bulletManager)
|
||
|
bulletManager.init(b)
|
||
|
} else {
|
||
|
bulletManager.render(b)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|