You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
AdLike2048/assets/script/ui/BaseInfo.ts

213 lines
7.5 KiB

3 weeks ago
import { _decorator, Component, isValid, Label, Node } from 'cc';
import { GBundle, GPath, UIID } from '../game/ConfigRes';
import { GEvent } from '../../mx/module/event/GEvent';
import MTools from '../../mx/tools/MTools';
import { ConstNumType } from '../game/GameDataEnums';
import { ItemNumType, StatisticsType } from '../game/GameData';
import { sdkConfig } from '../sdk/sdkConfig';
import { HeadIconManager } from '../manager/HeadIconManager';
const { ccclass, property } = _decorator;
@ccclass('BaseInfo')
export class BaseInfo extends Component {
ui_head: Node
ui_power: Node
lb_combatPower: Label
ui_vigour: Node
ui_vigourRed: Node
lb_vigour: Label
timeLab: Label
ui_money: Node
lb_money: Label
ui_diamond: Node
lb_diamond: Label
ui_skipAd: Node
lb_skipAd: Label
maxVigour: number = 30
/** 绑定顶部信息条节点(兼容 ui_power 新布局与 ui_head/lb_combatPower 旧布局) */
protected onLoad(): void {
this.ui_head = this.node.getChildByName('ui_head');
this.ui_power = this.node.getChildByName('ui_power');
this.lb_combatPower = this._bindPowerLabel();
this.ui_vigour = this.node.getChildByName('ui_vigour');
if (this.ui_vigour) {
this.lb_vigour = this.ui_vigour.getChildByName('lb_vigour')?.getComponent(Label) ?? null;
this.ui_vigourRed = this.ui_vigour.getChildByName('红点');
this.timeLab = this.ui_vigour.getChildByName('timeLab')?.getComponent(Label) ?? null;
}
this.ui_money = this.node.getChildByName('ui_money');
if (this.ui_money) {
this.lb_money = this.ui_money.getChildByName('lb_money')?.getComponent(Label) ?? null;
}
this.ui_diamond = this.node.getChildByName('ui_diamond');
if (this.ui_diamond) {
this.lb_diamond = this.ui_diamond.getChildByName('lb_diamond')?.getComponent(Label) ?? null;
}
this.ui_skipAd = this.node.getChildByName('ui_skipAd');
if (this.ui_skipAd) {
this.lb_skipAd = this.ui_skipAd.getChildByName('lb_skipAd')?.getComponent(Label) ?? null;
}
}
/** 战力 Label:优先 ui_power/lb_power,回退 ui_head/lb_combatPower */
private _bindPowerLabel(): Label | null {
if (this.ui_power) {
const lb = this.ui_power.getChildByName('lb_power')?.getComponent(Label);
if (lb) return lb;
}
if (this.ui_head) {
const lb = this.ui_head.getChildByName('lb_combatPower')?.getComponent(Label);
if (lb) return lb;
}
console.log('[BaseInfo] 未找到战力 Label(ui_power/lb_power 或 ui_head/lb_combatPower)');
return null;
}
protected onEnable(): void {
GEvent.Ins.on(GEvent.UpdateItemNum, this.updateItemNum, this);
if (this.ui_head) {
this.ui_head.on(Node.EventType.TOUCH_END, this.clickHead, this);
}
if (this.ui_vigour) {
this.ui_vigour.on(Node.EventType.TOUCH_END, this.clickVigour, this);
}
if (this.ui_money) {
this.ui_money.on(Node.EventType.TOUCH_END, this.clickMoney, this);
}
if (this.ui_diamond) {
this.ui_diamond.on(Node.EventType.TOUCH_END, this.clickDiamond, this);
}
if (this.ui_skipAd) {
this.ui_skipAd.on(Node.EventType.TOUCH_END, this.clickSkipAd, this);
}
GEvent.Ins.on(GEvent.UpdateCombatPower, this.updateCombatPower, this);
GEvent.Ins.on(GEvent.UpdateVigour, this.updateVigour, this);
GEvent.Ins.on(GEvent.UpdateMoney, this.updateMoney, this);
GEvent.Ins.on(GEvent.UpdateDiamond, this.updateDiamond, this);
GEvent.Ins.on(GEvent.UpdateRole, this.updateHead, this);
GEvent.Ins.on(GEvent.UpdateHeadIcon, this.updateHead, this);
if (this.ui_skipAd) {
this.ui_skipAd.active = sdkConfig.isIAPVersion;
}
this.updateItemNum();
this.schedule(this._tickVigourRecovery, 1);
}
private _tickVigourRecovery(): void {
if (!this.timeLab) return;
const maxVigour = gg.data.table.getConst(ConstNumType.MaxVigour);
const curentVigour = gg.data.getItemNum(ItemNumType.vigour);
this.timeLab.node.active = curentVigour < maxVigour;
if (this.timeLab.node.active) {
const time = gg.data.project.getAddVigourTime();
this.timeLab.string = MTools.formatTimeString(time, 'mm:ss');
}
}
protected onDisable(): void {
this.unschedule(this._tickVigourRecovery);
GEvent.Ins.off(GEvent.UpdateItemNum, this.updateItemNum, this);
if (isValid(this.ui_head)) {
this.ui_head.off(Node.EventType.TOUCH_END, this.clickHead, this);
}
if (isValid(this.ui_vigour)) {
this.ui_vigour.off(Node.EventType.TOUCH_END, this.clickVigour, this);
}
if (isValid(this.ui_money)) {
this.ui_money.off(Node.EventType.TOUCH_END, this.clickMoney, this);
}
if (isValid(this.ui_diamond)) {
this.ui_diamond.off(Node.EventType.TOUCH_END, this.clickDiamond, this);
}
if (isValid(this.ui_skipAd)) {
this.ui_skipAd.off(Node.EventType.TOUCH_END, this.clickSkipAd, this);
}
GEvent.Ins.off(GEvent.UpdateCombatPower, this.updateCombatPower, this);
GEvent.Ins.off(GEvent.UpdateVigour, this.updateVigour, this);
GEvent.Ins.off(GEvent.UpdateMoney, this.updateMoney, this);
GEvent.Ins.off(GEvent.UpdateDiamond, this.updateDiamond, this);
GEvent.Ins.off(GEvent.UpdateRole, this.updateHead, this);
GEvent.Ins.off(GEvent.UpdateHeadIcon, this.updateHead, this);
}
protected start(): void {
this.updateCombatPower()
this.updateVigour()
this.updateMoney()
this.updateDiamond()
this.updateHead()
}
clickHead() {
gg.ui.openPanel(UIID.Settings);
}
clickVigour() {
gg.ui.openPanel(UIID.buyPower);
}
clickMoney() {
gg.game.enterMain(UIID.Shop);
}
clickDiamond() {
gg.game.enterMain(UIID.Shop);
}
clickSkipAd() {
}
/** 刷新首页头像(ui_head/headMask/head) */
updateHead() {
void HeadIconManager.refreshUiHeadAsync(this.ui_head);
}
/** 更新战力 */
updateCombatPower() {
if (!this.lb_combatPower) return;
const num = gg.data.getCurPower();
this.lb_combatPower.string = MTools.formatChineseUnit({ num: num });
}
/** 更新体力 */
updateVigour() {
if (!this.lb_vigour) return;
const num = gg.data.getVigour();
this.lb_vigour.string = MTools.formatChineseUnit({ num: num }) + '/' + this.maxVigour;
}
/** 更新金钱 */
updateMoney() {
if (!this.lb_money) return;
const num = gg.data.getMoney();
this.lb_money.string = MTools.formatChineseUnit({ num: num });
}
/** 更新钻石 */
updateDiamond() {
if (!this.lb_diamond) return;
const num = gg.data.getDiamond();
this.lb_diamond.string = MTools.formatChineseUnit({ num: num });
}
/** 更新免广券 */
updateSkipAd() {
if (!this.lb_skipAd) return;
const num = gg.data.getItemNum(ItemNumType.skipAd);
this.lb_skipAd.string = MTools.formatChineseUnit({ num: num });
}
updateItemNum() {
//更新所有道具数量
this.updateVigour()
this.updateMoney()
this.updateDiamond()
this.updateSkipAd()
}
}