140 lines
5.6 KiB
TypeScript
140 lines
5.6 KiB
TypeScript
|
/************************************************************************************
|
||
|
|
||
|
* FileName : DanCell.ts
|
||
|
* Description :
|
||
|
* Version : v1.0.0
|
||
|
* CreateTime : 2024-12-06 16:20:50
|
||
|
* Author :
|
||
|
* Copyright (c) since 2024
|
||
|
* ==============================================================
|
||
|
* Method Description:
|
||
|
*
|
||
|
* ==============================================================
|
||
|
************************************************************************************/
|
||
|
import {_decorator, Color, Component, Label, Node, Sprite, SpriteFrame} from 'cc';
|
||
|
import {IDanCfg} from "db://assets/module_basic/shared/configs/interface/IDanCfg";
|
||
|
import {resLoader} from "db://assets/core_tgx/base/utils/ResLoader";
|
||
|
import {ModuleDef} from "db://assets/scripts/ModuleDef";
|
||
|
import {areanMgr} from "db://assets/module_arean/scripts/AreanManager";
|
||
|
import {UserMgr} from "db://assets/module_basic/scripts/UserMgr";
|
||
|
import {tgxAudioMgr} from "db://assets/core_tgx/tgx";
|
||
|
import {CommonFun} from "db://assets/module_arean/scripts/CommonFun";
|
||
|
|
||
|
const { ccclass, property } = _decorator;
|
||
|
|
||
|
@ccclass('DanCell')
|
||
|
export class DanCell extends Component {
|
||
|
@property(Label)
|
||
|
danName : Label = null;
|
||
|
|
||
|
@property(Sprite)
|
||
|
danIcon : Sprite = null;
|
||
|
|
||
|
@property(Node)
|
||
|
starNode : Node = null;
|
||
|
|
||
|
@property(Node)
|
||
|
rewardNode : Node = null;
|
||
|
|
||
|
private _fun : Function = null;
|
||
|
private _canReward : boolean = false;
|
||
|
private _localData : IDanCfg = null;
|
||
|
private _serverData : any = null;
|
||
|
private _curDanInfo : {curDan : number,mainDan : number,needStar : number} = null;
|
||
|
|
||
|
public init(curDanInfo : {curDan : number,mainDan : number,needStar : number},localData : IDanCfg,serverDanData : any,canReward : any,callBack : Function) : void{
|
||
|
this._curDanInfo = curDanInfo;
|
||
|
this._localData = localData;
|
||
|
this._serverData = serverDanData;
|
||
|
this._fun = callBack;
|
||
|
this._canReward = canReward;
|
||
|
this._updateBtnState();
|
||
|
this._initLocalData(localData);
|
||
|
this._updateStar();
|
||
|
this.updateCurDanState(curDanInfo.curDan === localData.id);
|
||
|
this._updateBgColor();
|
||
|
this._initReward();
|
||
|
this._updateRewarded(false);
|
||
|
}
|
||
|
|
||
|
private _initLocalData(localData : IDanCfg) : void{
|
||
|
this.danName.string = localData.danName;
|
||
|
let sp : SpriteFrame = resLoader.getSpriteFrame('res/danIcon/dan_' + localData.mainDan,ModuleDef.Arean);
|
||
|
if(sp) this.danIcon.spriteFrame = sp;
|
||
|
}
|
||
|
|
||
|
// 当前段位标识
|
||
|
public updateCurDanState(show : boolean) : void{
|
||
|
this.node.getChildByName("curDan").active = show
|
||
|
}
|
||
|
|
||
|
private _initReward() : void{
|
||
|
let reward = this._localData.reward;
|
||
|
let rewardNode = this.node.getChildByName("rewardNode");
|
||
|
let len = reward.length;
|
||
|
for(let i = 0;i < len;i++){
|
||
|
let element = rewardNode.getChildByName(`itemBg_${i + 1}`);
|
||
|
element.active = true;
|
||
|
let arr = reward[i];
|
||
|
let itemId = arr[0];
|
||
|
let count = arr[1];
|
||
|
let res = CommonFun.getInstance().getIcon(itemId);
|
||
|
let sp : SpriteFrame = resLoader.getSpriteFrame(res,ModuleDef.Arean);
|
||
|
if(sp) element.getChildByName("icon").getComponent(Sprite).spriteFrame = sp;
|
||
|
element.getChildByName("count").getComponent(Label).string = `${count}`;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private _updateBgColor() : void{
|
||
|
this.node.getChildByName("cellBg4").active = this._localData.reward.length > 0 ;
|
||
|
}
|
||
|
|
||
|
private _updateStar() : void{
|
||
|
let starNode = this.node.getChildByName("starNode");
|
||
|
let starNum = this._localData.starNumber;
|
||
|
if(starNum > 5){
|
||
|
starNode.children[0].active = true;
|
||
|
starNode.children[0].getChildByName("star").active = true;
|
||
|
starNode.getChildByName("lab").active = true;
|
||
|
let preData = areanMgr.cfgMgr.DanData.getData(this._localData.id - 1);
|
||
|
let curStar = 0;
|
||
|
if(preData){
|
||
|
curStar = UserMgr.inst.userInfo.danGrading - preData.allStar;
|
||
|
}else{
|
||
|
curStar = UserMgr.inst.userInfo.danGrading;
|
||
|
}
|
||
|
curStar = curStar > 0 ? curStar : 0;
|
||
|
starNode.getChildByName("lab").getComponent(Label).string = `${curStar}/${starNum}`;
|
||
|
}else{
|
||
|
let curStar = UserMgr.inst.userInfo.danGrading;
|
||
|
for(let i = 0;i < starNum;i++){
|
||
|
starNode.children[i].active = true;
|
||
|
starNode.children[i].getChildByName("star").active = this._curDanInfo.curDan > this._localData.id;
|
||
|
if(this._curDanInfo.curDan === this._localData.id){
|
||
|
let shortStar = this._curDanInfo.needStar - curStar;
|
||
|
let showNum = starNum - shortStar; // 2
|
||
|
starNode.children[i].getChildByName("star").active = showNum >= (i + 1);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private _updateRewarded(show : boolean) : void{
|
||
|
// console.log(" this._serverData ====",this._serverData)
|
||
|
this.node.getChildByName("rewarded").active = show || this._serverData.find((data)=> data.danId === this._localData.id);
|
||
|
}
|
||
|
|
||
|
private _updateBtnState() : void{
|
||
|
// this.btnNode.getChildByName("noFinished").active = !this._canReward;
|
||
|
// this.btnNode.getChildByName("lab").getComponent(Label).string = !this._canReward ? "已领取" : "领取";
|
||
|
}
|
||
|
|
||
|
public clickBtn() : void{
|
||
|
if(this._canReward){
|
||
|
tgxAudioMgr.inst.playCommonBtn();
|
||
|
this._fun && this._fun(this._localData.id);
|
||
|
this._updateRewarded(true);
|
||
|
this._canReward = false;
|
||
|
}
|
||
|
}
|
||
|
}
|