import { _decorator, Component, Node } from 'cc'; import { GameUILayers, gui } from 'db://assets/core/ui/ui'; import ui_base from 'db://assets/core/ui/ui_base'; import { ModuleDef } from 'db://assets/Script/ModuleDef'; import { Layout_Skin } from './Layout_Skin'; import { ch } from 'db://assets/ch/ch'; import { UI_Main } from '../main/UI_Main'; import { Role } from '../../game/tui/role/Role'; import { LvData } from '../../game/LvData/LvData'; import { ani_ui } from '../../game/animation_utils'; import ch_audio from 'db://assets/ch/audio/audio'; import { audioManager } from '../../Audio/AudioManager'; const { ccclass, property } = _decorator; @ccclass('UI_Skin') export class UI_Skin extends ui_base { constructor() { super(ModuleDef.GAME, 'ui/UI_Skin/skinDlg', GameUILayers.HUD, Layout_Skin); } private stoneButtons: Node[] = []; private roleButtons: Node[] = []; private blockButtons: Node[] = []; protected onCreated() { ani_ui(this.node); const layout = this.getLayout(); //人物 this.roleButtons = [ layout.roleBtn1, layout.roleBtn2, layout.roleBtn3, layout.roleBtn4 ]; //石头 this.stoneButtons = [ layout.stoneBtn1, layout.stoneBtn2, layout.stoneBtn3, layout.stoneBtn4 ]; //方块 this.blockButtons = [ layout.blcokBtn1, layout.blcokBtn2, layout.blcokBtn3, layout.blcokBtn4 ]; this.onButtonEvent(layout.offBtn, () => { gui.close(UI_Skin); }); this.onButtonEvent(layout.RS, () => { audioManager.playOneShot('sound/click_Btn'); layout.roleSkin.active = true layout.blockBtn.active = false layout.stoneSkin.active = false }); this.onButtonEvent(layout.SK, () => { audioManager.playOneShot('sound/click_Btn'); layout.stoneSkin.active = true layout.roleSkin.active = false layout.blockBtn.active = false }); this.onButtonEvent(layout.BS, () => { audioManager.playOneShot('sound/click_Btn'); layout.blockBtn.active = true layout.roleSkin.active = false layout.stoneSkin.active = false }); // 绑定事件 this.onButtonEvent(layout.stoneBtn1, () => handleStoneButton(layout.stoneBtn1, 1, this.stoneButtons)); this.onButtonEvent(layout.stoneBtn2, () => handleStoneButton(layout.stoneBtn2, 2, this.stoneButtons)); this.onButtonEvent(layout.stoneBtn3, () => handleStoneButton(layout.stoneBtn3, 3, this.stoneButtons)); this.onButtonEvent(layout.stoneBtn4, () => handleStoneButton(layout.stoneBtn4, 4, this.stoneButtons)); // 统一 const handleStoneButton = async (button: Node, skinId: number, buttons: Node[]) => { const seven = button.getChildByName('7'); const yes = button.getChildByName('yes'); if (!seven.active) { // 皮肤已解锁,直接选择 this.stoneButtons.forEach(btn => { btn.getChildByName('yes').active = false; }); yes.active = true; LvData.instance.stpf = skinId; console.log('石头皮肤' + LvData.instance.stpf) return; } if (seven.active) { // 皮肤未解锁,观看广告解锁 const res = await chsdk.playRewardAd(`获得stone${skinId}皮肤`); if (res) { LvData.instance.unlockStoneSkin(skinId); // 解锁皮肤并选择 this.stoneButtons.forEach(btn => { btn.getChildByName('yes').active = false; }); seven.active = false; yes.active = true; LvData.instance.stpf = skinId; console.log('石头皮肤解锁并选择' + LvData.instance.stpf) } } }; // 绑定事件 this.onButtonEvent(layout.roleBtn1, () => handleRoleButton(layout.roleBtn1, 1, this.roleButtons)); this.onButtonEvent(layout.roleBtn2, () => handleRoleButton(layout.roleBtn2, 2, this.roleButtons)); this.onButtonEvent(layout.roleBtn3, () => handleRoleButton(layout.roleBtn3, 3, this.roleButtons)); this.onButtonEvent(layout.roleBtn4, () => handleRoleButton(layout.roleBtn4, 4, this.roleButtons)); // 统一 const handleRoleButton = async (button: Node, skinId: number, buttons: Node[]) => { const seven = button.getChildByName('7'); const yes = button.getChildByName('yes'); if (!seven.active) { // 确保只选中一个 this.roleButtons.forEach(btn => { btn.getChildByName('yes').active = false; }); yes.active = true; LvData.instance.rwpf = skinId; console.log('人物皮肤' + LvData.instance.rwpf); return; } if (seven.active) { const res = await chsdk.playRewardAd(`获得role${skinId}皮肤`); if (res) { LvData.instance.unlockRoleSkin(skinId); // 先关闭所有yes this.roleButtons.forEach(btn => { btn.getChildByName('yes').active = false; }); // 再开启当前yes seven.active = false; yes.active = true; LvData.instance.rwpf = skinId; console.log('人物皮肤解锁并选择' + LvData.instance.rwpf); } } }; // 绑定事件 this.onButtonEvent(layout.blcokBtn1, () => handleBlcokButton(layout.blcokBtn1, 1, this.blockButtons)); this.onButtonEvent(layout.blcokBtn2, () => handleBlcokButton(layout.blcokBtn2, 2, this.blockButtons)); this.onButtonEvent(layout.blcokBtn3, () => handleBlcokButton(layout.blcokBtn3, 3, this.blockButtons)); this.onButtonEvent(layout.blcokBtn4, () => handleBlcokButton(layout.blcokBtn4, 4, this.blockButtons)); // 统一 const handleBlcokButton = async (button: Node, skinId: number, buttons: Node[]) => { const seven = button.getChildByName('7'); const yes = button.getChildByName('yes'); if (!seven.active) { // 确保只选中一个 this.blockButtons.forEach(btn => { btn.getChildByName('yes').active = false; }); yes.active = true; LvData.instance.fkpf = skinId; console.log('方块皮肤' + LvData.instance.fkpf); return; } if (seven.active) { const res = await chsdk.playRewardAd(`获得block${skinId}皮肤`); if (res) { LvData.instance.unlockBlockSkin(skinId); // 先关闭所有yes this.blockButtons.forEach(btn => { btn.getChildByName('yes').active = false; }); // 再开启当前yes seven.active = false; yes.active = true; LvData.instance.fkpf = skinId; console.log('方块皮肤解锁并选择' + LvData.instance.fkpf); } } }; this.initSkinButtons(); } private initSkinButtons() { // 初始化石头按钮 this.stoneButtons.forEach((btn, index) => { const skinId = index + 1; const isSelected = skinId === LvData.instance.stpf; // 使用新的方法检查皮肤是否已解锁 const isUnlocked = LvData.instance.isStoneSkinUnlocked(skinId); btn.getChildByName('yes').active = isSelected; btn.getChildByName('7').active = !isUnlocked; }); // 初始化角色按钮 this.roleButtons.forEach((btn, index) => { const skinId = index + 1; const isSelected = skinId === LvData.instance.rwpf; // 使用新的方法检查皮肤是否已解锁 const isUnlocked = LvData.instance.isRoleSkinUnlocked(skinId); btn.getChildByName('yes').active = isSelected; btn.getChildByName('7').active = !isUnlocked; }); // 初始化方块按钮 this.blockButtons.forEach((btn, index) => { const skinId = index + 1; const isSelected = skinId === LvData.instance.fkpf; // 使用新的方法检查皮肤是否已解锁 const isUnlocked = LvData.instance.isBlockSkinUnlocked(skinId); btn.getChildByName('yes').active = isSelected; btn.getChildByName('7').active = !isUnlocked; }); } }