GameDetailLayer.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. import { _decorator, Component, Node, Event } from "cc";
  2. import { game } from "cc";
  3. import { instantiate } from "cc";
  4. import { Vec3 } from "cc";
  5. import { Sprite } from "cc";
  6. import BaseUI from "../../scripts/base/BaseUI";
  7. import UIUtils from "../../scripts/base/UIUtils";
  8. import { Hall } from "../hall/Hall";
  9. import { WheelDialog } from "../play/WheelDialog";
  10. import UserM, {
  11. GameInfo,
  12. GameTaskInfo,
  13. GoodInfo,
  14. } from "../../scripts/api/UserM";
  15. import Utils from "../../scripts/utils/Utils";
  16. import ConfigM, { ConfigGame } from "../../scripts/api/ConfigM";
  17. import AB from "../../scripts/base/AB";
  18. import GameM, { GameSpinResult } from "../../scripts/api/GameM";
  19. import { GameTaskItem, GameTaskItemCallback } from "../play/GameTaskItem";
  20. import TgM from "../../scripts/mgr/TgM";
  21. import { Tips } from "../../scripts/mgr/Tips";
  22. import { TipsData, TipsLayer } from "./TipsLayer";
  23. import { GoodsId } from "../../scripts/api/GoodsId";
  24. import ItemsM from "../../scripts/mgr/ItemsM";
  25. import { RewardLayer } from "./RewardLayer";
  26. const { ccclass, property } = _decorator;
  27. @ccclass("GameDetailLayer")
  28. export class GameDetailLayer extends BaseUI implements GameTaskItemCallback {
  29. private claiming: boolean = false;
  30. onClaim(taskId: number, refreshTimes: number, node: Node): void {
  31. if (this.claiming) {
  32. return;
  33. }
  34. this.claiming = true;
  35. let self = this;
  36. let a = instantiate(node);
  37. a.parent = node;
  38. a.setPosition(0, 0, 0);
  39. UIUtils.moveToNewParent(this.FindNode("icon_spin_slot"), a);
  40. UIUtils.AnimBezierMoveAndScale(a, new Vec3(0, 0, 0), 1.2, 0.8, () => {
  41. self.claiming = false;
  42. a.destroy();
  43. self.onClaimSpinCount(refreshTimes);
  44. });
  45. }
  46. onClaimSpinCount(refreshTimes: number) {
  47. this.data.currentSpin = refreshTimes;
  48. this.refreshSpinTimes();
  49. UIUtils.AnimScaleShake(this.FindNode("lbl_game_spin_times"), 2, 0.6, false);
  50. }
  51. static async show(gameId: number) {
  52. let layer = await Hall.ins.showLayer("prefab/layer/GameDetailLayer");
  53. layer.getComponent(GameDetailLayer).init(gameId);
  54. }
  55. protected onLoad(): void {
  56. super.onLoad();
  57. this.FindNode("game_pass_pvz_node").active = false;
  58. }
  59. // private spinTimes: number = 0;
  60. private data: GameInfo;
  61. async init(gameId: number) {
  62. let data = await GameM.ins.getGameDetailInfo(gameId);
  63. if (!data) {
  64. return;
  65. }
  66. this.data = data;
  67. if (data.gameId == 2) {
  68. this.FindNode("btn_play").active = false;
  69. }
  70. if (data.gameId == 1) {
  71. this.FindNode("game_pass_pvz_node").active = true;
  72. }
  73. let c = ConfigM.ins.getGame(data.gameId);
  74. this.showButtons(c);
  75. this.setText("lbl_name", c.gameName);
  76. console.log("item.remainPog", data.remainPog);
  77. if (data.seasonPog > 0) {
  78. this.setText(
  79. "lbl_remain_info",
  80. Utils.formatNumber(data.remainPog, 2) +
  81. "/" +
  82. Utils.formatNumber(data.seasonPog, 0)
  83. );
  84. this.setText(
  85. "lbl_my_reward_pog",
  86. Utils.formatNumber(data.myRewardPog, 0)
  87. );
  88. } else {
  89. this.setText("lbl_remain_info", "Coming soon");
  90. this.setText(
  91. "lbl_my_reward_pog",
  92. Utils.formatNumber(data.myRewardPog, 0)
  93. );
  94. }
  95. this.setIcon(data.gameId);
  96. this.initTask(data.taskList);
  97. this.refreshSpinTimes();
  98. }
  99. showButtons(c: ConfigGame) {
  100. this.FindNode("btn_start_game_web").active = c.webLink && c.webLink != "";
  101. this.FindNode("btn_start_game_tg").active = c.tgLink && c.tgLink != "";
  102. this.FindNode("btn_start_game_line").active =
  103. c.lineLink && c.lineLink != "";
  104. }
  105. async initTask(taskList: GameTaskInfo[]) {
  106. if (!taskList || taskList.length == 0) {
  107. this.FindNode("NoData").active = true;
  108. return;
  109. }
  110. this.FindNode("NoData").active = false;
  111. let prefab = await AB.ins.loadPrefab("prefab/play/GameTaskItem");
  112. let layout = this.FindNode("task_layout");
  113. for (let i = 0; i < taskList.length; i++) {
  114. let task = taskList[i];
  115. let item = instantiate(prefab);
  116. item.parent = layout;
  117. item.getComponent(GameTaskItem).init(this.data.gameId, task, this);
  118. }
  119. }
  120. async setIcon(gameId: number) {
  121. let path = "texture/play/icon_" + gameId;
  122. let spriteFrame = await AB.ins.loadSpriteFrame(path);
  123. this.FindAs("icon_game", Sprite).spriteFrame = spriteFrame;
  124. }
  125. protected simpleOnBtnClick(name: string): void {
  126. let c = ConfigM.ins.getGame(this.data.gameId);
  127. switch (name) {
  128. case "btn_start_spin":
  129. this.startSpin();
  130. break;
  131. case "btn_start_game_web":
  132. ConfigM.ins.openLink(c.webLink);
  133. break;
  134. case "btn_start_game_tg":
  135. ConfigM.ins.openLink(c.tgLink);
  136. break;
  137. case "btn_start_game_line":
  138. ConfigM.ins.openLink(c.lineLink);
  139. break;
  140. case "btn_game_pass_claim_pvz":
  141. Tips.show("Coming soon");
  142. break;
  143. }
  144. }
  145. async choose10times() {
  146. return new Promise((resolve) => {
  147. let tips = Utils.setI18nLabel("Play.TipsChoose10Times");
  148. let d = new TipsData(tips);
  149. d.onCancel = () => {
  150. resolve(false);
  151. };
  152. d.onConfirm = () => {
  153. resolve(true);
  154. };
  155. d.forceConfirm = false;
  156. d.show();
  157. });
  158. }
  159. async startSpin() {
  160. if (this.data.currentSpin <= 0) {
  161. return;
  162. }
  163. if (this.data.currentSpin >= 10) {
  164. let ok = await this.choose10times();
  165. if (ok) {
  166. this.spin10times();
  167. return;
  168. }
  169. }
  170. WheelDialog.show(this.data.gameId);
  171. this.data.currentSpin--;
  172. this.refreshSpinTimes();
  173. }
  174. async spin10times() {
  175. let onceTimes = 10;
  176. this.data.currentSpin -= onceTimes;
  177. this.refreshSpinTimes();
  178. let rewards = [];
  179. for (let index = 0; index < onceTimes; index++) {
  180. let spinResult: GameSpinResult = await GameM.ins.gameSpin(
  181. this.data.gameId
  182. );
  183. if (!spinResult) {
  184. continue;
  185. }
  186. let pog = spinResult.totalPog;
  187. let critResult = await GameM.ins.getCritReward(
  188. this.data.gameId,
  189. spinResult.spinId
  190. );
  191. if (!critResult) {
  192. continue;
  193. }
  194. pog += critResult.critPog;
  195. let g = new GoodInfo();
  196. g.count = pog;
  197. g.id = GoodsId.POG;
  198. rewards.push(g);
  199. }
  200. RewardLayer.show(rewards);
  201. await UserM.ins.refreshInfo();
  202. }
  203. refreshSpinTimes() {
  204. this.setText("lbl_game_spin_times", "x" + this.data.currentSpin + "");
  205. this.FindAs("btn_start_spin", Sprite).grayscale =
  206. this.data.currentSpin <= 0;
  207. }
  208. }