Files
18xchat/assets/Scripts/Sub/UI/PreloadUI.ts
T
2025-09-21 16:04:51 +08:00

274 lines
8.3 KiB
TypeScript

import {
_decorator,
AssetManager,
AudioClip,
BufferAsset,
Button,
Component,
ImageAsset,
instantiate,
JsonAsset,
Label,
Material,
Node,
Prefab,
ProgressBar,
Sprite,
SpriteFrame,
VideoPlayer,
} from "cc";
import { GButton } from "../../Main/Common/GButton";
import ResManager from "../../Main/Manager/ResManager";
import Resource from "../../Main/Config/Resource";
import { SDKManager } from "../../Main/Channel/SDKManager";
import HttpUnit from "../../Main/Common/HttpUnit";
import GlobalValue from "../../Main/Common/GlobalValue";
import Utils from "../../Main/Common/Utils";
import { InnerMsgCode } from "../../Main/Config/InnerMsgCode";
import { promptItem } from "../Item/promptItem";
import LanguageUtils, { LanguageType } from "../../Main/Common/LanguageUtils";
import { DataId, DataManager } from "../../chat18x/data/DataManager";
import { ConfigManager } from "../../chat18x/manager/ConfigManager";
import { AppConfig } from "db://assets/Scripts/chat18x/config/env/appConfig";
import DeviceIdService from "db://assets/Scripts/chat18x/foundation/identity/DeviceIdService";
import { AccountData } from "../../chat18x/data/AccountData";
import { EnvData } from "../../chat18x/data/EnvData";
import { GlobalData } from "../../chat18x/data/GlobalData";
import { AiCharacterData } from "../../chat18x/data/AiCharacterData";
import { LoginData } from "../../chat18x/data/LoginData";
import { PlayerData } from "../../chat18x/data/PlayerData";
import { WalletData } from "../../chat18x/data/WalletData";
import MachineInfoService from "../../chat18x/foundation/identity/MachineInfoService";
import { AuthService } from "../../chat18x/network/services/AuthService";
import { CommonService } from "../../chat18x/network/services/CommonService";
import { PlayerDataService } from "../../chat18x/network/services/PlayerDataService";
import proto from "db://assets/Scripts/proto/proto.pb.js";
import { ViewManager } from "../../Main/Manager/ViewManager";
import { MainScene } from "../MainScene";
import { prodConfig } from "../../chat18x/config/env/prod";
const { ccclass, property } = _decorator;
@ccclass("PreloadUI")
export class PreloadUI extends Component {
@property(MainScene)
mainScene: MainScene = null;
private _nodeTab: any = {};
private preloadFinish = false; //资源加载完成
private loginFinish = false; //登录完成
loginBtn: Node;
private jinduBar: Node = null;
private jinduFilled: Sprite = null;
private VideoPlayer: VideoPlayer;
private appVersion: Label;
start() {
//Utils.addInnerEL(InnerMsgCode.UI_ShowPrompt, this, this.resiveShowPrompt)
Utils.parseNode(this.node, this._nodeTab);
// 添加新的事件监听
this.appVersion = this._nodeTab.appVersion.getComponent(Label);
this.loginBtn = this._nodeTab.btnLogin;
this.loginBtn.active = false;
this.appVersion.string = `app version: ${prodConfig.appVersion}`;
this.VideoPlayer = this._nodeTab.VideoPlayer.getComponent(VideoPlayer);
this.VideoPlayer.node.on(
VideoPlayer.EventType.META_LOADED,
this.onVideoReadyToPlay,
this
);
GButton.BandClick(this.loginBtn, this.onLogin, this);
this.jinduBar = this.node.getChildByName("jinduBar");
this.jinduFilled = this.jinduBar
.getChildByName("jinduFilled")
.getComponent(Sprite);
//适配背景图
//let BG = this.node.getChildByName("BG");
//Utils.adjustBgPixelRatio(BG, 1);
this.jinduFilled.fillRange = 0;
if (!this.VideoPlayer.isPlaying) this.VideoPlayer.play();
// 初始化配置
ConfigManager.I.init();
//console.log(ConfigManager.tables.TbGlobalConfig.GameName);
// 初始化环境配置
AppConfig.I;
// 初始化数据层
DataManager.I.init();
// 设备号
DeviceIdService.I.init();
SDKManager.init();
this.preloadFiles();
}
protected onDestroy(): void {}
onVideoReadyToPlay() {
console.log("Video Ready");
this._nodeTab.VideoPlayer.getComponent(VideoPlayer).play();
}
update(deltaTime: number) {
if (!this.preloadFinish) {
return;
//this.goToLoginScene();
}
if (this.loginFinish) {
this.loginFinish = false;
this.preloadFinish = false;
this.enterGame();
}
}
//预加载文件
preloadFiles() {
let preloadTab = [
{ path: "ChatPanel", bundleName: "Chat18x", ftype: Prefab },
{ path: "GirlDetailPanel", bundleName: "Chat18x", ftype: Prefab },
{ path: "GirlListPanel", bundleName: "Chat18x", ftype: Prefab },
{ path: "tblanguage", bundleName: "DataTable", ftype: BufferAsset },
];
let num_prefab = 0; //已加载数量
let num_succeed = 0; //加载成功数量
let preloadFunc = () => {
if (num_prefab < preloadTab.length) {
let cfg = preloadTab[num_prefab];
ResManager.I.preLoadSubpackageFile(
cfg.path,
cfg.bundleName,
cfg.ftype,
(ret) => {
num_prefab++;
num_succeed++;
let percent = 0.45 * (num_prefab / preloadTab.length);
this.showLoadProgress(0.55 + percent);
console.log("加载成功:", cfg.path);
preloadFunc();
},
(err) => {
num_prefab++;
console.log("加载失败:", err);
}
);
} else {
if (num_succeed >= num_prefab) {
this.startloginProcess();
}
}
};
preloadFunc();
}
//加载配置表
startloginProcess() {
this.jinduBar.active = false;
this.preloadFinish = true;
this.loginBtn.active = true;
}
//显示加载进度
showLoadProgress(percent) {
this.jinduFilled.fillRange = percent;
}
// 跳转到登录场景
// private goToLoginScene() {
// this.jinduBar.active = false;
// ResManager.I.goCustomScene("LoginScene");
// }
async onLogin() {
const deviceId = DeviceIdService.I.id;
const os = MachineInfoService.I.getMachineOs();
const reqData = {
platType: "guest",
userId: deviceId,
device: deviceId,
os,
};
console.log("登录请求数据:", reqData);
let res = await AuthService.I.login(reqData);
console.log("登录响应数据:", res);
if (res && res.code === proto.cs.EnmRetCode.SUCCESS) {
const resData = res.data;
const loginData = DataManager.I.getDataById<LoginData>(DataId.Login);
loginData.token = resData.token;
loginData.refreshToken = resData.refreshToken;
loginData.expire = Number(resData.expire);
const playerData = DataManager.I.getDataById<PlayerData>(DataId.Player);
playerData.name = resData.name;
const accountData = DataManager.I.getDataById<AccountData>(
DataId.Account
);
accountData.accId = resData.accId;
const envData = DataManager.I.getDataById<EnvData>(DataId.Env);
envData.cdn = resData.cdn;
this.loginFinish = true;
this.reqMyInfo();
this.reqGlobalData();
this.reqAiCharacterData();
}
}
// 请求个人数据
private async reqMyInfo() {
const reqData = {};
let res = await PlayerDataService.I.reqMyInfo(reqData);
console.log("个人信息响应数据:", res);
if (res && res.code === proto.cs.EnmRetCode.SUCCESS) {
const resData = res.data;
const walletData = DataManager.I.getDataById<WalletData>(DataId.Wallet);
walletData.balance = Number(resData.balance);
walletData.vipExpire = Number(resData.vipExpire);
}
}
// 请求全局数据
private async reqGlobalData() {
const reqData = {
resName: "TbGlobalConfig",
};
let res = await CommonService.I.reqResConfig(reqData);
if (res && res.code === proto.cs.EnmRetCode.SUCCESS) {
// 保存数据
const globalData = DataManager.I.getDataById<GlobalData>(DataId.Global);
globalData.globals = res.data;
}
}
// 请求Ai角色配置
private async reqAiCharacterData() {
const reqData = {
resName: "TbAiCharacters",
};
let res = await CommonService.I.reqResConfig(reqData);
if (res && res.code === proto.cs.EnmRetCode.SUCCESS) {
// 保存数据
const aiCharacterData = DataManager.I.getDataById<AiCharacterData>(
DataId.AiCharacter
);
aiCharacterData.aiCharacter = res.data;
}
}
private enterGame() {
this.mainScene.init();
this.node.destroy();
//ResManager.I.goMainScene();
}
}