FormationOne.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import { _decorator, Component, Sprite, Label, SpriteFrame, Node, sp, color, instantiate, Prefab } from "cc";
  2. import { DataSystem } from "../../core/data/DataSystem";
  3. import { ResourceLoader } from "../../core/resourceManager/ResourceLoader";
  4. import { DragSource } from "../../core/ui/drag/DragSource";
  5. import { DragSystem } from "../../core/ui/drag/DragSystem";
  6. import { DragTarget } from "../../core/ui/drag/DragTarget";
  7. import { ConfigData } from "../../data/ConfigData";
  8. import { UserData } from "../../data/UserData";
  9. import { Fixbug } from "../../FixBug";
  10. import { UserHero, UserHeroData } from "../UserHeroData";
  11. import { Formation } from "./Formation";
  12. import { FormationData } from "./FormationData";
  13. import { FormationHeroItem } from "./FormationHeroItem";
  14. import { HeroDragHelper } from "./HeroDragHelper";
  15. const { ccclass, requireComponent, property } = _decorator;
  16. /**
  17. * 单个阵位
  18. * @author 袁浩
  19. */
  20. @ccclass('FormationOne')
  21. @requireComponent(ResourceLoader)
  22. export class FormationOne extends Component {
  23. @property({ tooltip: "阵位编号(统帅位为5)" })
  24. public id: number = 1;
  25. @property({ tooltip: "武将骨骼动画组件", type: sp.Skeleton })
  26. public body: sp.Skeleton;
  27. @property({ tooltip: "武将名称", type: Label })
  28. public nameLabel: Label;
  29. @property({ tooltip: "锁", type: Node })
  30. public lock: Node;
  31. @property({ tooltip: "锁文本", type: Label })
  32. public lockLabel: Label;
  33. @property({ tooltip: "台子(未拖入)", type: Node })
  34. public baseNodeNormal: Node;
  35. @property({ tooltip: "台子(拖入)", type: Node })
  36. public baseNodeActive: Node;
  37. @property({ tooltip: "拖拽生成的预制体", type: Prefab })
  38. public dragPrefab: Prefab;
  39. public formation: Formation;
  40. private dragSource: DragSource;
  41. public helper: HeroDragHelper;
  42. public data: UserHero;
  43. start() {
  44. this.dragSource = this.getComponent(DragSource);
  45. let unlockLevel = parseInt(DataSystem.getData(ConfigData).get("serverConfig").embattle_open.split(",")[this.id - 1])
  46. this.lock.active = DataSystem.getData(UserData).level < unlockLevel;
  47. this.lockLabel.string = `${unlockLevel}关`;
  48. // this.body.color = color(255, 255, 255, 0);
  49. this.baseNodeNormal.active = true;
  50. this.baseNodeActive.active = false;
  51. this.loadHero();
  52. }
  53. public async onDrop(source: DragSource, target: DragTarget) {
  54. this.baseNodeNormal.active = true;
  55. this.baseNodeActive.active = false;
  56. !this.lock.active && this.formation.onDrop(source, target);
  57. }
  58. update() {
  59. if (DataSystem.watch(FormationData, this.id + "")) {//如果本阵位数据发生变化
  60. this.loadHero();
  61. }
  62. }
  63. public fixSpine() {
  64. Fixbug.fixSpine_active(this.body);
  65. }
  66. private loadHero() {
  67. let heroID = DataSystem.getData(FormationData).get(this.id);
  68. if (heroID) {
  69. this.data = DataSystem.getData(UserHeroData).get(heroID);
  70. this.updateUI(this.data);
  71. }
  72. else {
  73. this.nameLabel.string = "";
  74. this.body.node.active = false;
  75. this.data = null;
  76. // this.body.skeletonData = null;
  77. }
  78. }
  79. public onDraging(source: DragSource, inTarget: boolean) {
  80. if (!this.lock.active) {
  81. this.baseNodeNormal.active = !inTarget;
  82. this.baseNodeActive.active = inTarget;
  83. }
  84. }
  85. public checkSame(source: DragSource, inTarget: boolean) {
  86. let helper = (source.getComponent(FormationHeroItem) || source.getComponent(FormationOne)).helper;
  87. if (inTarget && this.data && helper && helper.data.client.id == this.data.client.id) {
  88. this.nameLabel.node.active = false;
  89. this.body.node.active = false;
  90. }
  91. else {
  92. this.nameLabel.node.active = true;
  93. // this.body.color = color(255, 255, 255, !!DataSystem.getData(FormationData).get(this.id) ? 255 : 0);
  94. this.body.node.active = !!DataSystem.getData(FormationData).get(this.id) ? true : false;
  95. this.body.node.active && this.fixSpine();
  96. }
  97. }
  98. private async updateUI(data: UserHero) {
  99. this.nameLabel.string = data.client.name;
  100. this.body.node.active = true;
  101. this.fixSpine();
  102. this.body.skeletonData = await this.getComponent(ResourceLoader).load(`spine/hero/${data.client.hero_res}`, sp.SkeletonData);
  103. this.body.setAnimation(0, "idle", true);
  104. }
  105. public onSourceDrag(isInTarget: boolean) {
  106. this.helper && this.helper.node.destroy();
  107. this.helper = null;
  108. if (!this.data) {
  109. return;
  110. }
  111. let dragNode = instantiate(this.dragPrefab);
  112. dragNode.parent = DragSystem.touchNode;
  113. this.helper = dragNode.getComponent(HeroDragHelper);
  114. this.helper.data = this.data;
  115. this.helper.onDrag(this.dragSource, this.data);
  116. }
  117. public onSourceDraging() {
  118. this.helper && (this.helper.onDraging(this.dragSource));
  119. }
  120. public onSourceDrop() {
  121. this.helper && this.helper.node.destroy();
  122. this.helper = null;
  123. }
  124. }