import { coinMadnessMockEnabled, coinMadnessMockKey } from './CoinMadnessProgress'; import { MiniGameSdk } from './Sdk/MiniGameSdk'; import HomePopupQueue from '../home_popup_queue/HomePopupQueue'; const { ccclass } = cc._decorator; const BUNDLE = 'coin_madness'; /** Resident bridge only: never import the bundle's UI class or preload its assets. */ @ccclass export default class CoinMadnessHost extends cc.Component { private static occupied = false; private bundle: cc.AssetManager.Bundle = null; private panel: cc.Node = null; private entry: cc.Node = null; private loading = false; private releasing = false; private requestId = 0; private finishQueue: () => void = null; private alive = true; start() { if (coinMadnessMockEnabled()) this.updateEntry(); } private updateEntry() { const visible = coinMadnessMockEnabled(); if (!visible) { if (this.entry) this.entry.destroy(); this.entry = null; return; } if (!this.entry) { this.entry = new cc.Node('CoinMadnessEntry'); (this.node.getChildByName('Load') || this.node).addChild(this.entry); this.entry.setContentSize(146, 112); const widget = this.entry.addComponent(cc.Widget); widget.isAlignLeft = true; widget.left = 24; widget.isAlignBottom = true; widget.bottom = 425; const g = this.entry.addComponent(cc.Graphics); g.fillColor = new cc.Color(231, 121, 24); g.roundRect(-73, -56, 146, 112, 22); g.fill(); const title = new cc.Node('Title'); this.entry.addChild(title); const label = title.addComponent(cc.Label); label.string = '金币狂热'; label.fontSize = 27; label.useSystemFont = true; label.lineHeight = 31; title.color = cc.Color.WHITE; this.entry.on(cc.Node.EventType.TOUCH_END, this.open, this); } this.entry.getChildByName('Title').getComponent(cc.Label).string = '金币狂热\n模拟联调'; this.entry.active = true; } open() { if (!coinMadnessMockEnabled() || this.loading || this.releasing || this.panel) return; HomePopupQueue.get(this.node).enqueue('coinMadness', 55, done => { if (!this.alive) { done(); return; } this.finishQueue = done; this.load(); }); } private load() { if (CoinMadnessHost.occupied) { this.close(); this.toast('活动资源释放中,请稍后重试'); return; } CoinMadnessHost.occupied = true; this.loading = true; const requestId = ++this.requestId; cc.assetManager.loadBundle(BUNDLE, (error, bundle) => { if (error) { CoinMadnessHost.occupied = false; this.loading = false; this.close(); this.toast('活动加载失败,请重试'); return; } if (!this.alive || requestId !== this.requestId) { this.release(bundle); return; } this.bundle = bundle; bundle.load('CoinMadness', cc.Prefab, (loadError, prefab: cc.Prefab) => { this.loading = false; if (!this.alive || requestId !== this.requestId) { this.release(bundle); return; } if (loadError) { this.close(); this.toast('活动资源加载失败,请重试'); return; } this.panel = cc.instantiate(prefab); this.node.addChild(this.panel); this.panel.zIndex = cc.macro.MAX_ZINDEX - 1; const key = coinMadnessMockKey(); this.panel.getComponent('CoinMadnessPanel').initialize(null, { close: () => this.close(), read: () => cc.fx.StorageMessage.getStorage(key), write: value => cc.fx.StorageMessage.setStorage(key, value), level: () => cc.fx.GameConfig.GM_INFO.level, }); }); }); } close() { ++this.requestId; const wasLoading = this.loading; this.loading = false; if (this.panel) { this.panel.destroy(); this.panel = null; } const bundle = this.bundle; this.bundle = null; if (bundle && !wasLoading) this.release(bundle); const done = this.finishQueue; this.finishQueue = null; if (done) done(); } private release(bundle: cc.AssetManager.Bundle) { this.releasing = true; cc.director.once(cc.Director.EVENT_AFTER_DRAW, () => { bundle.releaseAll(); if (cc.assetManager.getBundle(BUNDLE) === bundle) cc.assetManager.removeBundle(bundle); this.releasing = false; CoinMadnessHost.occupied = false; }); } private toast(message: string) { if (this.alive) MiniGameSdk.API.showToast(message); } onDestroy() { this.alive = false; this.unscheduleAllCallbacks(); this.close(); if (this.entry) this.entry.destroy(); this.entry = null; } }