291 lines
9.3 KiB
TypeScript
Raw Normal View History

2023-10-26 03:06:44 +08:00
import { resources } from "cc";
2023-10-23 18:56:01 +08:00
import { BehaviorManager } from "../../extensions/Behavior Creator/runtime/main";
2023-10-26 03:06:44 +08:00
import SystemBase from "../../extensions/ngame/assets/ngame/system/SystemBase";
2023-10-23 18:56:01 +08:00
import JNSocket from "../../extensions/ngame/assets/ngame/network/JNSocket";
import { JNSyncAction } from "../../extensions/ngame/assets/ngame/sync/JNSyncAction";
import { JNSyncMessage } from "../../extensions/ngame/assets/ngame/sync/JNSyncMessage";
import JNSyncFrame, { JNFrameInfo, JNFrameInfos, JNFrameInputs } from "../../extensions/ngame/assets/ngame/sync/frame/JNSyncFrame";
import JNSyncFrameComponent from "../../extensions/ngame/assets/ngame/sync/frame/game/JNSyncFrameComponent";
import JNSyncFrameProtoComponent from "../../extensions/ngame/assets/ngame/sync/frame/game/JNSyncFrameProtoComponent";
import JNLayer, { JNLayerAnim, JNLayerInfo } from "../../extensions/ngame/assets/ngame/ui/JNLayer";
import JNLayerBase from "../../extensions/ngame/assets/ngame/ui/base/JNLayerBase";
import { EventDispatcher } from "../../extensions/ngame/assets/ngame/util/EventDispatcher";
2023-10-26 03:06:44 +08:00
import { JsonLoad, JsonUtil } from "../../extensions/ngame/assets/ngame/util/JsonUtil";
2023-10-23 18:56:01 +08:00
import NGameMessage from "../../extensions/ngame/assets/ngame/util/NGameMessage";
import GBattleModeManager from "./battle/GBattleModeManager";
2023-11-08 02:32:54 +08:00
import { GLayer, GUI, UIConfig } from "./ui/UIConfig";
2023-10-26 03:06:44 +08:00
import JLoaderSystem from "../../extensions/ngame/assets/ngame/system/JLoaderSystem";
import { sp } from "cc";
2023-10-27 02:38:08 +08:00
import { SpriteFrame } from "cc";
2023-11-05 03:26:09 +08:00
import Loading from "../../extensions/ngame/assets/ngame/util/Loading";
2023-12-06 19:08:16 +08:00
import { TB, Tables } from "../resources/config/data/schema";
2023-11-06 02:25:02 +08:00
import { JsonAsset } from "cc";
2023-11-14 18:52:25 +08:00
import { GAction } from "./consts/GAction";
2023-11-08 02:32:54 +08:00
import { StorageData, StorageEnum } from "./consts/GData";
2023-11-09 18:58:02 +08:00
import { JAPI, JAPIConfig } from "../../extensions/ngame/assets/ngame/util/JAPI";
2023-11-10 03:56:07 +08:00
import { AppData } from "./AppData";
2023-11-20 18:25:50 +08:00
import AppAction from "./AppAction";
2023-12-06 19:08:16 +08:00
import { Asset } from "cc";
import { Component } from "cc";
2023-11-09 04:22:04 +08:00
2023-12-03 03:31:19 +08:00
// let APIPath = `http://localhost:8080`
// let WsPath = `ws://localhost:8080/websocket`
2023-12-07 21:34:26 +08:00
// let APIPath = `http://192.168.1.23:8080`
// let WsPath = `ws://192.168.1.23:8080/websocket`
let APIPath = `http://192.168.0.128:8080`
let WsPath = `ws://192.168.0.128:8080/websocket`
2023-11-20 03:47:00 +08:00
// let APIPath = `https://api.pet.jisol.cn`
// let WsPath = `wss://api.pet.jisol.cn/websocket`
2023-10-23 18:56:01 +08:00
//重写UI
class JNGLayer extends JNLayer{
2023-11-08 02:32:54 +08:00
layers: string[] = [GLayer.View,GLayer.Popup,GLayer.Tips];
2023-10-23 18:56:01 +08:00
views: { [key: string]: JNLayerInfo; } = UIConfig;
}
//重写Socket
class JNGSocket extends JNSocket{
2023-11-07 18:56:52 +08:00
async onInit() {
this.on(GAction.TOKEN_EXPIRED,this.onTokenExpired.bind(this));
await super.onInit();
}
2023-11-28 19:20:11 +08:00
public async url():Promise<string> {
2023-11-08 02:32:54 +08:00
return new Promise<string>(resolve => {
//获取Token
let token = StorageData.get(StorageEnum.Token);
if(token){
2023-11-10 03:56:07 +08:00
resolve(`${WsPath}/${token}`)
2023-11-08 02:32:54 +08:00
return;
}
const loginResolve = (token:string) => {
2023-11-10 03:56:07 +08:00
resolve(`${WsPath}/${token}`);
2023-11-08 02:32:54 +08:00
}
//如果没有Token则弹出登入页面
app.layer.Open(GUI.Login,loginResolve);
});
2023-11-07 18:56:52 +08:00
}
//Token失效
onTokenExpired(){
console.log("onTokenExpired");
2023-11-08 02:32:54 +08:00
//清除Token
StorageData.delect(StorageEnum.Token);
2023-10-23 18:56:01 +08:00
}
}
// 重写帧同步
class JNGSyncFrame extends JNSyncFrame{
protected onResetValue(){
//重置状态机
BehaviorManager.deleteInstance();
super.onResetValue();
}
async onServerData(start?:number,end?:number): Promise<JNFrameInfos> {
return app.proto
.getType(JNSyncMessage.JNFrameInfos)
.decode(
new Uint8Array(
(await app.api.get("/sync/frame",{
responseType:'arraybuffer',
params:{
start,
end
}
})).data
)
) as any;
}
onSendInput(message: JNFrameInputs) {
app.socket.Send(JNSyncAction.NSyncFrameInput,message,JNSyncMessage.JNFrameInputs);
}
}
2023-11-06 02:25:02 +08:00
export var TD:Tables = null;
2023-10-26 03:06:44 +08:00
//读写config
export class JNGConfig extends SystemBase{
2023-11-05 03:26:09 +08:00
static loading = "JNGConfig";
2023-10-26 03:06:44 +08:00
async onInit(): Promise<any> {
2023-11-05 03:26:09 +08:00
app.loading.setCurrent(JNGConfig.loading);
2023-11-06 02:25:02 +08:00
let json2 = resources.getDirWithPath("config/json/",JsonAsset).map((info) => {
let args = info.path.split("/");
return {
name:args[args.length - 1],
bundle:"resources",
path:"config/json/"
}
})
for (const key in json2) {
const load = json2[key];
2023-10-26 03:06:44 +08:00
await JsonUtil.load(load);
}
2023-11-05 03:26:09 +08:00
2023-11-06 02:25:02 +08:00
TD = new Tables((file) => JsonUtil.get(file))
console.log(TD);
2023-11-05 03:26:09 +08:00
app.loading.ok(JNGConfig.loading);
2023-10-26 03:06:44 +08:00
}
}
2023-11-05 03:26:09 +08:00
//资源初始器
export class JLoaderBattle extends JLoaderSystem{
static loading = "JLoaderBattle";
static loadingInit = "JLoaderBattle_Init";
2023-10-26 03:06:44 +08:00
2023-12-06 19:08:16 +08:00
//资源
resources:{[id:number]:Asset} = {};
2023-12-07 21:34:26 +08:00
battleResources:number[] = []; //战斗资源Id
2023-12-06 19:08:16 +08:00
2023-10-26 03:06:44 +08:00
async onInit(): Promise<any> {
2023-11-05 03:26:09 +08:00
app.loading.setCurrent(JLoaderBattle.loading);
2023-10-26 03:06:44 +08:00
await super.onInit();
2023-10-30 02:34:11 +08:00
2023-11-05 03:26:09 +08:00
app.loading.ok(JLoaderBattle.loading);
app.loading.setCurrent(JLoaderBattle.loadingInit);
//默认加载全部资源
2023-12-07 21:34:26 +08:00
await this.loadBattleResources(...TD.TbBattleResource.getDataList());
2023-11-05 03:26:09 +08:00
app.loading.ok(JLoaderBattle.loadingInit);
2023-10-27 02:38:08 +08:00
2023-10-26 03:06:44 +08:00
}
2023-12-07 21:34:26 +08:00
//加载战斗资源
async loadBattleResources(...ress:TB.TbBattleResource[]){
2023-12-06 19:08:16 +08:00
for (const res of ress) {
2023-12-07 21:34:26 +08:00
await this.loadBattleResource(res);
2023-12-06 19:08:16 +08:00
}
}
2023-12-07 21:34:26 +08:00
//加载战斗资源
loadBattleResource(res:TB.TbBattleResource){
2023-12-06 19:08:16 +08:00
return (new Promise<void>(r => {
let Type:new ()=>Asset = [sp.SkeletonData,SpriteFrame][res.type];
this.bundle.load(res.path,Type,(error,data) => {
2023-12-07 21:34:26 +08:00
if(this.battleResources.indexOf(res.id) < 0){
2023-12-06 19:08:16 +08:00
//添加永久资源
this.resources[res.id] = data;
data.addRef();
}
r();
})
}))
}
//加载组件资源
async loadComponentResource<T extends Asset>(res:TB.TbBattleResource,comp:Component):Promise<T>{
return (new Promise<T>(r => {
this.bundle.load(res.path,(error,data) => {
if(!this.resources[res.id])
this.resources[res.id] = data;
//添加资源次数
data.addRef();
//添加销毁资源引用
let onDestroy = comp["onDestroy"];
comp["onDestroy"] = () => {
if(onDestroy)
onDestroy.bind(comp)();
data.decRef();
this.onUpdateResources();
}
r(data as T);
})
}))
}
2023-12-07 21:34:26 +08:00
//销毁长久资源
clearBattleResources(...ress:TB.TbBattleResource[]){
this.battleResources.forEach(id => {
2023-12-06 19:08:16 +08:00
if(this.resources[id]){
this.resources[id].decRef();
}
});
2023-12-07 21:34:26 +08:00
this.battleResources = [];
2023-12-06 19:08:16 +08:00
this.onUpdateResources();
}
//更新资源(过滤没有用的资源)
onUpdateResources(){
for (const key in this.resources) {
if (Object.prototype.hasOwnProperty.call(this.resources, key)) {
const data = this.resources[key];
if(!data.isValid) this.resources[key] = null;
}
}
}
//获取资源
getData<T extends Asset>(resId:number | string):T{
2023-12-06 19:08:16 +08:00
let res = this.resources[resId]
if(!res) console.info(`[JLoaderBattle] 未加载资源${resId}`);
return res as T;
}
//获取角色Spine
getRoleSpine(roleId:number){
return this.getData<sp.SkeletonData>(TD.TbGRole.get(roleId).spine)
}
//获取地图
getMap(mapId:number):SpriteFrame[]{
let info = TD.TbGMap.get(mapId)
return ["1","2","3"].map(key => {
return this.getData<SpriteFrame>(info[`map${key}`]);
})
}
2023-10-26 03:06:44 +08:00
}
2023-10-23 18:56:01 +08:00
export const app = {
layer : new JNGLayer(), //UI
socket : new JNGSocket(), //Socket
sync : new JNGSyncFrame(), //同步
event : EventDispatcher.getIns(), //通知
proto : NGameMessage.getIns(), //消息
2023-11-09 18:58:02 +08:00
api : JAPI.create({
2023-11-10 03:56:07 +08:00
baseURL: `${APIPath}`,
2023-11-09 18:58:02 +08:00
}), //请求
2023-11-09 04:22:04 +08:00
// api : {}, //请求
2023-10-23 18:56:01 +08:00
battle : GBattleModeManager.getIns(), //战斗
2023-11-05 03:26:09 +08:00
config : new JNGConfig(), //配置文件
battleRes : new JLoaderBattle("battle"), //battle包
tbRes : new JLoaderSystem("tbresource"), //tbresource包
2023-11-10 03:56:07 +08:00
data : new AppData(), //游戏基础信息
2023-11-20 18:25:50 +08:00
action : new AppAction(), //游戏行为
loading : new Loading({
2023-11-05 03:26:09 +08:00
[JNGConfig.loading]:{title:"加载配置文件"},
[JLoaderBattle.loading]:{title:"加载战斗资源"},
[JLoaderBattle.loadingInit]:{title:"初始化战斗资源"},
2023-11-10 03:56:07 +08:00
[AppData.loading]:{title:"初始化信息"},
2023-11-20 18:25:50 +08:00
})
2023-11-09 04:22:04 +08:00
}
2023-11-10 03:56:07 +08:00
app.api.addRequestInterceptors((config:JAPIConfig) => {
2023-11-22 17:46:08 +08:00
//设置Token
if(StorageData.get(StorageEnum.Token))
config.headers["Token"] = StorageData.get(StorageEnum.Token);
2023-11-09 18:58:02 +08:00
return true;
})