import Userdata from "./Userdata";
|
import Global from "./Global";
|
import Menu from "./Menu";
|
import SkinItem from "./SkinItem";
|
import { Sdk } from "./Sdk/Sdk";
|
import RankTool from "./Sdk/RankTool";
|
import { SdkToutiao } from "./Sdk/SdkToutiao";
|
|
const { ccclass, property } = cc._decorator;
|
|
@ccclass
|
export default class SkinLayer extends cc.Component {
|
// LIFE-CYCLE CALLBACKS:
|
|
@property(cc.Label)
|
gemsLabel: cc.Label = null;
|
|
@property(cc.Prefab)
|
skinItemPrefab: cc.Prefab = null;
|
|
@property(cc.ScrollView)
|
scrollview: cc.ScrollView = null;
|
|
allItems: any = {};
|
enterSkin: number = -1;
|
|
onLoad() {
|
for (let i = 0; i < 100; ++i) {
|
let node = cc.instantiate(this.skinItemPrefab);
|
let item = node.getComponent(SkinItem);
|
item.initData(i, this);
|
node.parent = this.scrollview.content;
|
node.active = true;
|
|
this.allItems[i] = item;
|
}
|
}
|
|
onEnable() {
|
this.enterSkin = Userdata.CurrSkin;
|
this.updateGems();
|
}
|
|
onDisable() {
|
Userdata.saveUserdata();
|
if (this.enterSkin != Userdata.CurrSkin) {//update skin
|
RankTool.uploadAsync(Userdata.Stars, Userdata.CurrSkin, true);
|
}
|
}
|
|
onClickClose() {
|
this.node.active = false;
|
}
|
|
updateGems() {
|
this.gemsLabel.string = Userdata.Gem.toString();
|
}
|
|
buyBall(id: number) {
|
if (Userdata.Gem >= Global.SKIN_COST) {
|
let lastId = Userdata.CurrSkin;
|
Userdata.unlockSkin(id);
|
Userdata.addGem(-Global.SKIN_COST);
|
this.updateGems();
|
|
this.allItems[lastId].updateState();
|
this.allItems[id].updateState();
|
|
Global.showTip('恭喜, 解锁了新皮肤!');
|
|
let node = cc.find('Canvas');
|
if (node) {
|
node.emit('updateInfo');
|
}
|
} else {
|
Global.showTip('钻石不足');
|
this.showShop();
|
}
|
}
|
|
useBall(id: number) {
|
let lastId = Userdata.CurrSkin;
|
Userdata.CurrSkin = id;
|
|
this.allItems[lastId].updateState();
|
this.allItems[id].updateState();
|
}
|
|
showShop() {
|
if (Sdk.isToutiao) {
|
return;
|
}
|
Sdk.ins.isPaymentReady(ready => {
|
if (ready) {//payment ready
|
let loading = cc.find('Canvas/Loading');
|
if (loading) {
|
loading.active = true;
|
loading.zIndex = 999;
|
}
|
|
cc.loader.loadRes("prefabs/ui/ShopLayer", (err, prefab) => {
|
let _node = cc.instantiate(prefab);
|
_node.name = "Shop";
|
|
loading.active = false;
|
_node.parent = cc.find('Canvas');
|
_node.active = true;
|
});
|
}
|
});
|
}
|
|
onClickWatchAd() {
|
Global.debounceClicks(() => {
|
let isGot = false;
|
if (Sdk.isToutiao) {
|
SdkToutiao.showRewardVideoAd(() => {
|
if (!isGot) {
|
Userdata.giveVideoRewardRuby();
|
// Global.showTip("Got " + Global.WATCH_VIDEO_GEM + " Gems !");
|
Global.showTip("获得 " + Global.WATCH_VIDEO_GEM + " 钻石 !");
|
this.updateGems();
|
|
Userdata.saveUserdata();
|
let node = cc.find('Canvas');
|
if (node) {
|
node.emit('updateInfo');
|
}
|
}
|
}, () => {
|
Global.showTip('广告展示失败, 请稍后重试!');
|
});
|
|
return;
|
}
|
|
Sdk.ins.showVideoAd(result => {
|
if (result) {
|
if (!isGot) {
|
Userdata.giveVideoRewardRuby();
|
// Global.showTip("Got " + Global.WATCH_VIDEO_GEM + " Gems !");
|
Global.showTip("获得 " + Global.WATCH_VIDEO_GEM + " 钻石 !");
|
this.updateGems();
|
|
Userdata.saveUserdata();
|
let node = cc.find('Canvas');
|
if (node) {
|
node.emit('updateInfo');
|
}
|
}
|
} else {
|
Global.showTip('广告展示失败, 请稍后重试!');
|
// Global.showTip('Ads not ready!');
|
}
|
});
|
|
}, 'SkinLayer-onClickWatchAd');
|
}
|
}
|