JisolGame/JisolGameCocos/assets/script/battle/GBattleModeManager.ts

238 lines
6.1 KiB
TypeScript
Raw Normal View History

2023-11-15 02:32:00 +08:00
import { Prefab } from "cc";
2023-10-23 18:56:01 +08:00
import Singleton from "../../../extensions/ngame/assets/ngame/util/Singleton";
2023-11-15 02:32:00 +08:00
import { Camera } from "cc";
import GBaseMode from "./GBaseMode";
import { Node } from "cc";
import { instantiate } from "cc";
2023-12-07 21:34:26 +08:00
import { TD, app } from "../App";
2023-11-15 02:32:00 +08:00
import { JNFrameInfo, JNSyncFrameEvent } from "../../../extensions/ngame/assets/ngame/sync/frame/JNSyncFrame";
2023-11-15 18:38:00 +08:00
import { CCObject } from "cc";
2023-11-22 03:51:37 +08:00
import { Env, EnvCurrent } from "../Env";
2023-12-07 21:34:26 +08:00
import { TB } from "../../resources/config/data/schema";
import BattleResource from "../tools/BattleResource";
2023-10-23 18:56:01 +08:00
export enum BattleMode{
2023-11-15 02:32:00 +08:00
//无尽模式
OnHook = 0,
2023-10-23 18:56:01 +08:00
//PVP 模式
2023-11-15 02:32:00 +08:00
PVP = 1,
2023-12-10 00:58:43 +08:00
//阵营守护
CampGuardian = 2,
2023-11-15 02:32:00 +08:00
}
export interface GBattleModeInfo{
modes:Prefab[], //模式预制体
camera:Camera, //场景相机
root:Node, //世界场景Root
2023-10-23 18:56:01 +08:00
}
2023-11-22 17:46:08 +08:00
//事件
export enum GBattleModeEvent{
//关闭模式
Close = "GBattleModeEvent_Close",
2023-12-07 21:34:26 +08:00
StartLoadingResource = "GBattleModeEvent_StartLoadingResource",
EndLoadingResource = "GBattleModeEvent_EndLoadingResource",
2023-11-22 17:46:08 +08:00
}
2023-10-23 18:56:01 +08:00
//全局战斗模式管理器
export default class GBattleModeManager extends Singleton {
2023-11-15 02:32:00 +08:00
//模式预制体
modes:Prefab[] = [];
//场景相机
camera:Camera;
//世界场景Root
root:Node;
//是否初始化
isInit:boolean = false;
//当前模式
current:BattleMode = null;
2023-11-22 03:51:37 +08:00
//是否自动推帧
2023-11-15 02:32:00 +08:00
isAuto:boolean = false;
2023-11-22 03:51:37 +08:00
//是否推追帧
isAutoMaxFrame:boolean = false;
2023-11-15 02:32:00 +08:00
//自动推帧间隔
autoTime:number = 0;
2023-11-20 18:55:49 +08:00
//默认模式
default:BattleMode = BattleMode.OnHook; //默认无限模式
2023-11-21 01:57:40 +08:00
//当前帧不切换模式
frameNoSwitch:boolean = false;
//模式数据
data:any;
2023-12-07 21:34:26 +08:00
//是否加载资源
isLoadingResource:boolean = false;
//加载index
loadingIndex:number = 0;
2023-11-15 02:32:00 +08:00
//初始化管理器
async onInit(info:GBattleModeInfo){
this.modes = info.modes || [];
this.camera = info.camera;
this.root = info.root;
2023-12-10 00:58:43 +08:00
this.camera.enabled = false;
2023-11-15 02:32:00 +08:00
//监听帧同步 世界创建逻辑
app.event.on(JNSyncFrameEvent.CLEAR,this.clear,this);
app.event.on(JNSyncFrameEvent.CREATE,this.create,this);
this.isInit = true;
}
//打开指定模式
2023-12-07 21:34:26 +08:00
// isAuto 是否自动推帧
// data 模式数据
// res 资源加载列表 (因为是帧同步所以打开模式前必须提前加载可能使用的资源)
2023-11-21 01:57:40 +08:00
async Open(mode:BattleMode = null,isAuto:boolean = false,data:any = this.data){
2023-12-07 21:34:26 +08:00
this.Close();
2023-11-21 01:57:40 +08:00
this.data = data;
2023-11-20 18:55:49 +08:00
if(!this.current && mode == null){
2023-11-22 03:51:37 +08:00
//裁决员不允许默认模式
if(EnvCurrent == Env.Server) return;
2023-11-21 01:57:40 +08:00
await this.Open(this.default,true,data);
2023-11-20 18:55:49 +08:00
return;
}else if(mode == null){
return;
}
2023-11-15 02:32:00 +08:00
this.current = mode;
this.setAuto(isAuto);
2023-12-07 21:34:26 +08:00
//加载资源
let loadingIndex = (this.loadingIndex+=1);
this.isLoadingResource = true;
app.event.emit(GBattleModeEvent.StartLoadingResource);
console.log("[GBattleModeManager] 加载资源");
await BattleResource.loadResource(mode,data);
console.log("[GBattleModeManager] 加载结束",loadingIndex,this.loadingIndex);
if(this.loadingIndex == loadingIndex){
this.isLoadingResource = false;
app.event.emit(GBattleModeEvent.EndLoadingResource);
}else{
//如果加载中途切换了模式则直接返回
return;
}
//资源加载完成则显示世界
2023-11-15 02:32:00 +08:00
app.sync.onReset();
app.sync.onStart();
}
2023-12-05 01:43:43 +08:00
//重置当前模式
Reset(){
app.sync.onReset();
app.sync.onStart();
}
2023-11-15 02:32:00 +08:00
//关闭当前模式
2023-11-22 17:46:08 +08:00
async Close(data?:any){
2023-11-15 02:32:00 +08:00
//主动调用场景销毁
app.sync.onReset();
2023-11-22 17:46:08 +08:00
let current = this.current;
2023-11-15 02:32:00 +08:00
this.current = null;
2023-11-21 01:57:40 +08:00
this.frameNoSwitch = true;
2023-11-20 18:55:49 +08:00
2023-11-22 17:46:08 +08:00
//结束通知
app.event.emit(GBattleModeEvent.Close,current,data);
2023-11-15 02:32:00 +08:00
}
//设置自动推帧 ( 帧不由addFrame控制 管理器自动推帧)
2023-11-22 03:51:37 +08:00
setAuto(is:boolean,isAutoMaxFrame:boolean = false){
2023-11-15 02:32:00 +08:00
this.isAuto = is;
2023-11-22 03:51:37 +08:00
this.isAutoMaxFrame = isAutoMaxFrame;
2023-11-15 02:32:00 +08:00
this.autoTime = 0;
}
//清除当前模式
private clear(){
if(!this.isInit) return;
this.root.destroyAllChildren();
2023-12-10 00:58:43 +08:00
//关闭相机
this.camera.enabled = false;
2023-11-15 02:32:00 +08:00
}
//创建当前模式
private create(){
2023-11-15 18:38:00 +08:00
2023-11-15 02:32:00 +08:00
if(!this.isInit || this.current == null) return;
let mode = instantiate(this.modes[this.current]);
2023-12-10 00:58:43 +08:00
//关闭相机
this.camera.enabled = false;
2023-11-15 02:32:00 +08:00
mode.getComponent(GBaseMode).camera = this.camera;
2023-11-21 01:57:40 +08:00
mode.getComponent(GBaseMode).data = this.data;
2023-11-15 02:32:00 +08:00
this.root.addChild(mode)
}
//向场景推帧
addFrame(info:JNFrameInfo){
if(this.isAuto) return; //如果是自动推帧则返回
app.sync.addFrame(info,true);
}
//管理器更新
onUpdate(dt:number){
//更新帧同步
app.sync.update(dt);
//自动推帧
this.onAutoFrame(dt);
2023-11-21 01:57:40 +08:00
//如果当前模式是空则默认模式
if(this.current == null && !this.frameNoSwitch){
this.Open();
}
this.frameNoSwitch = false;
2023-11-15 02:32:00 +08:00
}
2023-10-23 18:56:01 +08:00
2023-11-15 02:32:00 +08:00
//自动推帧
private onAutoFrame(dt:number){
if(!this.isAuto) return;
2023-11-22 03:51:37 +08:00
if(this.isAutoMaxFrame){
//保持超高频率追帧
while(app.sync.nFrameQueue.length < (app.sync.nMaxFrameLoopBan * 2)){
//速度推帧
app.sync.addFrame({
index:app.sync.nLocalFrame + 1
});
}
}else{
//正常追帧
this.autoTime += dt * 1000;
//获取当前帧同步的帧数推空帧
if(app.sync.nSyncTime < this.autoTime){
//如果事件够则推帧
this.autoTime -= app.sync.nSyncTime;
app.sync.addFrame({
index:app.sync.nLocalFrame + 1
});
}
2023-11-15 02:32:00 +08:00
}
}
2023-10-23 18:56:01 +08:00
}