import { _decorator, Component, Sprite, Label, SpriteFrame, Node, sp, color, instantiate, Prefab } from "cc"; import { DataSystem } from "../../core/data/DataSystem"; import { ResourceLoader } from "../../core/resourceManager/ResourceLoader"; import { DragSource } from "../../core/ui/drag/DragSource"; import { DragSystem } from "../../core/ui/drag/DragSystem"; import { DragTarget } from "../../core/ui/drag/DragTarget"; import { ConfigData } from "../../data/ConfigData"; import { UserData } from "../../data/UserData"; import { Fixbug } from "../../FixBug"; import { UserHero, UserHeroData } from "../UserHeroData"; import { Formation } from "./Formation"; import { FormationData } from "./FormationData"; import { FormationHeroItem } from "./FormationHeroItem"; import { HeroDragHelper } from "./HeroDragHelper"; const { ccclass, requireComponent, property } = _decorator; /** * 单个阵位 * @author 袁浩 */ @ccclass('FormationOne') @requireComponent(ResourceLoader) export class FormationOne extends Component { @property({ tooltip: "阵位编号(统帅位为5)" }) public id: number = 1; @property({ tooltip: "武将骨骼动画组件", type: sp.Skeleton }) public body: sp.Skeleton; @property({ tooltip: "武将名称", type: Label }) public nameLabel: Label; @property({ tooltip: "锁", type: Node }) public lock: Node; @property({ tooltip: "锁文本", type: Label }) public lockLabel: Label; @property({ tooltip: "台子(未拖入)", type: Node }) public baseNodeNormal: Node; @property({ tooltip: "台子(拖入)", type: Node }) public baseNodeActive: Node; @property({ tooltip: "拖拽生成的预制体", type: Prefab }) public dragPrefab: Prefab; public formation: Formation; private dragSource: DragSource; public helper: HeroDragHelper; public data: UserHero; start() { this.dragSource = this.getComponent(DragSource); let unlockLevel = parseInt(DataSystem.getData(ConfigData).get("serverConfig").embattle_open.split(",")[this.id - 1]) this.lock.active = DataSystem.getData(UserData).level < unlockLevel; this.lockLabel.string = `${unlockLevel}关`; // this.body.color = color(255, 255, 255, 0); this.baseNodeNormal.active = true; this.baseNodeActive.active = false; this.loadHero(); } public async onDrop(source: DragSource, target: DragTarget) { this.baseNodeNormal.active = true; this.baseNodeActive.active = false; !this.lock.active && this.formation.onDrop(source, target); } update() { if (DataSystem.watch(FormationData, this.id + "")) {//如果本阵位数据发生变化 this.loadHero(); } } public fixSpine() { Fixbug.fixSpine_active(this.body); } private loadHero() { let heroID = DataSystem.getData(FormationData).get(this.id); if (heroID) { this.data = DataSystem.getData(UserHeroData).get(heroID); this.updateUI(this.data); } else { this.nameLabel.string = ""; this.body.node.active = false; this.data = null; // this.body.skeletonData = null; } } public onDraging(source: DragSource, inTarget: boolean) { if (!this.lock.active) { this.baseNodeNormal.active = !inTarget; this.baseNodeActive.active = inTarget; } } public checkSame(source: DragSource, inTarget: boolean) { let helper = (source.getComponent(FormationHeroItem) || source.getComponent(FormationOne)).helper; if (inTarget && this.data && helper && helper.data.client.id == this.data.client.id) { this.nameLabel.node.active = false; this.body.node.active = false; } else { this.nameLabel.node.active = true; // this.body.color = color(255, 255, 255, !!DataSystem.getData(FormationData).get(this.id) ? 255 : 0); this.body.node.active = !!DataSystem.getData(FormationData).get(this.id) ? true : false; this.body.node.active && this.fixSpine(); } } private async updateUI(data: UserHero) { this.nameLabel.string = data.client.name; this.body.node.active = true; this.fixSpine(); this.body.skeletonData = await this.getComponent(ResourceLoader).load(`spine/hero/${data.client.hero_res}`, sp.SkeletonData); this.body.setAnimation(0, "idle", true); } public onSourceDrag(isInTarget: boolean) { this.helper && this.helper.node.destroy(); this.helper = null; if (!this.data) { return; } let dragNode = instantiate(this.dragPrefab); dragNode.parent = DragSystem.touchNode; this.helper = dragNode.getComponent(HeroDragHelper); this.helper.data = this.data; this.helper.onDrag(this.dragSource, this.data); } public onSourceDraging() { this.helper && (this.helper.onDraging(this.dragSource)); } public onSourceDrop() { this.helper && this.helper.node.destroy(); this.helper = null; } }