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-11-13 02:37:29 +08:00
|
|
|
import { 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-11-09 04:22:04 +08:00
|
|
|
|
2023-11-21 18:52:01 +08:00
|
|
|
// let APIPath = `http://localhost:8080`
|
|
|
|
// let WsPath = `ws://localhost:8080/websocket`
|
2023-11-22 17:46:08 +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.123:8080`
|
|
|
|
let WsPath = `ws://192.168.0.123: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-08 02:32:54 +08:00
|
|
|
public url():Promise<string> {
|
|
|
|
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-11-06 02:25:02 +08:00
|
|
|
roleSpine:{[id:number]:sp.SkeletonData} = {}; //角色Spine
|
|
|
|
roleResImage:{[id:number]:SpriteFrame} = {}; //角色战斗素材图片
|
|
|
|
roleResSpine:{[id:number]:sp.SkeletonData} = {}; //角色战斗素材Spine
|
|
|
|
effects:{[id:number]:sp.SkeletonData} = {}; //Spine特效
|
|
|
|
maps:{[id:number]:SpriteFrame[]} = {}; //地图
|
2023-10-30 02:34:11 +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-10-26 03:06:44 +08:00
|
|
|
//将配置表的角色都读取出来
|
2023-11-06 02:25:02 +08:00
|
|
|
for (const info of TD.TbGRole.getDataMap().values()) {
|
|
|
|
this.roleSpine[info.id] = await (new Promise(r => {
|
2023-10-30 02:34:11 +08:00
|
|
|
this.bundle.load(info.spine,sp.SkeletonData,(err,data) => {
|
|
|
|
r(data);
|
|
|
|
})
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2023-11-06 02:25:02 +08:00
|
|
|
//读取角色战斗素材
|
|
|
|
for(const info of TD.TbGRoleBattleRes.getDataMap().values()){
|
|
|
|
if(info.resType == "Spine"){
|
|
|
|
this.roleResSpine[info.id] = await (new Promise(r => {
|
|
|
|
this.bundle.load(info.resSrc,sp.SkeletonData,(err,data) => {
|
|
|
|
r(data);
|
|
|
|
})
|
|
|
|
}))
|
|
|
|
}else{
|
|
|
|
this.roleResImage[info.id] = await (new Promise(r => {
|
|
|
|
this.bundle.load(info.resSrc,SpriteFrame,(err,data) => {
|
|
|
|
r(data);
|
|
|
|
})
|
|
|
|
}))
|
|
|
|
}
|
2023-10-30 02:34:11 +08:00
|
|
|
}
|
2023-11-03 02:57:38 +08:00
|
|
|
|
|
|
|
//读取地图
|
2023-11-06 02:25:02 +08:00
|
|
|
for (const info of TD.TbGMap.getDataMap().values()) {
|
2023-11-03 02:57:38 +08:00
|
|
|
|
|
|
|
this.maps[info.id] = await (Promise.all(["1","2","3"].map(index => `map${index}`).map(key => {
|
|
|
|
return new Promise<SpriteFrame>(r => {
|
|
|
|
if(info[key]){
|
|
|
|
this.bundle.load(info[key],SpriteFrame,(err,data) => {
|
|
|
|
r(data);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})))
|
|
|
|
|
|
|
|
}
|
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-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包
|
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;
|
|
|
|
})
|