cb/assets/gacha_bundle/script/GachaManager.ts
2026-04-09 19:07:54 +08:00

232 lines
9.1 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import JiaZai from "../../Script/JiaZai";
import Utils from "../../Script/module/Pay/Utils";
import { MiniGameManager } from "../../Script/Sdk/MiniGameManager";
import { MiniGameSdk } from "../../Script/Sdk/MiniGameSdk";
const { ccclass, property } = cc._decorator;
//排行榜
@ccclass
export default class GachaManager extends cc.Component {
@property(cc.Node) //用户头像换图
avatar: cc.Node = null;
@property({ type: [cc.SpriteFrame], tooltip: "头像图片" })
icon: Array<cc.SpriteFrame> = [];
@property({ type: [cc.SpriteFrame], tooltip: "头像稀有度" })
kuang: Array<cc.SpriteFrame> = [];
avatarData: any = null;
own = 0;
reward = 0;
onLoad() {
this.avatarData = [];
this.own = 0;
this.reward = 0;
// this.init();
}
//初始化数据
init(data, film) {
this.avatarData = [];
this.own = 0;
console.log("222222初始化数据", data);
if (data) {
this.avatarData = data;
this.own = this.avatarData.length;
}
for (let i = 0; i < this.avatarData.length; i++) {
let iphone = this.avatarData[i] - 19;
this.avatar.children[iphone].getChildByName("own").active = true;
}
let botoom = this.node.getChildByName("bottom");
botoom.getChildByName("no").active = false;
botoom.getChildByName("yes").active = false;
if (this.own >= 8) {
botoom.getChildByName("total").active = false;
botoom.active = false;
this.node.getChildByName("finishi").active = true;
}
else {
let cost = this.getCostFilm();
botoom.getChildByName("total").getComponent(cc.Label).string = cost.toString();
console.log("抽卡所需film", cost);
if (film >= cost) {
botoom.getChildByName("yes").active = true;
console.log("有足够film");
botoom.getChildByName("yes").getComponent(cc.Label).string = film.toString();
}
else {
console.log("没有足够film");
botoom.getChildByName("no").active = true;
botoom.getChildByName("no").getComponent(cc.Label).string = film.toString();
}
console.log("拥有film", film);
}
}
getCostFilm() {
let film = [5, 10, 25, 35, 55, 70, 100, 120];
if (this.own > film.length)
this.own = film.length - 1;
let count = film[this.own];
return count;
}
closeGacha() {
const jiazaiNode = cc.find("Canvas"); // 假设 JiaZai 挂在 Canvas 节点
const jiazaiComp = jiazaiNode.getComponent(JiaZai);
if (jiazaiComp) {
jiazaiComp.closeGacha();
} else {
console.log("无法获取JiaZai组件");
}
}
playGacha() {
if (this.own >= 8) {
MiniGameSdk.API.showToast("已拥有当期所有头像");
return;
}
else {
let cost = this.getCostFilm();
if (cc.fx.GameConfig.GM_INFO.film < cost) {
MiniGameSdk.API.showToast("代币不足,闯关获取更多代币!");
return;
}
}
this.openLoad();
Utils.getGacha(res => {
console.log("抽卡结果", res);
if (res.code == 1) {
console.log("抽卡成功");
cc.fx.GameConfig.GM_INFO.film = res.data.film;
console.log("所剩余代币", cc.fx.GameConfig.GM_INFO.film);
let headArr = cc.fx.GameTool.getAvatarData(res.data.headArr);
this.init(headArr, cc.fx.GameConfig.GM_INFO.film);
this.openReward(res.data.reward);
cc.fx.GameTool.shushu_Track("avatar_gacha", {
pool_id: "spring",
pull_count: (this.own + 1),
film_count: (8 - this.own),
film_left: (8 - this.own - 1),
is_success: true,
result: "spring_" + res.data.reward.toString(),
});
}
else if (res.code == 0) {
MiniGameSdk.API.showToast(res.msg);
cc.fx.GameTool.shushu_Track("avatar_gacha", {
pool_id: "spring",
pull_count: (this.own + 1),
film_count: (8 - this.own),
film_left: (8 - this.own),
is_success: false,
result: "",
});
}
else {
MiniGameSdk.API.showToast("网络异常,请稍后再试");
cc.fx.GameTool.shushu_Track("avatar_gacha", {
pool_id: "spring",
pull_count: (this.own + 1),
film_count: (8 - this.own),
film_left: (8 - this.own),
is_success: false,
result: "",
});
}
this.closeLoad();
});
// let cost = this.getCostFilm();
// if (cc.fx.GameConfig.GM_INFO.film >= cost) {
// }
}
openReward(data) {
this.reward = data;
this.node.getChildByName("geReward").active = true;
this.node.getChildByName("geReward").getChildByName("r").zIndex = 100;
this.node.getChildByName("geReward").getChildByName("sr").zIndex = 100;
this.node.getChildByName("geReward").getChildByName("ssr").zIndex = 100;
this.node.getChildByName("geReward").getChildByName("r").active = false;
this.node.getChildByName("geReward").getChildByName("sr").active = false;
this.node.getChildByName("geReward").getChildByName("ssr").active = false;
this.node.getChildByName("geReward").getChildByName("btn").active = false;
let kuang = 0;
if (data == 24 || data == 25)
kuang = 1;
else if (data == 26)
kuang = 2;
else
kuang = 0;
this.node.getChildByName("geReward").getChildByName("paper").getChildByName("bg").getComponent(cc.Sprite).spriteFrame = this.kuang[kuang];
this.node.getChildByName("geReward").getChildByName("paper").getChildByName("icon").getComponent(cc.Sprite).spriteFrame = this.icon[this.reward - 19];
this.node.getChildByName("geReward").getChildByName("paper").opacity = 0;
this.node.getChildByName("geReward").getChildByName("dayin1").opacity = 0;
this.node.getChildByName("geReward").getChildByName("dayin2").opacity = 0;
this.node.getChildByName("geReward").getChildByName("dayin1").zIndex = 2;
this.node.getChildByName("geReward").getChildByName("dayin2").zIndex = 4;
this.node.getChildByName("geReward").getChildByName("paper").getChildByName("bg").getComponent("CardFlipEffect").init();
this.node.getChildByName("geReward").getChildByName("paper").getChildByName("icon").getComponent("CardFlipEffect").init();
cc.tween(this.node.getChildByName("geReward").getChildByName("paper"))
.to(0.2, { opacity: 255 })
.start();
cc.tween(this.node.getChildByName("geReward").getChildByName("dayin1"))
.to(0.2, { opacity: 255 })
.start();
cc.tween(this.node.getChildByName("geReward").getChildByName("dayin2"))
.to(0.2, { opacity: 255 })
.start();
}
showReward() {
this.node.getChildByName("geReward").getChildByName("btn").active = true;
let actionName = cc.fx.GameTool.getActionName(this.reward.toString());
if (this.reward < 24) {
this.node.getChildByName("geReward").getChildByName("r").active = true;
cc.fx.GameTool.loadSpineSimple(this.node.getChildByName("geReward").getChildByName("r").getChildByName("sp"), actionName);
}
else if (this.reward == 24 || this.reward == 25) {
this.node.getChildByName("geReward").getChildByName("sr").active = true;
cc.fx.GameTool.loadSpineSimple(this.node.getChildByName("geReward").getChildByName("sr").getChildByName("sp"), actionName);
if (this.reward == 24) {
this.node.getChildByName("geReward").getChildByName("sr").getChildByName("sp").y = 60
} else {
this.node.getChildByName("geReward").getChildByName("sr").getChildByName("sp").y = 35
}
}
else if (this.reward == 26) {
this.node.getChildByName("geReward").getChildByName("ssr").active = true;
cc.fx.GameTool.loadSpineSimple(this.node.getChildByName("geReward").getChildByName("ssr").getChildByName("sp"), actionName);
}
}
closeReward() {
this.node.getChildByName("geReward").active = false;
}
openLoad() {
this.node.getChildByName("Loading").active = true;
this.node.getChildByName("Loading").getChildByName("load").stopAllActions();
this.node.getChildByName("Loading").getChildByName("load").runAction(cc.rotateTo(2, 1080).repeatForever());
}
closeLoad() {
this.node.getChildByName("Loading").active = false;
}
start() {
}
}