Files
MatchMaster/assets/gacha_bundle/script/GachaManager.ts
T
2026-10-23 11:36:16 +08:00

864 lines
31 KiB
TypeScript

import JiaZai from "../../Script/JiaZai";
import LoadingCatAnimation from "../../Script/LoadingCatAnimation";
import Utils from "../../Script/module/Pay/Utils";
import { getOwnedDrawableCats, getCatDrawCost } from "../../Script/module/Tool/CatDrawRules";
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(cc.Node) //用户头像换图
catNode: cc.Node = null;
@property({ type: [cc.SpriteFrame], tooltip: "头像图片" })
icon: Array<cc.SpriteFrame> = [];
@property({ type: [cc.SpriteFrame], tooltip: "头像稀有度" })
kuang: Array<cc.SpriteFrame> = [];
@property({ type: [cc.SpriteFrame], tooltip: "猫图片" })
cat_Icon: Array<cc.SpriteFrame> = [];
@property(cc.AudioClip)
buttonAudio: cc.AudioClip = null;
@property(cc.AudioClip)
dropAudio: cc.AudioClip = null;
@property(cc.AudioClip)
openAudio: cc.AudioClip = null;
avatarData: any = null;
catArr: any = null;
headArr: any = null;
own = 0;
cat = 0;
reward = 0;
phase = 2;
private _phaseCenterX: number = 0;
private _phaseSwitching: boolean = false;
private _phaseTweens: cc.Tween[] = [];
private _phaseTabTweens: cc.Tween[] = [];
private _phaseTabOrigin: { [key: string]: any } = {};
private readonly _phaseMoveDuration: number = 0.3;
private _showReward2Button = () => {
const rewardNode = this.node.getChildByName("getReward2");
const buttonNode = rewardNode && rewardNode.getChildByName("btn");
if (buttonNode) {
buttonNode.active = true;
}
};
onLoad() {
this.avatarData = [];
this.own = 0;
this.cat = 0;
this.reward = 0;
const phase2Node = this.node.getChildByName("Phase2");
const phase1Node = this.node.getChildByName("Phase1");
this.phase = phase1Node && phase2Node ? 2 : 1;
this._phaseCenterX = phase2Node ? phase2Node.x : (phase1Node ? phase1Node.x : 0);
this._cachePhaseTabLayout();
this._showPhaseImmediately(this.phase);
// this.init();
}
//初始化数据
init(data, film, catArr, keepCurrentPhase: boolean = false) {
this.avatarData = [];
this.own = 0;
this.cat = 0;
if (!keepCurrentPhase) {
this.phase = this._hasMultiPhaseLayout() ? 2 : 1;
}
else if (!this._hasMultiPhaseLayout()) {
// Gacha is the legacy single-phase avatar layout.
this.phase = 1;
}
this.catArr = getOwnedDrawableCats(catArr);
this.cat = this.catArr.length;
console.log("222222初始化数据", data, film, catArr);
cc.fx.GameConfig.GM_INFO.film = film;
if (data) {
this.avatarData = data;
this.own = this.avatarData.length;
}
this._refreshOwnedStates();
this._showPhaseImmediately(this.phase);
this._refreshPhaseCurrency(this.phase);
if (this.phase === 2) {
const phase2Node = this.node.getChildByName("Phase2");
const bottomNode = phase2Node && phase2Node.getChildByName("bottom");
const buttonNode = bottomNode && bottomNode.getChildByName("dayinBtn");
if (buttonNode) {
cc.tween(buttonNode)
.delay(1)
.to(0.5, { rotation: 0 })
.start();
}
}
}
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;
}
getCostFilm2() {
return getCatDrawCost(this.cat);
}
closeGacha() {
cc.fx.AudioManager._instance.playEffect("anniu_little", null);
const jiazaiNode = cc.find("Canvas"); // 假设 JiaZai 挂在 Canvas 节点
const jiazaiComp = jiazaiNode.getComponent(JiaZai);
if (jiazaiComp) {
jiazaiComp.closeGacha();
} else {
console.log("无法获取JiaZai组件");
}
}
playGacha() {
cc.fx.AudioManager._instance.playEffect("anniu_little", null);
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(null);
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);
const rewardId = Number(res.data.reward);
if (headArr.indexOf(rewardId) === -1) {
headArr.push(rewardId);
}
this.init(headArr, cc.fx.GameConfig.GM_INFO.film, this.catArr, true);
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) {
// }
}
playGacha2() {
const phase2Node = this.node.getChildByName("Phase2");
const bottomNode = phase2Node && phase2Node.getChildByName("bottom");
const btn = bottomNode && bottomNode.getChildByName("dayinBtn");
if (!phase2Node || !bottomNode || !btn) {
console.warn("playGacha2 is only available in the Gacha2 layout.");
return;
}
// 点击按钮的一瞬间播放抽猫专用按钮音效。
this.playGachaEffect("button");
if (this.cat >= 8) {
MiniGameSdk.API.showToast("已拥有当期所有猫咪");
return;
}
else {
let cost = this.getCostFilm2();
if (cc.fx.GameConfig.GM_INFO.film < cost) {
MiniGameSdk.API.showToast("代币不足,闯关获取更多代币!");
return;
}
}
btn.runAction(cc.rotateTo(0.8, 90));
this.openLoad("drop");
setTimeout(() => {
Utils.getCat(res => {
console.log("抽卡结果", res);
if (res.code == 1) {
console.log("抽卡成功");
cc.fx.GameConfig.GM_INFO.film = res.data.film;
cc.fx.GameConfig.GM_INFO.catArr = Array.isArray(res.data.catArr)
? res.data.catArr.slice()
: [];
console.log("所剩余代币", cc.fx.GameConfig.GM_INFO.film);
let catArr = cc.fx.GameTool.getCatData(res.data.catArr);
const rewardId = Number(res.data.reward);
if (catArr.indexOf(rewardId) === -1) {
catArr.push(rewardId);
}
this.init(this.avatarData, cc.fx.GameConfig.GM_INFO.film, catArr, true);
this.openReward2(res.data.reward);
cc.fx.GameTool.shushu_Track("avatar_gacha", {
pool_id: "summer1",
pull_count: (this.cat + 1),
film_count: (8 - this.cat),
film_left: (8 - this.cat - 1),
is_success: true,
result: "summer_" + res.data.reward.toString(),
});
}
else if (res.code == 0) {
MiniGameSdk.API.showToast(res.msg);
cc.fx.GameTool.shushu_Track("avatar_gacha", {
pool_id: "summer1",
pull_count: (this.cat + 1),
film_count: (8 - this.cat),
film_left: (8 - this.cat),
is_success: false,
result: "",
});
}
else {
MiniGameSdk.API.showToast("网络异常,请稍后再试");
cc.fx.GameTool.shushu_Track("avatar_gacha", {
pool_id: "summer1",
pull_count: (this.cat + 1),
film_count: (8 - this.cat),
film_left: (8 - this.cat),
is_success: false,
result: "",
});
}
this.closeLoad();
});
}, 1000);
}
/** 播放抽猫流程音效,并沿用游戏现有的音效开关。 */
public playGachaEffect(type: string) {
if (cc.fx.GameConfig.GM_INFO
&& cc.fx.GameConfig.GM_INFO.effectOpen === false) {
return;
}
let clip: cc.AudioClip = null;
if (type === "button") {
clip = this.buttonAudio;
}
else if (type === "drop") {
clip = this.dropAudio;
}
else if (type === "open") {
clip = this.openAudio;
}
if (!clip) {
return;
}
const audioManager = cc.fx.AudioManager && cc.fx.AudioManager._instance;
if (audioManager && audioManager.play) {
audioManager.play(clip, false, null, false);
}
else {
cc.audioEngine.playEffect(clip, false);
}
}
openReward(data) {
this.reward = data;
const rewardNode = this._getAvatarRewardNode();
const rNode = rewardNode && rewardNode.getChildByName("r");
const srNode = rewardNode && rewardNode.getChildByName("sr");
const ssrNode = rewardNode && rewardNode.getChildByName("ssr");
const buttonNode = rewardNode && rewardNode.getChildByName("btn");
const paperNode = rewardNode && rewardNode.getChildByName("paper");
const dayin1Node = rewardNode && rewardNode.getChildByName("dayin1");
const dayin2Node = rewardNode && rewardNode.getChildByName("dayin2");
const paperBgNode = paperNode && paperNode.getChildByName("bg");
const paperIconNode = paperNode && paperNode.getChildByName("icon");
if (!rewardNode || !rNode || !srNode || !ssrNode || !buttonNode
|| !paperNode || !dayin1Node || !dayin2Node || !paperBgNode || !paperIconNode) {
console.warn("Avatar reward nodes are incomplete.");
return;
}
rNode.zIndex = 100;
srNode.zIndex = 100;
ssrNode.zIndex = 100;
rNode.active = false;
srNode.active = false;
ssrNode.active = false;
buttonNode.active = false;
let kuang = 0;
if (data == 24 || data == 25)
kuang = 1;
else if (data == 26)
kuang = 2;
else
kuang = 0;
const bgSprite = paperBgNode.getComponent(cc.Sprite);
const iconSprite = paperIconNode.getComponent(cc.Sprite);
const bgFlipEffect = paperBgNode.getComponent("CardFlipEffect") as any;
const iconFlipEffect = paperIconNode.getComponent("CardFlipEffect") as any;
if (!bgSprite || !iconSprite || !bgFlipEffect || !iconFlipEffect) {
console.warn("Avatar reward components are incomplete.");
return;
}
bgSprite.spriteFrame = this.kuang[kuang];
iconSprite.spriteFrame = this.icon[this.reward - 19];
rewardNode.active = true;
paperNode.opacity = 0;
dayin1Node.opacity = 0;
dayin2Node.opacity = 0;
dayin1Node.zIndex = 2;
dayin2Node.zIndex = 4;
bgFlipEffect.init();
iconFlipEffect.init();
cc.tween(paperNode)
.to(0.2, { opacity: 255 })
.start();
cc.tween(dayin1Node)
.to(0.2, { opacity: 255 })
.start();
cc.tween(dayin2Node)
.to(0.2, { opacity: 255 })
.start();
}
openReward2(data) {
this.reward = data;
const rewardNode = this.node.getChildByName("getReward2");
const animeNode = rewardNode && rewardNode.getChildByName("gatchaAnime");
const buttonNode = rewardNode && rewardNode.getChildByName("btn");
const resultNode = animeNode && animeNode.getChildByName("4");
const resultSprite = resultNode && resultNode.getComponent(cc.Sprite);
if (!rewardNode || !animeNode || !buttonNode || !resultNode || !resultSprite) {
console.warn("Cat reward nodes are only available in the Gacha2 layout.");
return;
}
const wasActive = rewardNode.active;
this.unschedule(this._showReward2Button);
buttonNode.active = false;
console.log("具体奖励:", this.reward);
resultSprite.spriteFrame = this.cat_Icon[this.reward];
rewardNode.active = true;
// 正常的关闭后重开由 GachaDrawAnime.onEnable 自动重播;
// 如果重复调用时节点本来就是打开的,也主动从头重播。
if (wasActive) {
const anime = animeNode.getComponent("GachaDrawAnime") as any;
if (anime && anime.playAnime) {
anime.playAnime();
}
}
this.scheduleOnce(this._showReward2Button, 2);
}
showReward() {
const rewardNode = this._getAvatarRewardNode();
const buttonNode = rewardNode && rewardNode.getChildByName("btn");
const rNode = rewardNode && rewardNode.getChildByName("r");
const srNode = rewardNode && rewardNode.getChildByName("sr");
const ssrNode = rewardNode && rewardNode.getChildByName("ssr");
if (!rewardNode || !buttonNode || !rNode || !srNode || !ssrNode) {
console.warn("Avatar reward nodes are incomplete.");
return;
}
buttonNode.active = true;
let actionName = cc.fx.GameTool.getActionName(this.reward.toString());
if (this.reward < 24) {
const spineNode = rNode.getChildByName("sp");
if (!spineNode) {
return;
}
rNode.active = true;
cc.fx.GameTool.loadSpineSimple(spineNode, actionName);
}
else if (this.reward == 24 || this.reward == 25) {
const spineNode = srNode.getChildByName("sp");
if (!spineNode) {
return;
}
srNode.active = true;
cc.fx.GameTool.loadSpineSimple(spineNode, actionName);
if (this.reward == 24) {
spineNode.y = 60
} else {
spineNode.y = 35
}
}
else if (this.reward == 26) {
const spineNode = ssrNode.getChildByName("sp");
if (!spineNode) {
return;
}
ssrNode.active = true;
cc.fx.GameTool.loadSpineSimple(spineNode, actionName);
}
}
closeReward() {
cc.fx.AudioManager._instance.playEffect("anniu_little", null);
this.unschedule(this._showReward2Button);
const avatarRewardNode = this._getAvatarRewardNode();
const catRewardNode = this.node.getChildByName("getReward2");
if (avatarRewardNode) {
avatarRewardNode.active = false;
}
if (catRewardNode) {
catRewardNode.active = false;
}
}
openLoad(type) {
const loadingNode = this.node.getChildByName("Loading");
if (!loadingNode) {
console.warn("Loading nodes are incomplete.");
return;
}
if (type === "drop") {
LoadingCatAnimation.stop(loadingNode);
loadingNode.active = true;
loadingNode.opacity = 0;
}
else {
loadingNode.opacity = 255;
LoadingCatAnimation.play(loadingNode);
}
setTimeout(() => {
if (!this.node || !cc.isValid(this.node)) {
return;
}
const currentLoadingNode = this.node.getChildByName("Loading");
if (type === "drop" && currentLoadingNode && currentLoadingNode.active) {
currentLoadingNode.opacity = 255;
LoadingCatAnimation.play(currentLoadingNode);
}
}, 1000);
}
closeLoad() {
const loadingNode = this.node.getChildByName("Loading");
if (loadingNode) {
loadingNode.opacity = 255;
LoadingCatAnimation.stop(loadingNode);
}
const phase2Node = this.node.getChildByName("Phase2");
const bottomNode = phase2Node && phase2Node.getChildByName("bottom");
const buttonNode = bottomNode && bottomNode.getChildByName("dayinBtn");
if (buttonNode) {
buttonNode.rotation = 0;
}
}
start() {
}
changePhase(event, customEventData) {
cc.fx.AudioManager._instance.playEffect("anniu_little", null);
const phaseName = (customEventData || "").toString().toLowerCase();
const targetPhase = phaseName === "phase1" ? 1 : (phaseName === "phase2" ? 2 : 0);
if (targetPhase === 0) {
console.warn("changePhase收到无效参数:", customEventData);
return;
}
if (this._phaseSwitching || targetPhase === this.phase) {
return;
}
const phase1Node = this.node.getChildByName("Phase1");
const phase2Node = this.node.getChildByName("Phase2");
if (!phase1Node || !phase2Node) {
return;
}
this._stopPhaseTweens();
this._phaseSwitching = true;
this.phase = targetPhase;
this._refreshPhaseCurrency(targetPhase);
const offsetX = this._getPhaseOffsetX(phase1Node, phase2Node);
const incomingNode = targetPhase === 1 ? phase1Node : phase2Node;
const outgoingNode = targetPhase === 1 ? phase2Node : phase1Node;
const incomingStartX = this._phaseCenterX + (targetPhase === 1 ? -offsetX : offsetX);
const outgoingEndX = this._phaseCenterX + (targetPhase === 1 ? offsetX : -offsetX);
// Phase 根节点有全屏 Widget。先停用 Widget 并设置屏外坐标,
// 再激活节点,避免 Widget 在激活首帧把节点拉回中间造成闪烁。
this._setPhaseWidgetEnabled(incomingNode, false);
this._setPhaseWidgetEnabled(outgoingNode, false);
incomingNode.x = incomingStartX;
outgoingNode.x = this._phaseCenterX;
incomingNode.active = true;
outgoingNode.active = true;
const incomingTween: cc.Tween = cc.tween(incomingNode)
.to(this._phaseMoveDuration, { x: this._phaseCenterX }, { easing: "sineInOut" })
.call(() => {
incomingNode.x = this._phaseCenterX;
this._setPhaseWidgetEnabled(incomingNode, true);
this._phaseSwitching = false;
this._phaseTweens = [];
});
const outgoingTween: cc.Tween = cc.tween(outgoingNode)
.to(this._phaseMoveDuration, { x: outgoingEndX }, { easing: "sineInOut" })
.call(() => {
outgoingNode.x = outgoingEndX;
outgoingNode.active = false;
this._setPhaseWidgetEnabled(outgoingNode, true);
});
this._phaseTweens = [incomingTween, outgoingTween];
incomingTween.start();
outgoingTween.start();
this._playPhaseTabTweens(targetPhase);
}
private _showPhaseImmediately(targetPhase: number) {
const phase1Node = this.node.getChildByName("Phase1");
const phase2Node = this.node.getChildByName("Phase2");
if (!phase1Node || !phase2Node) {
return;
}
this._stopPhaseTweens();
const offsetX = this._getPhaseOffsetX(phase1Node, phase2Node);
this.phase = targetPhase === 1 ? 1 : 2;
this._setPhaseWidgetEnabled(phase1Node, false);
this._setPhaseWidgetEnabled(phase2Node, false);
if (this.phase === 1) {
phase1Node.x = this._phaseCenterX;
phase1Node.active = true;
phase2Node.x = this._phaseCenterX + offsetX;
phase2Node.active = false;
}
else {
phase2Node.x = this._phaseCenterX;
phase2Node.active = true;
phase1Node.x = this._phaseCenterX - offsetX;
phase1Node.active = false;
}
this._setPhaseWidgetEnabled(phase1Node, true);
this._setPhaseWidgetEnabled(phase2Node, true);
this._showPhaseTabsImmediately(this.phase);
}
private _getPhaseOffsetX(phase1Node: cc.Node, phase2Node: cc.Node): number {
return Math.max(
this.node.width,
cc.winSize.width,
phase1Node.width,
phase2Node.width
);
}
private _setPhaseWidgetEnabled(phaseNode: cc.Node, enabled: boolean) {
const widget = phaseNode.getComponent(cc.Widget);
if (!widget) {
return;
}
widget.enabled = enabled;
if (enabled && phaseNode.active) {
widget.updateAlignment();
}
}
private _stopPhaseTweens() {
for (let i = 0; i < this._phaseTweens.length; i++) {
if (this._phaseTweens[i]) {
this._phaseTweens[i].stop();
}
}
this._phaseTweens = [];
this._stopPhaseTabTweens();
this._phaseSwitching = false;
}
private _cachePhaseTabLayout() {
const tabNames = ["first", "two", "three"];
for (let i = 0; i < tabNames.length; i++) {
const node = this.node.getChildByName(tabNames[i]);
if (node) {
this._phaseTabOrigin[tabNames[i]] = {
x: node.x,
y: node.y,
width: node.width,
height: node.height,
scaleX: node.scaleX,
scaleY: node.scaleY
};
}
}
}
private _playPhaseTabTweens(targetPhase: number) {
const firstNode = this.node.getChildByName("first");
const twoNode = this.node.getChildByName("two");
const threeNode = this.node.getChildByName("three");
const firstOrigin = this._phaseTabOrigin.first;
const twoOrigin = this._phaseTabOrigin.two;
const threeOrigin = this._phaseTabOrigin.three;
if (!firstNode || !twoNode || !threeNode || !firstOrigin || !twoOrigin || !threeOrigin) {
return;
}
this._stopPhaseTabTweens();
firstNode.active = true;
twoNode.active = true;
threeNode.active = true;
const firstTarget = targetPhase === 1 ? twoOrigin : firstOrigin;
const twoTarget = targetPhase === 1 ? threeOrigin : twoOrigin;
const threeTarget = targetPhase === 1
? {
x: this._getPhaseTabRightOutX(threeNode),
y: threeOrigin.y,
width: threeOrigin.width,
height: threeOrigin.height,
scaleX: threeOrigin.scaleX,
scaleY: threeOrigin.scaleY
}
: threeOrigin;
const firstTween: cc.Tween = cc.tween(firstNode)
.to(this._phaseMoveDuration, firstTarget, { easing: "sineInOut" });
const twoTween: cc.Tween = cc.tween(twoNode)
.to(this._phaseMoveDuration, twoTarget, { easing: "sineInOut" });
const threeTween: cc.Tween = cc.tween(threeNode)
.to(this._phaseMoveDuration, threeTarget, { easing: "sineInOut" });
this._phaseTabTweens = [firstTween, twoTween, threeTween];
firstTween.start();
twoTween.start();
threeTween.start();
}
private _showPhaseTabsImmediately(targetPhase: number) {
const firstNode = this.node.getChildByName("first");
const twoNode = this.node.getChildByName("two");
const threeNode = this.node.getChildByName("three");
const firstOrigin = this._phaseTabOrigin.first;
const twoOrigin = this._phaseTabOrigin.two;
const threeOrigin = this._phaseTabOrigin.three;
if (!firstNode || !twoNode || !threeNode || !firstOrigin || !twoOrigin || !threeOrigin) {
return;
}
this._stopPhaseTabTweens();
this._applyPhaseTabState(firstNode, targetPhase === 1 ? twoOrigin : firstOrigin);
this._applyPhaseTabState(twoNode, targetPhase === 1 ? threeOrigin : twoOrigin);
this._applyPhaseTabState(threeNode, targetPhase === 1
? {
x: this._getPhaseTabRightOutX(threeNode),
y: threeOrigin.y,
width: threeOrigin.width,
height: threeOrigin.height,
scaleX: threeOrigin.scaleX,
scaleY: threeOrigin.scaleY
}
: threeOrigin);
}
private _applyPhaseTabState(node: cc.Node, state: any) {
node.x = state.x;
node.y = state.y;
node.width = state.width;
node.height = state.height;
node.scaleX = state.scaleX;
node.scaleY = state.scaleY;
}
private _getPhaseTabRightOutX(threeNode: cc.Node): number {
const viewWidth = Math.max(this.node.width, cc.winSize.width);
return this._phaseCenterX + viewWidth / 2 + threeNode.width * Math.abs(threeNode.scaleX) / 2 + 20;
}
private _stopPhaseTabTweens() {
for (let i = 0; i < this._phaseTabTweens.length; i++) {
if (this._phaseTabTweens[i]) {
this._phaseTabTweens[i].stop();
}
}
this._phaseTabTweens = [];
}
private _hasMultiPhaseLayout(): boolean {
return !!this.node.getChildByName("Phase1")
&& !!this.node.getChildByName("Phase2");
}
private _getPhaseContentNode(targetPhase: number): cc.Node {
const phaseNode = this.node.getChildByName(targetPhase === 1 ? "Phase1" : "Phase2");
if (phaseNode) {
return phaseNode;
}
// The A prefab has no phase wrappers; its avatar content is on the root.
if (targetPhase === 1
&& !this.node.getChildByName("Phase1")
&& !this.node.getChildByName("Phase2")) {
return this.node;
}
return null;
}
private _getAvatarRewardNode(): cc.Node {
// Gacha uses the historical typo "geReward"; Gacha2 uses "getReward".
return this.node.getChildByName("getReward")
|| this.node.getChildByName("geReward");
}
private _getLoadingPart(currentName: string, legacyName: string): cc.Node {
const loadingNode = this.node.getChildByName("Loading");
if (!loadingNode) {
return null;
}
return loadingNode.getChildByName(currentName)
|| loadingNode.getChildByName(legacyName);
}
private _refreshPhaseCurrency(targetPhase: number) {
const phaseNode = this._getPhaseContentNode(targetPhase);
if (!phaseNode) {
return;
}
const bottomNode = phaseNode.getChildByName("bottom");
if (!bottomNode) {
return;
}
const noNode = bottomNode.getChildByName("no");
const yesNode = bottomNode.getChildByName("yes");
const totalNode = bottomNode.getChildByName("total");
if (!noNode || !yesNode || !totalNode) {
return;
}
noNode.active = false;
yesNode.active = false;
const ownedCount = targetPhase === 1 ? this.own : this.cat;
const finishNode = phaseNode.getChildByName("finishi");
if (finishNode) {
finishNode.active = ownedCount >= 8;
}
if (ownedCount >= 8) {
totalNode.active = false;
if (targetPhase === 1) {
bottomNode.active = false;
}
return;
}
bottomNode.active = true;
totalNode.active = true;
const cost = targetPhase === 1 ? this.getCostFilm() : this.getCostFilm2();
const film = Number(cc.fx.GameConfig.GM_INFO.film) || 0;
const totalLabel = totalNode.getComponent(cc.Label);
if (totalLabel) {
totalLabel.string = cost.toString();
}
const filmNode = film >= cost ? yesNode : noNode;
filmNode.active = true;
const filmLabel = filmNode.getComponent(cc.Label);
if (filmLabel) {
filmLabel.string = film.toString();
}
}
private _refreshOwnedStates() {
this._setOwnedStates(this.avatar, this.avatarData, 19);
this._setOwnedStates(this.catNode, this.catArr, 4);
}
private _setOwnedStates(container: cc.Node, ownedData: any[], idOffset: number) {
if (!container) {
return;
}
for (let i = 0; i < container.childrenCount; i++) {
const ownNode = container.children[i].getChildByName("own");
if (ownNode) {
ownNode.active = false;
}
}
if (!ownedData) {
return;
}
for (let i = 0; i < ownedData.length; i++) {
const itemIndex = Number(ownedData[i]) - idOffset;
if (itemIndex < 0 || itemIndex >= container.childrenCount) {
continue;
}
const ownNode = container.children[itemIndex].getChildByName("own");
if (ownNode) {
ownNode.active = true;
}
}
}
}