RewardLayer.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import { _decorator, Component, Node } from "cc";
  2. import { Hall } from "../hall/Hall";
  3. import { ScrollView } from "cc";
  4. import BaseUI from "../../scripts/base/BaseUI";
  5. import UIUtils from "../../scripts/base/UIUtils";
  6. import AB from "../../scripts/base/AB";
  7. import { Prefab } from "cc";
  8. import { instantiate } from "cc";
  9. import { UITransform } from "cc";
  10. import { Sprite } from "cc";
  11. import { GoodInfo } from "../../scripts/api/UserM";
  12. import { TapClose } from "../../scripts/base/TapClose";
  13. import { RewardItem } from "../item/RewardItem";
  14. import { GoodsId } from "../../scripts/api/GoodsId";
  15. import { FlyContainer } from "../hall/FlyContainer";
  16. import { isValid } from "cc";
  17. const { ccclass, property } = _decorator;
  18. export interface OnCloseCallback {
  19. (): void;
  20. }
  21. @ccclass("RewardLayer")
  22. export class RewardLayer extends BaseUI {
  23. static mock() {
  24. if (1 > 1) {
  25. RewardLayer.show([
  26. {
  27. id: 1,
  28. count: 100,
  29. },
  30. ]);
  31. return;
  32. }
  33. RewardLayer.show([
  34. {
  35. id: 1,
  36. count: 100,
  37. },
  38. {
  39. id: 2,
  40. count: 600,
  41. },
  42. {
  43. id: 4,
  44. count: 300,
  45. },
  46. {
  47. id: 1,
  48. count: 100,
  49. },
  50. {
  51. id: 1,
  52. count: 100,
  53. },
  54. {
  55. id: 1,
  56. count: 100,
  57. },
  58. {
  59. id: 2,
  60. count: 200,
  61. },
  62. {
  63. id: 1,
  64. count: 300,
  65. },
  66. {
  67. id: 1,
  68. count: 300,
  69. },
  70. {
  71. id: 1,
  72. count: 100,
  73. },
  74. ]);
  75. }
  76. static async show(items: GoodInfo[], onClose: OnCloseCallback = null) {
  77. let layer = await Hall.ins.showLayer("prefab/layer/RewardLayer");
  78. layer.getComponent(RewardLayer).init(items, onClose);
  79. }
  80. protected onLoad(): void {
  81. super.onLoad();
  82. }
  83. private items: GoodInfo[] = [];
  84. private onClose: OnCloseCallback;
  85. init(items: GoodInfo[], onClose: OnCloseCallback) {
  86. this.items = items;
  87. this.onClose = onClose;
  88. let bgTr = this.FindAs("bg", UITransform);
  89. if (items.length == 1) {
  90. bgTr.node.getComponent(Sprite).enabled = false;
  91. }
  92. this.FindNode("lbl_nothing").active = false;
  93. if (!items || items.length == 0) {
  94. this.FindNode("lbl_nothing").active = true;
  95. return;
  96. }
  97. this.ui();
  98. }
  99. protected closePage(): void {
  100. this.onClose?.();
  101. try {
  102. this.getComponentsInChildren(RewardItem).forEach((item) => {
  103. let good = item.getItem();
  104. FlyContainer.ins.onReward(item.node, good);
  105. });
  106. this.node.destroy();
  107. } catch (error) {
  108. console.log("error", error);
  109. }
  110. super.closePage();
  111. }
  112. async ui() {
  113. this.canClose = false;
  114. let tap = this.node.getComponentInChildren(TapClose);
  115. tap.node.active = false;
  116. let tpl: Prefab = await AB.ins.loadPrefab("prefab/item/RewardItem");
  117. let animTime = 200;
  118. for (let i = 0; i < this.items.length; i++) {
  119. let item = this.items[i];
  120. let itemNode = instantiate(tpl);
  121. itemNode.active = false;
  122. itemNode.setPosition(0, 0, 0);
  123. itemNode.parent = this.FindNode("slot" + (i + 1));
  124. let delay = 200 + i * 200;
  125. animTime = delay;
  126. itemNode.getComponent(RewardItem).init(delay, item);
  127. }
  128. let self = this;
  129. setTimeout(() => {
  130. self.canClose = true;
  131. if (isValid(self.node)) {
  132. tap.node.active = true;
  133. }
  134. }, this.items.length * 200 + 2000);
  135. }
  136. }