import { _decorator, Component, Node, Label, v3 } from 'cc'; import { DataSystem } from '../core/data/DataSystem'; import { ConfigData } from '../data/ConfigData'; import { UserData } from '../data/UserData'; import { BattleData } from './BattleData'; import { HeroSlotData } from './HeroSlotData'; const { ccclass, property } = _decorator; @ccclass('HeroSlot') export class HeroSlot extends Component { @property({ tooltip: "插槽ID" }) slotID: number = 0; @property({ type: Node, tooltip: "锁定状态" }) lock: Node; @property({ type: Node, tooltip: "空闲状态" }) empty: Node; @property({ type: Node, tooltip: "满状态" }) full: Node; @property({ type: Label, tooltip: "关卡文本" }) unlockLabel: Label; @property({ type: Node, tooltip: "上层结构" }) hero: Node; private config: Array; private data: HeroSlotData; public start() { this.data = new HeroSlotData(); DataSystem.regeditData(this.data); DataSystem.getData(BattleData).slots.set(this.slotID, this.data); this.config = DataSystem.getData(ConfigData)["serverConfig"]["embattle_open"].split(","); this.data.node = this.node; DataSystem.getData(UserData).lv = DataSystem.getData(UserData).lv; } update() { DataSystem.watch(UserData, "lv") && this.setLevel(); DataSystem.watch(this.data, "_state") && this.setState(); this.heroTargetChange(); } /**根据等级判断插槽是否解锁 */ private setLevel(): void { console.log("----------------------------------------观察到lv变化:"+DataSystem.getData(UserData).lv); if (this.data._state == HeroSlotState.lock) { if (DataSystem.getData(UserData).lv >= parseInt(this.config[this.slotID - 1])) { this.data._state = HeroSlotState.empty; } else { this.data._state = HeroSlotState.lock; } this.setState(); } } /**设置插槽状态 */ private setState(): void { this.lock.active = this.data._state == HeroSlotState.lock; this.empty.active = this.data._state == HeroSlotState.empty; this.unlockLabel.string = `${this.config[this.slotID - 1]}关`; } private heroTargetChange(): void { if (this.data._state == HeroSlotState.full) { let target = DataSystem.getData(BattleData)._monsters[DataSystem.getData(BattleData).heros.get(this.slotID).target]; if (target && target.node && target.node.position.x) { this.hero.scale = target.node.position.x > this.node.position.x ? v3(-1, 1, 1) : v3(1, 1, 1); this.data._direction = this.hero.scale.x; } } } } export enum HeroSlotState { lock, empty, full }