FormationHeroItem.ts 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { _decorator, Component, Sprite, Label, SpriteFrame, Prefab, instantiate, Node } from 'cc';
  2. import { DataSystem } from '../../core/data/DataSystem';
  3. import { ResourceLoader } from '../../core/resourceManager/ResourceLoader';
  4. import { BitmapFont } from '../../core/ui/BitmapFont';
  5. import { DragSource } from '../../core/ui/drag/DragSource';
  6. import { DragSystem } from '../../core/ui/drag/DragSystem';
  7. import { UserHero, UserHeroData } from '../UserHeroData';
  8. import { FormationData } from './FormationData';
  9. import { HeroDragHelper } from './HeroDragHelper';
  10. const { ccclass, property, requireComponent } = _decorator;
  11. /**
  12. * 阵位英雄项
  13. * @author 袁浩
  14. */
  15. @ccclass('FormationHeroItem')
  16. @requireComponent(ResourceLoader)
  17. export class FormationHeroItem extends Component {
  18. @property({ tooltip: "统帅值", type: Label })
  19. public leaderLabel: Label;
  20. @property({ tooltip: "等级", type: Label })
  21. public lvLabel: Label;
  22. @property({ tooltip: "图标", type: Sprite })
  23. public avatorSprite: Sprite;
  24. @property({ tooltip: "星级", type: BitmapFont })
  25. public starBitmapLabel: BitmapFont;
  26. @property({ tooltip: "拖拽生成的预制体", type: Prefab })
  27. public dragPrefab: Prefab;
  28. @property({ tooltip: "品质框", type: Sprite })
  29. public qualityBorder: Sprite;
  30. @property({ tooltip: "所有品质框", type: [SpriteFrame] })
  31. public qualityBorderList: SpriteFrame[] = [];
  32. @property({ tooltip: "在阵上节点", type: Node })
  33. public inFormationNode: Node;
  34. public data: UserHero;
  35. public helper: HeroDragHelper;
  36. private dragSource: DragSource;
  37. start() {
  38. this.dragSource = this.getComponent(DragSource);
  39. }
  40. public async onDataChange(data: UserHero, index?: number) {
  41. if (!data) {
  42. this.node.active = false;
  43. return;
  44. }
  45. this.data = data;
  46. this.node.active = true;
  47. this.leaderLabel.string = data.client.commander + "";
  48. this.lvLabel.string = `Lv.${data.server.lv}`;
  49. let starString = "";
  50. for (let i = 0; i < data.server.star; i++) {
  51. starString += "0";
  52. }
  53. this.starBitmapLabel.string = starString;
  54. this.qualityBorder.spriteFrame = this.qualityBorderList[data.client.color - 1];
  55. let formation = DataSystem.getData(FormationData);
  56. this.inFormationNode.active = !!formation.getIDByHero(this.data.client.id);
  57. this.avatorSprite.spriteFrame = await this.getComponent(ResourceLoader).load(`images/general/texture/head_img/${data.client.hero_res}/spriteFrame`, SpriteFrame);
  58. }
  59. update() {
  60. if (this.data && DataSystem.watch(UserHeroData, this.data.client.id + "")) {
  61. this.onDataChange(this.data);
  62. }
  63. if (this.data && (
  64. DataSystem.watch(FormationData, "1") ||
  65. DataSystem.watch(FormationData, "2") ||
  66. DataSystem.watch(FormationData, "3") ||
  67. DataSystem.watch(FormationData, "4") ||
  68. DataSystem.watch(FormationData, "5")
  69. )) {
  70. this.onDataChange(this.data);
  71. }
  72. }
  73. public onDrag(isInTarget: boolean) {
  74. this.helper && this.helper.node.destroy();
  75. this.helper = null;
  76. let dragNode = instantiate(this.dragPrefab);
  77. dragNode.parent = DragSystem.touchNode;
  78. this.helper = dragNode.getComponent(HeroDragHelper);
  79. this.helper.data = this.data;
  80. this.helper.onDrag(this.dragSource, this.data);
  81. }
  82. public onDraging() {
  83. this.helper && (this.helper.onDraging(this.dragSource));
  84. }
  85. public onDrop() {
  86. this.helper && this.helper.node.destroy();
  87. this.helper = null;
  88. }
  89. }