squidGame/tgx-games-client/assets/module_arean/scripts/AreanLobbyScene.ts

478 lines
15 KiB
TypeScript

import {_decorator, Component, director, instantiate, JsonAsset, Label, Node, Prefab, tween, v3, SpriteFrame, Sprite, PageView, Input, EventTouch} from 'cc';
import {UserMgr} from '../../module_basic/scripts/UserMgr';
import {tgxAudioMgr, tgxModuleContext, tgxSceneUtil, tgxUIAlert, tgxUIMgr, tgxUIWaiting} from '../../core_tgx/tgx';
import {SceneDef} from '../../scripts/SceneDef';
import {SceneUtil} from '../../core_tgx/base/SceneUtils';
import {UI_SearchingRival} from 'db://assets/module_arean/ui_searching_rival/UI_SearchingRival';
import {EventName} from '../../core_tgx/easy_ui_framework/EventName';
import {ModuleDef} from '../../scripts/ModuleDef';
import {resLoader} from '../../core_tgx/base/utils/ResLoader';
import {areanMgr} from './AreanManager';
import {AreanEvent, AreanNetMgr} from './AreanNetMgr';
import {UISetRoom} from "db://assets/module_arean/ui_set_room/UISetRoom";
import {EFightModel} from "db://assets/module_basic/shared/protocols/public/arean/AreanTypeDef";
import { SubGameMgr } from '../../module_basic/scripts/SubGameMgr';
import {UserLocalCache} from "db://assets/module_basic/scripts/UserLocalCache";
import {UserHead} from "db://assets/module_arean/scripts/UserHead";
import {EMusicDefine} from "db://assets/module_basic/Define/MusicDefine";
import {UIBag} from "db://assets/module_arean/ui_bag/UIBag";
import {IDanCfg} from "db://assets/module_basic/shared/configs/interface/IDanCfg";
import {CommonFun} from "db://assets/module_arean/scripts/CommonFun";
import {UIMail} from "db://assets/module_arean/ui_mail/UIMail";
import {UIMoreModel} from "db://assets/module_arean/ui_more_model/UIMoreModel";
import { UIShopScreen } from '../shop/ui_shop/UIShopScreen';
const { ccclass, property } = _decorator;
@ccclass('AreanLobbyScene')
export class AreanLobbyScene extends Component {
private static isLoaded: boolean = true;
@property({ type: Node, displayName: ' loading选择' })
public loadingNode: Node | null = null;
@property({ type: Node })
public heroSpineNode: Node;
@property({ type: Label })
public fightModel: Label;
@property({ type: Label })
public fightModel2: Label;
@property(Node)
headNode: Node;
// 动画节点
@property(Node)
actionNode: Node;
//
@property(Node)
arrowBtn: Node;
//
@property(Node)
chooseHeroBtn: Node;
// 段位信息节点
@property(Node)
danInfoNode: Node;
// banner节点
@property(Node)
bannerPageView: Node;
// banner cell
@property(Node)
bannerCell: Node;
private _isAction : boolean = false;
private _pageCount : number = 5;
private _canClick : boolean = true;
protected onLoad(): void {
this.initEventListener();
tgxModuleContext.setDefaultModule(ModuleDef.Arean);
tgxUIMgr.inst.closeAll();
}
async start() {
if (AreanLobbyScene.isLoaded) {
AreanNetMgr.inst.initNetMsgHandlers();
await AreanNetMgr.inst.sendMsg_GetUserData();
this.loadingNode.active = true;
await this.loadData();
} else {
this.loadingNode.active = false;
this._updateHeadInfo();
}
this.onCompleteCallback().then();
this._updateFightModelName();
//this._initBannerPageView();
}
async loadData(){
tgxUIWaiting.show('数据加载', true);
AreanLobbyScene.isLoaded = false;
if (!UserMgr.inst.userId) {
tgxSceneUtil.loadScene(SceneDef.START);
return;
}
// await this.loadLanguage();
await this.loadGameRes();
await this.loadDatatables();
}
private initEventListener() {
director.on(AreanEvent.InBattleView, this.onStartGame, this);
}
/**
* 开始游戏
*/
private onStartGame(data:any) {
try {
let ui = tgxUIMgr.inst.getUI<UI_SearchingRival>(UI_SearchingRival);
let params = ui== null? UserMgr.inst.reTrayGameParmas:ui.params;
SubGameMgr.inst.enterSubGame(params,UserMgr.inst.uid,true);
} catch (error) {
console.log('开始游戏报错 error', error);
}
}
// 多人匹配,自定义按钮动画
onBtnShowAction() : void{
tgxAudioMgr.inst.playCommonBtn(EMusicDefine.EFFECT_CLICK);
this._updateArrow();
}
private _updateArrow() : void{
let angle = this.arrowBtn.getChildByName("arrow").angle;
// console.log("当前的角度 === ",angle);
if(angle == 180){
this.arrowBtn.getChildByName("arrow").angle = 0;
this.showAction(true);
}else{
this.arrowBtn.getChildByName("arrow").angle = 180;
this.showAction(false);
}
}
private showAction(open : boolean) : void{
if(this._isAction) return;
this._isAction = true;
let offsetY : number = open ? 280 : -280;
let time : number = 0.1;
if(offsetY > 0){
tween(this.actionNode)
.by(time,{position : v3(0,offsetY,0)},{easing: 'backOut'})
.call(()=>{
this._isAction = false;
})
.start()
}else{
tween(this.actionNode)
.by(time,{position : v3(0,offsetY,0)},{easing: 'backIn'})
.call(()=>{
this._isAction = false;
})
.start()
}
}
/** 是否可以点击 */
private setCanClick(flag : boolean) : void{
this._canClick = flag;
}
onBtnLobby() {
tgxAudioMgr.inst.playCommonBtn(EMusicDefine.EFFECT_CLICK);
SceneUtil.loadScene(SceneDef.LOBBY);
}
onBtnQuickPlayClicked() {
if(!this._canClick) return;
this._canClick = false;
tgxAudioMgr.inst.playCommonBtn(EMusicDefine.EFFECT_CLICK);
this._updateFightModel();
// if(UserLocalCache.inst.fightModel === EFightModel.Match){
// tgxUIAlert.show("研发团队加班中,敬请期待!");
// }else{
tgxUIWaiting.hide();
tgxUIMgr.inst.showUI(UI_SearchingRival, (ui: UI_SearchingRival) => {
this.scheduleOnce(() => {
ui.startMatch('arean',this.loadGame);
this.setCanClick(true);
}, 1.5);
});
// }
// director.loadScene(SceneDef.Arean.name);
}
private async loadGame(params?: any) {
await AreanNetMgr.inst.enterRoom(params,UserMgr.inst.uid);
}
onBtnCreate() {
tgxAudioMgr.inst.playCommonBtn(EMusicDefine.EFFECT_CLICK);
// tgxAudioMgr.inst.playCommonBtn();
// tgxUIMgr.inst.showUI(UICreateRoom);
}
onBtnJoinRoom() {
if(!this._canClick) return;
this._canClick = false;
UserLocalCache.inst.setFightModel(EFightModel.Room);
tgxAudioMgr.inst.playCommonBtn(EMusicDefine.EFFECT_CLICK);
tgxUIMgr.inst.showUI(UISetRoom,()=>{
this.setCanClick(true);
})
}
private _updateFightModel() : void{
let curModel = UserLocalCache.inst.fightModel;
if(curModel === EFightModel.Room){
let str = this.fightModel2.string;
let nextModel = str = EFightModel.Match ? EFightModel.Match : EFightModel.Ranking;
UserLocalCache.inst.setFightModel(nextModel);
}
}
// 刷新战斗模式
async onBtnRefreshFightModel(): Promise<void> {
tgxAudioMgr.inst.playCommonBtn(EMusicDefine.EFFECT_CLICK);
let str = this.fightModel2.string;
let nextModel = str = EFightModel.Match ? EFightModel.Ranking : EFightModel.Match;
UserLocalCache.inst.setFightModel(nextModel);
this._updateFightModelName();
}
// 刷新战斗模式名字
private _updateFightModelName() : void{
let curModel = UserLocalCache.inst.fightModel;
this.fightModel.string = curModel === EFightModel.Match ? "匹配" : "排位";
this.fightModel2.string = curModel === EFightModel.Match ? EFightModel.Match : EFightModel.Ranking;
}
update(deltaTime: number) {
}
private loadLanguage() {
this.modelName = "多语言模块加载";
return new Promise((resolve, reject) =>
{
resLoader.load(ModuleDef.Arean,'res/Config/Language',(err:Error,data:JsonAsset)=>{
if(err){
console.log('语言加载失败',err);
reject();
return;
}
resolve(data);
})
});
}
private async loadGameRes() {
this.modelName = "资源模块加载";
try {
return new Promise((resolve, reject) => {
resLoader.loadDir(ModuleDef.Arean, "res/", this.onProgressCallback.bind(this), resolve);
})
} catch (error) {
}
}
private progress: number = 0;
private modelName: string = '';
protected data: any = {
finished: 0,
total: 0,
progress: ""
};
private async loadDatatables() {
this.modelName = "数据模块加载";
return new Promise((resolve, reject) => {
areanMgr.cfgMgr.LoadAll(this.onProgressCallback.bind(this),resolve);
})
}
private onProgressCallback(finished: number, total: number, item: any) {
this.data.finished = finished;
this.data.total = total;
var progress = finished / total;
if (progress > this.progress) {
this.progress = progress;
this.data.progress = (progress * 100).toFixed(2);
}
director.emit(EventName.load_res, progress, this.modelName);
}
async onCompleteCallback() {
tgxUIWaiting.hide();
this.loadingNode.active = false;
console.log('数据加载完----------');
this._updateHeadInfo();
let isInRoom= UserMgr.inst.isInRoom();
if(isInRoom){
let ret2 = await UserMgr.inst.doTryEnterRoom(UserMgr.inst.roomId);
if (!ret2.isSucc) {
//进入大厅
let ret = await AreanNetMgr.inst.rpc_LeaveRoom();
if (ret.isSucc) {
console.log("退出房间成功");
}
}
}
}
// 刷新头像节点段位
private _updateHeadInfo() : void{
this.headNode.getComponent(UserHead).updateDanImg();
}
/** 排行榜 */
public onBtnRankClicked(){
tgxAudioMgr.inst.playCommonBtn(EMusicDefine.EFFECT_INVALID);
tgxUIAlert.show("研发团队加班中,敬请期待!");
}
/** 商店 */
public onBtnShopClicked(){
tgxAudioMgr.inst.playCommonBtn(EMusicDefine.EFFECT_INVALID);
// tgxUIAlert.show("研发团队加班中,敬请期待!");
tgxUIMgr.inst.showUI(UIShopScreen);
}
/** 活动 */
public onBtnActivityClicked(){
tgxAudioMgr.inst.playCommonBtn(EMusicDefine.EFFECT_INVALID);
tgxUIAlert.show("研发团队加班中,敬请期待!");
}
/** 神话降临 */
public onBtnActivity2Clicked(){
tgxAudioMgr.inst.playCommonBtn(EMusicDefine.EFFECT_INVALID);
tgxUIAlert.show("研发团队加班中,敬请期待!");
}
/** 背包事件 */
onBtnBagClicked(){
if(!this._canClick) return;
this._canClick = false;
tgxAudioMgr.inst.playCommonBtn(EMusicDefine.EFFECT_OPEN);
tgxUIMgr.inst.showUI(UIBag,()=>{
this.setCanClick(true);
});
}
/** 备战 */
onBtnWarClicked(){
tgxAudioMgr.inst.playCommonBtn(EMusicDefine.EFFECT_INVALID);
tgxUIAlert.show("研发团队加班中,敬请期待!");
}
/** 图鉴 */
onBtnIllustrationClicked(){
tgxAudioMgr.inst.playCommonBtn(EMusicDefine.EFFECT_INVALID);
tgxUIAlert.show("研发团队加班中,敬请期待!");
}
/** 好友 */
onBtnFriendClicked(){
tgxAudioMgr.inst.playCommonBtn(EMusicDefine.EFFECT_INVALID);
tgxUIAlert.show("研发团队加班中,敬请期待!");
}
/** 更多模式 */
onBtnMoreModelClicked(){
if(!this._canClick) return;
this._canClick = false;
tgxAudioMgr.inst.playCommonBtn(EMusicDefine.EFFECT_OPEN);
tgxUIMgr.inst.showUI(UIMoreModel,()=>{
this.setCanClick(true);
});
}
/** 邮件 */
onBtnMailClicked(){
tgxAudioMgr.inst.playCommonBtn(EMusicDefine.EFFECT_OPEN);
tgxUIMgr.inst.showUI(UIMail)
}
/** 获取页面通过索引 */
getPageByIndex(index: number): Node {
const pages = this.bannerPageView.getComponent(PageView).getPages();
return pages[index];
}
/** banner广告位 */
private _initBannerPageView() : void{
this.bannerPageView.on('page-turning', this._callback, this);
let len = this._pageCount;
let pageView = this.bannerPageView.getComponent(PageView);
pageView.removeAllPages();
for(let i = 0; i < len; i++){
let node = instantiate(this.bannerCell);
let attr = {
tag : i
}
node.attr(attr);
node.active = true;
node.getChildByName("lab").getComponent(Label).string = `${i + 1}`;
pageView.addPage(node);
node.on(Input.EventType.TOUCH_END,(e : EventTouch)=>{
tgxAudioMgr.inst.playCommonBtn(EMusicDefine.EFFECT_CLICK)
let tag = e.target.tag;
this._openBannerLayer(tag);
},this)
}
// 是否循环
let flag = true;
if (flag) { //如果循环需要在最前边和最后边在放一页
let firstNode = instantiate(this.getPageByIndex(0));
let lastNode = instantiate(this.getPageByIndex(this._pageCount - 1));
pageView.insertPage(lastNode, 0);
pageView.addPage(firstNode);
console.log("this.pages:", pageView.getPages());
}
pageView.setCurrentPageIndex(1);
this.schedule(this._autoScrollToNextPage, 5);
}
/** 轮播图事件 */
private _openBannerLayer(index : number) {
console.log("点击的轮播图 === ",index);
}
/** 翻页回调 */
private _callback(pageView: PageView) : void{
let newIndex : number = pageView.getCurrentPageIndex();
if (newIndex == 0) {
pageView.scrollToPage(this._pageCount, 0);
pageView.setCurrentPageIndex(this._pageCount);
}
if (newIndex == this._pageCount + 1) {
pageView.scrollToPage(1, 0);
pageView.setCurrentPageIndex(1);
}
}
/** 自动翻页 */
private _autoScrollToNextPage() : void{
let pageView = this.bannerPageView.getComponent(PageView);
let currentIndex = pageView.getCurrentPageIndex();
let nextIndex = (currentIndex + 1) % (this._pageCount + 2);
pageView.scrollToPage(nextIndex,1);
}
}