消消方块阵换皮表情
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

99 lines
3.2 KiB

import { assetManager, Layers, Node, Sprite, SpriteFrame } from 'cc';
import { SpriteLoad } from '../../mx/components/SpriteLoad';
import { GBundle, GPath } from '../game/ConfigRes';
/** hand 为 Sprite(UIRenderer),须走 UI_2D;勿与方块 battleScene 层同步 */
export const ROLE_HAND_LAYER = Layers.Enum.UI_2D;
/** SpriteLoad.onLoad 可能尚未执行,需手动绑定 Sprite 否则 setSprite 会直接 return */
function ensureSpriteLoadReady(sl: SpriteLoad, handNode: Node): void {
if (sl.spComp) return;
sl.spComp = handNode.getComponent(Sprite);
}
function loadHandSpriteFrame(handName: string, onDone: (sf: SpriteFrame | null) => void): void {
const cached = gg.res.getRes<SpriteFrame>(handName, 'hand/', GBundle.Items, SpriteFrame);
if (cached) {
onDone(cached);
return;
}
const path = `${GPath.Hand(handName)}/spriteFrame`;
const loadFromBundle = (bundle: assetManager.Bundle | null) => {
if (!bundle) {
onDone(null);
return;
}
const hit = bundle.get(path, SpriteFrame);
if (hit) {
onDone(hit);
return;
}
bundle.load(path, SpriteFrame, (err, sf) => {
if (err) {
console.warn('[roleHand] load sprite failed:', path, err);
onDone(null);
return;
}
onDone(sf);
});
};
const bundle = assetManager.getBundle(GBundle.Items);
if (bundle) {
loadFromBundle(bundle);
return;
}
assetManager.loadBundle(GBundle.Items, (err, b) => {
if (err) {
console.warn('[roleHand] load bundle failed:', GBundle.Items, err);
onDone(null);
return;
}
loadFromBundle(b);
});
}
/** 隐藏角色 Spine(anim),仅保留 hand 等其它子节点 */
export function hideRoleAnim(roleNode: Node | null): void {
if (!roleNode?.isValid) return;
const animNode = roleNode.getChildByName('anim');
if (animNode?.isValid) animNode.active = false;
}
/** 按方块 prefab 上 hand 节点 + handName 设置贴图(icons/hand) */
export function applyRoleHand(hostNode: Node | null, handName?: string): void {
if (!hostNode?.isValid || !handName) return;
hideRoleAnim(hostNode);
const handNode = hostNode.getChildByName('hand');
if (!handNode?.isValid) {
return;
}
handNode.active = true;
handNode.layer = ROLE_HAND_LAYER;
handNode.setSiblingIndex(hostNode.children.length - 1);
const sl = handNode.getComponent(SpriteLoad);
const tryLoad = () => {
if (!handNode.isValid || !hostNode.isValid) return;
handNode.layer = ROLE_HAND_LAYER;
if (sl?.isValid) {
ensureSpriteLoadReady(sl, handNode);
if (sl.spComp) {
sl.setSprite(GPath.Hand(handName), GBundle.Items);
return;
}
}
loadHandSpriteFrame(handName, (sf) => {
if (!sf || !handNode.isValid) return;
const sp = handNode.getComponent(Sprite);
if (sp) sp.spriteFrame = sf;
});
};
tryLoad();
if (sl?.isValid) {
sl.scheduleOnce(tryLoad, 0);
}
}