GameTaskItem.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { _decorator, Component, Node } from "cc";
  2. import BaseUI from "../../scripts/base/BaseUI";
  3. import { GameTaskInfo } from "../../scripts/api/UserM";
  4. import GameM from "../../scripts/api/GameM";
  5. const { ccclass, property } = _decorator;
  6. export interface GameTaskItemCallback {
  7. onClaim(taskId: number, times: number, node: Node): void;
  8. }
  9. @ccclass("GameTaskItem")
  10. export class GameTaskItem extends BaseUI {
  11. private callback: GameTaskItemCallback;
  12. private gameId: number;
  13. private task: GameTaskInfo;
  14. init(gameId: number, task: GameTaskInfo, callback: GameTaskItemCallback) {
  15. this.gameId = gameId;
  16. this.task = task;
  17. this.callback = callback;
  18. this.initUI();
  19. }
  20. initUI() {
  21. this.setText("lbl_exp","x"+this.task.xp)
  22. this.setText("lbl_title", this.task.desc);
  23. this.setText("lbl_spin_times", "x" + this.task.onceSpin + "");
  24. this.FindNode("btn_claim").active = this.task.spinCount > 0;
  25. if (this.task.spinCount > 0) {
  26. this.setText(
  27. "lbl_claim_spin_times",
  28. "Claim(" + this.task.spinCount + ")"
  29. );
  30. } else {
  31. this.setText("lbl_claim_spin_times", "Claim");
  32. }
  33. }
  34. protected simpleOnBtnClick(name: string): void {
  35. switch (name) {
  36. case "btn_claim":
  37. this.claimSpin();
  38. break;
  39. }
  40. }
  41. async claimSpin() {
  42. if (this.task.spinCount <= 0) {
  43. return;
  44. }
  45. let result = await GameM.ins.claimSpinTimes(this.gameId, this.task.taskId);
  46. if (!result) {
  47. return;
  48. }
  49. this.task.spinCount =0;
  50. this.initUI();
  51. this.callback.onClaim(
  52. this.task.taskId,
  53. result.currentSpin,
  54. this.FindNode("icon_spin")
  55. );
  56. }
  57. }