import { mLogger } from "../common/Logger"; import { _decorator, Node, Prefab, instantiate, Vec3 } from "cc"; import { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS"; import { CCComp } from "../../../../extensions/oops-plugin-framework/assets/module/common/CCComp"; import { SkillBoxComp } from "./SkillBoxComp"; import { oops } from "db://oops-framework/core/Oops"; import { GameEvent } from "../common/config/GameEvent"; import { smc } from "../common/SingletonModuleComp"; const { ccclass, property } = _decorator; interface SkillBoxSlot { x: number; y: number; used: boolean; node: Node | null; } /** 视图层对象 */ @ccclass('MissSkillsComp') @ecs.register('MissSkillsComp', false) export class MissSkillsComp extends CCComp { private debugMode: boolean = true; @property({type: Prefab}) private skill_box: Prefab = null; private slots: SkillBoxSlot[] = [ { x: -320, y: 240, used: false, node: null }, { x: -240, y: 240, used: false, node: null }, { x: -160, y: 240, used: false, node: null }, { x: -80, y: 240, used: false, node: null }, { x: 0, y: 240, used: false, node: null }, { x: -320, y: 320, used: false, node: null }, { x: -240, y: 320, used: false, node: null }, { x: -160, y: 320, used: false, node: null }, { x: -80, y: 320, used: false, node: null }, { x: 0, y: 320, used: false, node: null }, ]; onLoad() { oops.message.on(GameEvent.UseSkillCard, this.onUseSkillCard, this); oops.message.on(GameEvent.RemoveSkillBox, this.onRemoveSkillBox, this); } onDestroy() { oops.message.off(GameEvent.UseSkillCard, this.onUseSkillCard, this); oops.message.off(GameEvent.RemoveSkillBox, this.onRemoveSkillBox, this); } private onRemoveSkillBox(event: string, args: any) { const node = args as Node; let removed = false; for (let i = 0; i < this.slots.length; i++) { if (this.slots[i].node === node) { this.slots[i].used = false; this.slots[i].node = null; removed = true; break; } } if (removed) { this.rearrangeSlots(); } } private rearrangeSlots() { const validNodes: Node[] = []; for (let i = 0; i < this.slots.length; i++) { if (this.slots[i].used && this.slots[i].node && this.slots[i].node.isValid) { validNodes.push(this.slots[i].node); } this.slots[i].used = false; this.slots[i].node = null; } for (let i = 0; i < validNodes.length; i++) { if (i < this.slots.length) { this.slots[i].used = true; this.slots[i].node = validNodes[i]; validNodes[i].setPosition(new Vec3(this.slots[i].x, this.slots[i].y, 0)); } } } private onUseSkillCard(event: string, args: any) { const payload = args ?? event; const uuid = Number(payload?.uuid ?? 0); const card_lv = Math.max(1, Math.floor(Number(payload?.card_lv ?? 1))); if (!uuid) return; this.addSkill(uuid, card_lv); } start() { } addSkill(uuid: number, card_lv: number) { var parent = smc.map.MapView.scene.entityLayer!.node!.getChildByName("SKILL")!; if (!this.skill_box) { mLogger.error(this.debugMode, "MissSkillsComp", "skill_box prefab not set"); return; } const emptyIndex = this.slots.findIndex(slot => !slot.used); if (emptyIndex === -1) { mLogger.warn(this.debugMode, "MissSkillsComp", "skill_box slots are full"); return; } const node = instantiate(this.skill_box); node.parent = parent; node.setPosition(new Vec3(this.slots[emptyIndex].x, this.slots[emptyIndex].y, 0)); this.slots[emptyIndex].used = true; this.slots[emptyIndex].node = node; const comp = node.getComponent(SkillBoxComp) || node.addComponent(SkillBoxComp); comp.init(uuid, card_lv); } /** 视图对象通过 ecs.Entity.remove(ModuleViewComp) 删除组件是触发组件处理自定义释放逻辑 */ reset() { this.node.destroy(); } }