193 lines
		
	
	
		
			6.3 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			193 lines
		
	
	
		
			6.3 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
| 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';
 | |
| 
 | |
| const { ccclass } = _decorator;
 | |
| 
 | |
| @ccclass('BattleManager')
 | |
| export class BattleManager extends Component {
 | |
|     private stage: Node
 | |
|     private ui: Node
 | |
|     private shouldUpdate = false
 | |
| 
 | |
|     async start() {
 | |
|         //清空
 | |
|         this.clearGame()
 | |
| 
 | |
|         //资源加载和网络连接同步执行
 | |
|         await Promise.all([this.loadRes(), this.connectServer()])
 | |
| 
 | |
|         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)
 | |
|     }
 | |
| 
 | |
|     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()
 | |
|     }
 | |
| 
 | |
|     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()
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     leaveGame() {
 | |
|         this.clearGame()
 | |
|         director.loadScene(SceneEnum.Hall);
 | |
|     }
 | |
| 
 | |
|     async handleGameEnd() {
 | |
|         const { success, res, error } = await NetworkManager.Instance.callApi(ApiMsgEnum.ApiGameEnd, { rid: DataManager.Instance.roomInfo.id })
 | |
|         if (!success) {
 | |
|             console.log(error)
 | |
|             return;
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     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)
 | |
|     }
 | |
| 
 | |
|     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)
 | |
|         }
 | |
|     }
 | |
| 
 | |
| }
 | |
| 
 |