cb/assets/Script/JiaZai.ts
2025-07-02 16:41:42 +08:00

335 lines
12 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Learn TypeScript:
// - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
// - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
// - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
import GameManager from "./GameManager";
import NumberToImage from "./NumberToImage";
import { MiniGameSdk } from "./Sdk/MiniGameSdk";
import { LQCollideSystem } from "./lq_collide_system/lq_collide_system";
const { ccclass, property } = cc._decorator;
@ccclass
export default class NewClass extends cc.Component {
@property(cc.Node)
node1: cc.Node = null;
@property(cc.Node)
node2: cc.Node = null;
@property(cc.Node)
node3: cc.Node = null;
@property({ type: [cc.SpriteAtlas], tooltip: "方块颜色" })
Block_Color: Array<cc.SpriteAtlas> = [];
@property(cc.EditBox)
custom: cc.EditBox = null;
@property(cc.Node)
level: cc.Node = null;
@property(cc.Node)
coin: cc.Node = null;
// 健康值
@property(cc.Prefab)
health: cc.Prefab = null;
@property(cc.Node)
Stamina: cc.Node = null;
scheduleCallback: any;
private lastPauseClickTime: number = 0; // 用于记录上次点击的时间戳
@property(cc.Node)
setUi: cc.Node = null;
// LIFE-CYCLE CALLBACKS:
onLoad() {
cc.game.setFrameRate(63);
LQCollideSystem.is_enable = true;
// console.log("加载关卡配置2");
// window.initMgr();
GameManager._instance.Block_Color = this.Block_Color;
if (cc.fx.GameConfig.GM_INFO.first) {
console.log("————————准备注册事件", cc.fx.GameConfig.GM_INFO.openid);
if (cc.fx.GameConfig.GM_INFO.openid != "") {
console.log("————————发送注册事件");
cc.fx.GameTool.shushu_Track("register");
}
cc.fx.AudioManager._instance.playEffect("zhuan1", null);
this.node.getChildByName("zhuanchang").active = true;
this.node.getChildByName("zhuanchang").getComponent(sp.Skeleton).setAnimation(1, "up", false);
setTimeout(() => {
cc.fx.GameConfig.GM_INFO.first = false;
cc.director.loadScene("GameScene");
}, 1000);
}
}
start() {
// console.log("已经进入Home界面");
// console.log("金币",cc.fx.GameConfig.GM_INFO.coin);
// console.log("关卡",cc.fx.GameConfig.GM_INFO.level+1);
cc.fx.GameTool.getHealth(null);
// cc.fx.GameConfig.LEVEL_INFO_init(false,0);
setTimeout(() => {
NumberToImage.numberToImageNodes((cc.fx.GameConfig.GM_INFO.level + 1), 25, 15, "level_", this.level, false);
NumberToImage.numberToImageNodes(cc.fx.GameConfig.GM_INFO.coin, 30, 15, "coin_", this.coin, true);
this.setHealthInfo();
}, 0);
if (cc.fx.GameConfig.GM_INFO.gameState) {
this.node.getChildByName("zhuanchang").active = true;
setTimeout(() => {
cc.fx.AudioManager._instance.playEffect("zhuan2", null);
this.node.getChildByName("zhuanchang").getComponent(sp.Skeleton).setAnimation(1, "down", false);
this.node.getChildByName("zhuanchang").getComponent(sp.Skeleton).setCompleteListener((entry) => {
if (entry.animation.name === "down" && !cc.fx.GameConfig.GM_INFO.first) {
// 动画播放结束后执行的逻辑
this.node.getChildByName("zhuanchang").active = false;
}
});
}, 500);
}
else {
if (!cc.fx.GameConfig.GM_INFO.first)
this.node.getChildByName("zhuanchang").active = false;
}
//打开heath弹窗
this.openHeath();
}
//打开heath弹窗,创建预制体
openHeath() {
let health = cc.instantiate(this.health);
this.node.addChild(health);
let heathPop =health.getComponent("heathnum").coin ;
heathPop.scale = 0.7;
let swich = health.getComponent("heathnum").switchNode;
let spriteFrames = health.getComponent("heathnum").spriteFrames;
if(cc.fx.GameConfig.GM_INFO.hp <= 0) {
swich.getComponent(cc.Sprite).spriteFrame =spriteFrames[1];
}else {
swich.getComponent(cc.Sprite).spriteFrame = spriteFrames[0];
}
NumberToImage.numberToImageNodes(cc.fx.GameConfig.GM_INFO.hp, 40, 20, "time_", heathPop, true);
}
//开始倒计时
startTimeCutDown() {
this.scheduleCallback = function () {
if (this.pause) return;
if (cc.fx.GameConfig.GM_INFO.min_Time <= 0) {
this.stopTimeCutDown();
var timeTemp = cc.fx.GameTool.getTimeMargin(cc.fx.GameConfig.GM_INFO.min_Time);
this.Stamina.getChildByName("time").getComponent(cc.Label).string = timeTemp;
cc.fx.GameTool.setUserHealth(1, (data) => {
cc.fx.GameTool.getHealth(null);
this.setHealthInfo();
})
}
else {
cc.fx.GameConfig.GM_INFO.min_Time -= 1;
var timeTemp = cc.fx.GameTool.getTimeMargin(cc.fx.GameConfig.GM_INFO.min_Time);
this.Stamina.getChildByName("time").getComponent(cc.Label).string = timeTemp;
}
}.bind(this);
this.schedule(this.scheduleCallback, 1);
}
// 停止倒计时
stopTimeCutDown() {
if (this.scheduleCallback) {
this.unschedule(this.scheduleCallback);
}
}
setHealthInfo() {
if (cc.fx.GameConfig.GM_INFO.hp == 5) {
this.Stamina.getChildByName("man").active = true;
this.Stamina.getChildByName("health").active = false;
this.Stamina.getChildByName("time").active = false;
}
else {
this.Stamina.getChildByName("man").active = false;
this.Stamina.getChildByName("health").active = true;
NumberToImage.numberToImageNodes((cc.fx.GameConfig.GM_INFO.hp), 25, 15, "coin_", this.Stamina.getChildByName("health"), false);
this.Stamina.getChildByName("time").active = true;
if (cc.fx.GameConfig.GM_INFO.min_Time != 0) {
let time = cc.fx.GameTool.getTimeMargin(cc.fx.GameConfig.GM_INFO.min_Time);
this.Stamina.getChildByName("time").getComponent(cc.Label).string = time;
this.startTimeCutDown();
}
}
}
startGame() {
cc.fx.AudioManager._instance.playEffect("anniu_Big", null);
if (cc.fx.GameConfig.GM_INFO.hp < 1) {
MiniGameSdk.API.showToast("体力值不足");
return;
}
if (this.node.getChildByName("Load").getChildByName("startBtn").getComponent("btnControl")._touch) {
this.node.getChildByName("Load").getChildByName("startBtn").getComponent("btnControl").setTouch(false);
// if(this.custom.string != ""){
// cc.fx.GameConfig.GM_INFO.level = parseInt(this.custom.string) - 1;
// // cc.fx.StorageMessage.setStorage("level",cc.fx.GameConfig.GM_INFO.level.toString());
// cc.fx.GameConfig.LEVEL_INFO_init(true);
// }
// else{
cc.fx.AudioManager._instance.playEffect("zhuan1", null);
this.node.getChildByName("zhuanchang").active = true;
this.node.getChildByName("zhuanchang").getComponent(sp.Skeleton).setAnimation(1, "up", false);
cc.fx.GameConfig.LEVEL_INFO_init(true, 1000);
// }
}
}
//打开商店
openShop() {
// 加载 shop bundle
cc.assetManager.loadBundle('shop', (err: Error, bundle: cc.AssetManager.Bundle) => {
bundle.load('prefab/shop', cc.Prefab, (err: Error, prefab: cc.Prefab) => {
if (err) {
cc.error(err.message || err);
return;
}
if (prefab) {
const shopNode = cc.instantiate(prefab);
this.node.addChild(shopNode);
console.log("shopNode parent:", shopNode.parent);
}
});
});
}
closeRank() {
this.node.getChildByName("Rank").active = false;
}
openRank() {
this.node.getChildByName("Rank").active = true;
}
openReward() {
this.node.getChildByName("Reward").active = true;
}
clickShop() {
// 假设已经获取到了 userId 和 productId
const userId = cc.fx.GameConfig.GM_INFO.openid;
const productId = '1';
console.log("即将创建createorder");
//@ts-ignore
wx.cloud.callFunction({
name: "createOrder",
data: {
"userId": userId,
"productId": productId,
},
success: (res) => {
// 取得云函数返回的订单信息
console.log("createOrder结果:", res.result);
const payment = res.result.paymentResult.payment;
console.log("payment:", payment);
// 调起微信客户端支付
//@ts-ignore
// 调起微信客户端支付
//@ts-ignore
wx.requestPayment({
// 时间戳,从 1970 年 1 月 1 日 00:00:00 至今的秒数,即当前的时间
timeStamp: payment.timeStamp,
// 随机字符串长度为32个字符以下
nonceStr: payment.nonceStr,
// 统一下单接口返回的 prepay_id 参数值
package: payment.package,
// 签名算法,暂支持 MD5、HMAC-SHA256
signType: payment.signType || 'MD5',
// 支付签名
paySign: payment.paySign,
success(res) {
/* 成功回调 */
console.log("支付成功", res);
},
fail(res) {
/* 失败回调 */
console.log("支付失败", res);
},
complete(res) {
console.log("支付完成", res);
}
});
},
});
}
openPause() {
cc.fx.AudioManager._instance.playEffect("anniu_little", null);
console.log(cc.fx.GameConfig.GM_INFO);
if (cc.fx.GameConfig.GM_INFO.openid == undefined) {
console.log(cc.fx.GameConfig.GM_INFO.openid);
cc.fx.GameConfig.GM_INFO.openid = "";
}
this.node.getChildByName("Pause").active = true;
this.node.getChildByName("Pause").getChildByName("openID").getComponent(cc.Label).string = cc.fx.GameConfig.GM_INFO.openid;
}
closePause() {
cc.fx.AudioManager._instance.playEffect("anniu_little", null);
this.node.getChildByName("Pause").active = false;
}
openSet() {
const now = Date.now();
if (now - this.lastPauseClickTime < 300) {
// 0.3秒内禁止再次点击
return;
}
this.lastPauseClickTime = now;
cc.fx.AudioManager._instance.playEffect("anniu_little", null);
if (cc.fx.GameConfig.GM_INFO.openid == undefined) {
cc.fx.GameConfig.GM_INFO.openid = "";
}
if (!this.setUi.active) {
// 第一次点击,打开并播放动画
this.setUi.active = true;
this.setUi.getComponent(cc.Animation).play();
} else {
// 再次点击,关闭节点
this.setUi.active = false;
}
}
closeReward() {
this.node.getChildByName("Reward").active = false;
}
openStamina() {
this.node.getChildByName("Stamina").active = true;
}
closeStamina() {
this.node.getChildByName("Stamina").active = false;
}
// update (dt) {}
}