first commit
This commit is contained in:
195
assets/scripts/ButtonBadge.ts
Normal file
195
assets/scripts/ButtonBadge.ts
Normal file
@@ -0,0 +1,195 @@
|
||||
import { _decorator, Component, Node, Label, sys } from 'cc';
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass('ButtonBadge')
|
||||
export class ButtonBadge extends Component {
|
||||
@property(Node)
|
||||
hint_badge: Node | null = null;
|
||||
|
||||
@property(Label)
|
||||
hint_countLabel: Label | null = null;
|
||||
|
||||
@property(Node)
|
||||
hint_adIcon: Node | null = null;
|
||||
|
||||
@property(Node)
|
||||
shuffle_badge: Node | null = null;
|
||||
|
||||
@property(Label)
|
||||
shuffle_countLabel: Label | null = null;
|
||||
|
||||
@property(Node)
|
||||
shuffle_adIcon: Node | null = null;
|
||||
|
||||
@property(Node)
|
||||
magic_badge: Node | null = null;
|
||||
|
||||
@property(Label)
|
||||
magic_countLabel: Label | null = null;
|
||||
|
||||
@property(Node)
|
||||
magic_adIcon: Node | null = null;
|
||||
|
||||
private _hint_count: number = 0;
|
||||
private _hint_storageKey: string = "hint_button_usage_count";
|
||||
|
||||
private _shuffle_count: number = 0;
|
||||
private _shuffle_storageKey: string = "shuffle_button_usage_count";
|
||||
|
||||
private _magic_count: number = 0;
|
||||
private _magic_storageKey: string = "magic_button_usage_count";
|
||||
|
||||
onLoad() {
|
||||
// 从本地缓存读取
|
||||
const hintSaved = sys.localStorage.getItem(this._hint_storageKey);
|
||||
const shuffleSaved = sys.localStorage.getItem(this._shuffle_storageKey);
|
||||
const magicSaved = sys.localStorage.getItem(this._magic_storageKey);
|
||||
this._hint_count = hintSaved ? parseInt(hintSaved) : 3; // 默认初始 3 次
|
||||
this._shuffle_count = shuffleSaved ? parseInt(shuffleSaved) : 3; // 默认初始 3 次
|
||||
this._magic_count = magicSaved ? parseInt(magicSaved) : 3; // 默认初始 3 次
|
||||
console.log('load', this._shuffle_count, this._magic_count)
|
||||
this.updateHintView();
|
||||
this.updateShuffleView();
|
||||
this.updateMagicView();
|
||||
}
|
||||
|
||||
private updateHintView() {
|
||||
if (!this.hint_badge) return;
|
||||
|
||||
if (this._hint_count > 0) {
|
||||
this.hint_badge.active = true;
|
||||
if (this.hint_countLabel) {
|
||||
this.hint_countLabel.string = this._hint_count.toString();
|
||||
this.hint_countLabel.node.active = true;
|
||||
}
|
||||
if (this.hint_adIcon) this.hint_adIcon.active = false;
|
||||
} else {
|
||||
this.hint_badge.active = true;
|
||||
if (this.hint_countLabel) this.hint_countLabel.node.active = false;
|
||||
if (this.hint_adIcon) this.hint_adIcon.active = true;
|
||||
}
|
||||
}
|
||||
|
||||
private updateShuffleView() {
|
||||
if (!this.shuffle_badge) return;
|
||||
|
||||
if (this._shuffle_count > 0) {
|
||||
this.shuffle_badge.active = true;
|
||||
if (this.shuffle_countLabel) {
|
||||
this.shuffle_countLabel.string = this._shuffle_count.toString();
|
||||
this.shuffle_countLabel.node.active = true;
|
||||
}
|
||||
if (this.shuffle_adIcon) this.shuffle_adIcon.active = false;
|
||||
} else {
|
||||
this.shuffle_badge.active = true;
|
||||
if (this.shuffle_countLabel) this.shuffle_countLabel.node.active = false;
|
||||
if (this.shuffle_adIcon) this.shuffle_adIcon.active = true;
|
||||
}
|
||||
}
|
||||
|
||||
private updateMagicView() {
|
||||
if (!this.magic_badge) return;
|
||||
|
||||
if (this._magic_count > 0) {
|
||||
this.magic_badge.active = true;
|
||||
if (this.magic_countLabel) {
|
||||
this.magic_countLabel.string = this._magic_count.toString();
|
||||
this.magic_countLabel.node.active = true;
|
||||
}
|
||||
if (this.magic_adIcon) this.magic_adIcon.active = false;
|
||||
} else {
|
||||
this.magic_badge.active = true;
|
||||
if (this.magic_countLabel) this.magic_countLabel.node.active = false;
|
||||
if (this.magic_adIcon) this.magic_adIcon.active = true;
|
||||
}
|
||||
}
|
||||
|
||||
private saveHint() {
|
||||
sys.localStorage.setItem(this._hint_storageKey, this._hint_count.toString());
|
||||
}
|
||||
|
||||
private saveShuffle() {
|
||||
sys.localStorage.setItem(this._shuffle_storageKey, this._shuffle_count.toString());
|
||||
}
|
||||
|
||||
private saveMagic() {
|
||||
sys.localStorage.setItem(this._magic_storageKey, this._magic_count.toString());
|
||||
}
|
||||
|
||||
public useHintOnce(): boolean {
|
||||
if (this._hint_count > 0) {
|
||||
this._hint_count--;
|
||||
this.saveHint();
|
||||
this.updateHintView();
|
||||
return true; // 成功使用
|
||||
} else {
|
||||
// this.showAdAndReward();
|
||||
return false; // 没次数,去看广告
|
||||
}
|
||||
}
|
||||
|
||||
public useShuffleOnce(): boolean {
|
||||
if (this._shuffle_count > 0) {
|
||||
this._shuffle_count--;
|
||||
this.saveShuffle();
|
||||
this.updateShuffleView();
|
||||
return true; // 成功使用
|
||||
} else {
|
||||
// this.showAdAndReward();
|
||||
return false; // 没次数,去看广告
|
||||
}
|
||||
}
|
||||
|
||||
public useMagicOnce(): boolean {
|
||||
if (this._magic_count > 0) {
|
||||
this._magic_count--;
|
||||
this.saveMagic();
|
||||
this.updateMagicView();
|
||||
return true; // 成功使用
|
||||
} else {
|
||||
// this.showAdAndReward();
|
||||
return false; // 没次数,去看广告
|
||||
}
|
||||
}
|
||||
|
||||
public getHintCount() {
|
||||
return this._hint_count;
|
||||
}
|
||||
|
||||
public getShuffleCount() {
|
||||
return this._shuffle_count;
|
||||
}
|
||||
|
||||
public getMagicCount() {
|
||||
return this._magic_count;
|
||||
}
|
||||
|
||||
public showAdAndReward(type) {
|
||||
// ⚠️ 这里接入广告 SDK
|
||||
console.log("显示广告...");
|
||||
|
||||
// 假设广告结束回调
|
||||
if(type === 'hint') this.onAdHintComplete();
|
||||
if(type === 'shuffle') this.onAdShuffleComplete();
|
||||
if(type === 'magic') this.onAdMagicComplete();
|
||||
|
||||
}
|
||||
|
||||
private onAdHintComplete() {
|
||||
this._hint_count += 3;
|
||||
this.saveHint();
|
||||
this.updateHintView();
|
||||
}
|
||||
|
||||
private onAdShuffleComplete() {
|
||||
this._shuffle_count += 3;
|
||||
this.saveShuffle();
|
||||
this.updateShuffleView();
|
||||
}
|
||||
|
||||
private onAdMagicComplete() {
|
||||
this._magic_count += 3;
|
||||
this.saveMagic();
|
||||
this.updateMagicView();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user