HeroSlot.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { _decorator, Component, Node, Label, v3 } from 'cc';
  2. import { DataSystem } from '../core/data/DataSystem';
  3. import { ConfigData } from '../data/ConfigData';
  4. import { UserData } from '../data/UserData';
  5. import { BattleData } from './BattleData';
  6. import { HeroSlotData } from './HeroSlotData';
  7. const { ccclass, property } = _decorator;
  8. @ccclass('HeroSlot')
  9. export class HeroSlot extends Component {
  10. @property({ tooltip: "插槽ID" }) slotID: number = 0;
  11. @property({ type: Node, tooltip: "锁定状态" }) lock: Node;
  12. @property({ type: Node, tooltip: "空闲状态" }) empty: Node;
  13. @property({ type: Node, tooltip: "满状态" }) full: Node;
  14. @property({ type: Label, tooltip: "关卡文本" }) unlockLabel: Label;
  15. @property({ type: Node, tooltip: "上层结构" }) hero: Node;
  16. private config: Array<string>;
  17. private data: HeroSlotData;
  18. public start() {
  19. this.data = new HeroSlotData();
  20. DataSystem.regeditData(this.data);
  21. DataSystem.getData(BattleData).slots.set(this.slotID, this.data);
  22. this.config = DataSystem.getData(ConfigData)["serverConfig"]["embattle_open"].split(",");
  23. this.data.node = this.node;
  24. DataSystem.getData(UserData).lv = DataSystem.getData(UserData).lv;
  25. }
  26. update() {
  27. DataSystem.watch(UserData, "lv") && this.setLevel();
  28. DataSystem.watch(this.data, "_state") && this.setState();
  29. this.heroTargetChange();
  30. }
  31. /**根据等级判断插槽是否解锁 */
  32. private setLevel(): void {
  33. console.log("----------------------------------------观察到lv变化:"+DataSystem.getData(UserData).lv);
  34. if (this.data._state == HeroSlotState.lock) {
  35. if (DataSystem.getData(UserData).lv >= parseInt(this.config[this.slotID - 1])) {
  36. this.data._state = HeroSlotState.empty;
  37. } else {
  38. this.data._state = HeroSlotState.lock;
  39. }
  40. this.setState();
  41. }
  42. }
  43. /**设置插槽状态 */
  44. private setState(): void {
  45. this.lock.active = this.data._state == HeroSlotState.lock;
  46. this.empty.active = this.data._state == HeroSlotState.empty;
  47. this.unlockLabel.string = `${this.config[this.slotID - 1]}关`;
  48. }
  49. private heroTargetChange(): void {
  50. if (this.data._state == HeroSlotState.full) {
  51. let target = DataSystem.getData(BattleData)._monsters[DataSystem.getData(BattleData).heros.get(this.slotID).target];
  52. if (target && target.node && target.node.position.x) {
  53. this.hero.scale = target.node.position.x > this.node.position.x ? v3(-1, 1, 1) : v3(1, 1, 1);
  54. this.data._direction = this.hero.scale.x;
  55. }
  56. }
  57. }
  58. }
  59. export enum HeroSlotState {
  60. lock,
  61. empty,
  62. full
  63. }