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

628 lines
21 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 {UIDan} from "db://assets/module_arean/ui_dan/UIDan";
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 {UIChooseHero} from "db://assets/module_arean/ui_choose_hero/UIChooseHero";
import {EMusicDefine} from "db://assets/module_basic/Define/MusicDefine";
import {UICollection} from "db://assets/module_arean/ui_collection/UICollection";
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 {UISeason} from "db://assets/module_arean/ui_season/UISeason";
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() {
// await AreanNetMgr.inst.sendMsg_KingTDGetUserData();
// tgxUIMgr.inst.showUI(UIChat);
// tgxUIMgr.inst.showUI(UIAnnouncement,(ui:UIAnnouncement)=>{
// ui.setPosition(0,280);
// });
if (AreanLobbyScene.isLoaded) {
AreanNetMgr.inst.initNetMsgHandlers();
await AreanNetMgr.inst.sendMsg_GetUserData();
this.loadingNode.active = true;
await this.loadData();
} else {
this.loadingNode.active = false;
this._updateHeroSpine();
this._updateHeadInfo();
this._updateDanInfo();
}
this.onCompleteCallback().then();
this._updateChooseHeroBtnState(false);
this._updateFightModelName();
this._initBannerPageView();
// // TODO: 测试接口(添加英雄)
// await UserMgr.inst.rpc_addHero(UserMgr.inst.userId,1003);
// let res = await UserMgr.inst.rpc_updateHeroProficiency(1001,22);
// await UserMgr.inst.rpc_getHeroList(UserMgr.inst.userId).then();
// await UserMgr.inst.rpc_getDanInfo(UserMgr.inst.userId).then();
// let itemList = await UserMgr.inst.rpc_getItemInfo(UserMgr.inst.userId);
// console.log("itemList === ",itemList);
}
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);
director.on(AreanEvent.ChangeHero, this._updateHeroSpine.bind(this), 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);
}
}
private _updateHeroSpine() : void{
let localHeroData = areanMgr.cfgMgr.HeroDatas.getAllDataMap();
let curHeroData = localHeroData.get(UserMgr.inst.userInfo.heroId);
if(curHeroData){
let skin = curHeroData.skinRes[0];
let skinId = areanMgr.cfgMgr.SkinData.getData(skin).icon
resLoader.load<Prefab>(ModuleDef.Arean,`res/Prefebs/heroSpine/${skinId}`,(err: Error, prefab: Prefab) => {
if(err){
console.error(err);
return;
}else {
let spineNode = instantiate(prefab);
this.heroSpineNode.destroyAllChildren();
this.heroSpineNode.addChild(spineNode);
// spineNode.position = v3(0,-55,0);
}
})
}else {
console.error(`${UserMgr.inst.userInfo.heroId}英雄数据不存在`);
}
}
protected onDestroy() {
director.off(AreanEvent.ChangeHero, this._updateHeroSpine.bind(this), this);
this.unschedule(this._autoScrollToNextPage);
}
// 多人匹配,自定义按钮动画
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);
})
}
// 段位界面
onBtnShowDanLayer(): void {
if(!this._canClick) return;
this._canClick = false;
tgxAudioMgr.inst.playCommonBtn(EMusicDefine.EFFECT_CLICK);
tgxUIMgr.inst.showUI(UIDan,()=>{
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._updateHeroSpine();
this._updateHeadInfo();
this._updateDanInfo();
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();
}
/* 二级选择英雄按钮显隐 */
private _updateChooseHeroBtnState(show : boolean) : void{
if(this.chooseHeroBtn.active) return;
this.chooseHeroBtn.active = show;
if(show){
let time : number = 5;
tween(this.chooseHeroBtn)
.delay(time)
.call(()=>{
this.chooseHeroBtn.active = false;
})
.start();
}
}
/* 一级选择英雄按钮点击 */
onBtnHeroClicked(){
tgxAudioMgr.inst.playCommonBtn(EMusicDefine.EFFECT_CLICK);
this._updateChooseHeroBtnState(true);
}
/* 二级选择英雄按钮点击 */
onBtnHeroClicked2(){
if(!this._canClick) return;
this._canClick = false;
tgxAudioMgr.inst.playCommonBtn(EMusicDefine.EFFECT_OPEN);
tgxUIMgr.inst.showUI(UIChooseHero,()=>{
this.setCanClick(true);
});
this.chooseHeroBtn.active = false;
}
/** 赛季 */
public onBtnSeasonClicked(){
if(!this._canClick) return;
this._canClick = false;
tgxAudioMgr.inst.playCommonBtn(EMusicDefine.EFFECT_INVALID);
tgxUIMgr.inst.showUI(UISeason,()=>{
this.setCanClick(true);
});
}
/** 排行榜 */
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("研发团队加班中,敬请期待!");
}
/** 收藏点击事件 */
onBtnCollectionClicked(){
if(!this._canClick) return;
this._canClick = false;
tgxAudioMgr.inst.playCommonBtn(EMusicDefine.EFFECT_OPEN);
tgxUIMgr.inst.showUI(UICollection,()=>{
this.setCanClick(true);
});
}
/** 背包事件 */
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)
}
/** 更新段位信息 */
private _updateDanInfo() : void{
let curDanStar : number = UserMgr.inst.userInfo.danGrading;
let curDanInfo = CommonFun.getInstance().calDanData(curDanStar);
console.log("大厅刷新要段位信息 === ",curDanInfo)
let curDan : number = curDanInfo.curDan;
this.danInfoNode.getChildByName("tips").active = curDanInfo.curDan === 0;
if(curDan > 0){
let localData : IDanCfg = areanMgr.cfgMgr.DanData.getData(curDan);
let danInfo = this.danInfoNode.getChildByName("danInfo");
danInfo.active = true;
danInfo.getChildByName("danName").getComponent(Label).string = localData.danName;
let sp : SpriteFrame = resLoader.getSpriteFrame('res/Image/danIcon/dan_' + localData.mainDan,ModuleDef.Arean);
if(sp) danInfo.getChildByName("icon").getComponent(Sprite).spriteFrame = sp;
let normalStarNode = this.danInfoNode.getChildByName("starNode");
let specialStarNode = this.danInfoNode.getChildByName("specialStarNode");
let starNum = localData.starNumber;
normalStarNode.active = starNum > 0 && starNum < 10;
specialStarNode.active = starNum >= 10;
// 刷新星星
if(starNum > 0 && starNum < 10){
for(let i = 0;i < starNum;i++){
normalStarNode.children[i].active = true;
normalStarNode.children[i].getChildByName("star").active = curDanInfo.curDan > localData.id;
if(curDanInfo.curDan === localData.id){
let shortStar = curDanInfo.needStar - curDanStar;
let showNum = starNum - shortStar; // 2
normalStarNode.children[i].getChildByName("star").active = showNum >= (i + 1);
}
}
}else if( starNum >= 10){
let preData = areanMgr.cfgMgr.DanData.getData(localData.id - 1);
let curStar = 0;
if(preData){
curStar = curDanStar - preData.allStar;
}else{
curStar = curDanStar;
}
curStar = curStar > 0 ? curStar : 0;
specialStarNode.getChildByName("num").getComponent(Label).string = `${curStar}/${starNum}`;
}
}
}
/** 获取页面通过索引 */
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);
}
}