demo/assets/Script/HelloWorld.ts

135 lines
4.3 KiB
TypeScript
Raw Permalink Normal View History

2024-03-22 00:57:47 -07:00
import GameUtil from "./Common/GameUtil";
import SocketIoClient from "./Common/SocketIoClient";
import Configs from "./Common/Configs";
import { LanguageManager } from "./Common/LanguageManager";
const {ccclass, property} = cc._decorator;
@ccclass
export default class HelloWorld extends cc.Component {
@property(cc.ProgressBar)
progressBar : cc.ProgressBar = null;
@property(cc.Node)
btnStartGame: cc.Node = null;
// use this for initialization
async start() {
cc.log("HelloWorld onLoad");
//TODO
this.progressBar.node.active = true;
this.connectServer();
}
/**
* name
*/
private startGame() {
cc.director.loadScene('GamePlay');
}
protected getAuthLogin(url_string:string){
let dataDecode = GameUtil.parseUrlData(url_string);
if(!dataDecode){
return ;
}
return dataDecode;
}
private async connectServer() {
let auth = this.getAuthLogin(window.location.href);
Configs.LanguageConfig.language = auth.language?auth.language:"en";
Configs.LanguageConfig.subpath = auth.subpath;
Configs.LanguageConfig.version = auth.version?auth.version:"0.01";
Configs.LanguageConfig.server = auth.server;
await new LanguageManager().getLanguage(auth);
if(!auth || !auth.server){
console.log("Server not found!");
return;
}
SocketIoClient.getInstance().connectServer(auth.server,{auth:auth, path:auth.subpath});
this.initEventNetwork();
if(auth){
Configs.AppConfig.Username = auth.username;
}
}
protected initEventNetwork() {
console.log("Game info command:");
SocketIoClient.getInstance().on(Configs.SOCKET_EVENT.GAME_INFO, this.onGameInfo.bind(this));
SocketIoClient.getInstance().on(Configs.SOCKET_EVENT.CONNECTION, this.onConnection.bind(this));
}
protected removeListener() {
SocketIoClient.getInstance().off(Configs.SOCKET_EVENT.GAME_INFO, this.onGameInfo.bind(this));
SocketIoClient.getInstance().off(Configs.SOCKET_EVENT.CONNECTION, this.onConnection.bind(this));
}
protected onConnection() {
}
protected onConnect() {
// NotifyUtil.getInstance().emit(BNotifyType.HIDE_MESSAGE);
}
protected ondisconnect(reason: string) {
SocketIoClient.getInstance().close();
this.removeListener();
// NotifyUtil.getInstance().emit(BNotifyType.STOP_AUTO);
// NotifyUtil.getInstance().emit(BNotifyType.SHOW_DISCONNECT);
}
protected onConnectError(data: string) {
SocketIoClient.getInstance().close();
this.removeListener();
// NotifyUtil.getInstance().emit(BNotifyType.SHOW_DISCONNECT);
}
protected onLogin(data) {
if (data.status == 1) {
let result = data.result;
Configs.AppConfig.Coin = result.coin;
// NotifyUtil.getInstance().emit(BNotifyType.CHANGE_MONEY, this.myMoney);
} else {
let dataMsg = {
// title: GameUtil.capitalizeFirstLetter(LanguageManager.getText('title_message')),
msg: data.msg,
actionOk: (() => {
this.connectServer();
}).bind(this)
}
// NotifyUtil.getInstance().emit(BNotifyType.SHOW_MESSAGE, dataMsg);
}
}
protected onGameInfo(data) {
Configs.AppConfig.Coin = data.balance;
Configs.GameRule = data.settings;
cc.assetManager.loadBundle('Resources', (err, bundle) => {
cc.assetManager.loadBundle('Scene', (err, bundle) => {
cc.director.preloadScene('GamePlay', ((completedCount: number, totalCount: number, item: any) => {
let percent = completedCount / totalCount;
this.progressBar.progress = percent;
}).bind(this), (error: Error) => {
if (!error) {
this.btnStartGame.active = true;
this.progressBar.node.active = false;
}
})
});
});
}
protected onUpdateBalance(data) {
}
protected onUpdateCoin(data) {
}
}