189 lines
6.0 KiB
TypeScript
189 lines
6.0 KiB
TypeScript
import NumberToImage from "./NumberToImage";
|
|
import JiaZai from "./JiaZai";
|
|
import { MiniGameSdk } from "./Sdk/MiniGameSdk";
|
|
|
|
const HEALTH_REFILL_PRICES = [1000, 2000, 4000, 8000];
|
|
const HEALTH_REFILL_STORAGE_PREFIX = "home_health_refill_";
|
|
|
|
|
|
const { ccclass, property } = cc._decorator;
|
|
|
|
@ccclass
|
|
export default class NewClass extends cc.Component {
|
|
//商店界面
|
|
@property(cc.Node)
|
|
heath: cc.Node = null;
|
|
//体力数量
|
|
@property(cc.Node)
|
|
heatht: cc.Node = null;
|
|
//切换的节点
|
|
@property([cc.Node])
|
|
switchNode: cc.Node[] = [];
|
|
//倒计时健康回复
|
|
@property(cc.Node)
|
|
timeNode: cc.Node = null;
|
|
//切换的按钮数组
|
|
@property([cc.Node])
|
|
switchButtons: cc.Node[] = [];
|
|
//金币花费数
|
|
@property(cc.Node)
|
|
coin: cc.Node = null;
|
|
|
|
btn_Touch: boolean = true;
|
|
|
|
onLoad() {
|
|
this.btn_Touch = true;
|
|
}
|
|
start() {
|
|
this.btn_Touch = true;
|
|
this.openPop();
|
|
}
|
|
//打开商店界面
|
|
openPop() {
|
|
// console.log("打开heath弹窗", cc.fx.GameConfig.GM_INFO.hp);
|
|
NumberToImage.numberToImageNodes(this.getHealthRefillPrice(), 35, 15, "button_", this.coin, true);
|
|
}
|
|
|
|
private getTodayKey(): string {
|
|
const now = new Date();
|
|
const month = (now.getMonth() + 1).toString().padStart(2, "0");
|
|
const day = now.getDate().toString().padStart(2, "0");
|
|
return `${now.getFullYear()}-${month}-${day}`;
|
|
}
|
|
|
|
private getHealthRefillStorageKey(): string {
|
|
const uid = cc.fx.GameConfig.GM_INFO.uid || "guest";
|
|
return HEALTH_REFILL_STORAGE_PREFIX + uid;
|
|
}
|
|
|
|
private getHealthRefillRecord(): { date: string, count: number } {
|
|
const today = this.getTodayKey();
|
|
const saved = cc.fx.StorageMessage.getStorage(this.getHealthRefillStorageKey());
|
|
if (!saved || saved.date !== today) {
|
|
return { date: today, count: 0 };
|
|
}
|
|
|
|
const count = Math.max(0, Math.floor(Number(saved.count) || 0));
|
|
return { date: today, count };
|
|
}
|
|
|
|
private getHealthRefillPrice(count: number = this.getHealthRefillRecord().count): number {
|
|
return HEALTH_REFILL_PRICES[Math.min(count, HEALTH_REFILL_PRICES.length - 1)];
|
|
}
|
|
|
|
private recordHealthRefill(record: { date: string, count: number }) {
|
|
cc.fx.StorageMessage.setStorage(this.getHealthRefillStorageKey(), {
|
|
date: record.date,
|
|
count: record.count + 1
|
|
});
|
|
}
|
|
startGame() {
|
|
// 获取场景中的 JiaZai 组件
|
|
const jiazaiNode = cc.find("Canvas"); // 假设 JiaZai 挂在 Canvas 节点
|
|
if (jiazaiNode) {
|
|
const jiazaiComp = jiazaiNode.getComponent(JiaZai);
|
|
if (jiazaiComp) {
|
|
jiazaiComp.startGame();
|
|
} else {
|
|
console.warn("JiaZai 组件未找到");
|
|
}
|
|
} else {
|
|
console.warn("JiaZai 节点未找到");
|
|
}
|
|
this.closePop();
|
|
}
|
|
|
|
//购买体力
|
|
buyHeath() {
|
|
if (!this.btn_Touch) {
|
|
return;
|
|
}
|
|
this.btn_Touch = false;
|
|
//获取当前体力
|
|
let curHeath = cc.fx.GameConfig.GM_INFO.hp;
|
|
//获取当前金币
|
|
let curCoin = cc.fx.GameConfig.GM_INFO.coin;
|
|
const refillRecord = this.getHealthRefillRecord();
|
|
const refillPrice = this.getHealthRefillPrice(refillRecord.count);
|
|
if (curCoin >= refillPrice) {
|
|
this.switchButtons[1].active = false;
|
|
cc.fx.GameTool.changeCoin(-refillPrice);
|
|
const data = {
|
|
change_reason: "首页购买体力",
|
|
id: (1001 + ""),
|
|
num: -refillPrice
|
|
}
|
|
cc.fx.GameTool.shushu_Track("resource_cost", data);
|
|
cc.fx.GameConfig.GM_INFO.hp = cc.fx.GameConfig.GM_INFO.hp_Max;
|
|
cc.fx.GameConfig.GM_INFO.min_Time = 0;
|
|
this.recordHealthRefill(refillRecord);
|
|
const parentNode = cc.find("Canvas"); // 假设 JiaZai 挂在 Canvas 节点
|
|
if (parentNode) {
|
|
const jiazaiComp = parentNode.getComponent(JiaZai);
|
|
if (jiazaiComp) {
|
|
jiazaiComp.setHealthInfo(false);
|
|
jiazaiComp.updateCoin();
|
|
}
|
|
}
|
|
this.switchButtons[0].active = true;
|
|
cc.fx.GameTool.setUserHealth(0, (data) => {
|
|
cc.fx.GameTool.getHealth(null);
|
|
})
|
|
const buyData = {
|
|
item_id: "refill_health",
|
|
item_num: 3,
|
|
item_price: refillPrice,
|
|
cost_type: "gold"
|
|
}
|
|
// console.log("____________即将上传Shop_buy", buyData);
|
|
cc.fx.GameTool.shushu_Track("shop_buy", buyData);
|
|
|
|
}
|
|
else {
|
|
MiniGameSdk.API.showToast("金币不足,无法购买体力");
|
|
setTimeout(() => {
|
|
this.btn_Touch = true;
|
|
this.openShop();
|
|
}, 500);
|
|
}
|
|
}
|
|
|
|
//金币不够购买金币
|
|
openShop() {
|
|
if (!cc.fx.GameConfig.GM_INFO.canIos) {
|
|
return;
|
|
}
|
|
//获取场景中的 JiaZai 组件
|
|
const parentNode = cc.find("Canvas"); // 假设 JiaZai 挂在 Canvas 节点
|
|
if (parentNode) {
|
|
const jiazaiComp = parentNode.getComponent(JiaZai);
|
|
if (jiazaiComp) {
|
|
jiazaiComp.openShop();
|
|
}
|
|
}
|
|
}
|
|
|
|
//关闭弹窗
|
|
closePop() {
|
|
this.btn_Touch = true;
|
|
//销毁预制体
|
|
// console.log("关闭heath弹窗");
|
|
//关闭计时器
|
|
// 获取场景中的 JiaZai 组件
|
|
const jiazaiNode = cc.find("Canvas"); // 假设 JiaZai 挂在 Canvas 节点
|
|
if (jiazaiNode) {
|
|
const jiazaiComp = jiazaiNode.getComponent(JiaZai);
|
|
if (jiazaiComp) {
|
|
jiazaiComp.stopHeathTimeCutDown();
|
|
} else {
|
|
console.warn("JiaZai 组件未找到");
|
|
}
|
|
} else {
|
|
console.warn("JiaZai 节点未找到");
|
|
}
|
|
this.heath.destroy();
|
|
}
|
|
|
|
// update (dt) {}
|
|
}
|