BoxLayer.ts 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { _decorator, Component, Node, tween, v3, Label, Vec3 } from 'cc';
  2. import { BOX_TYPE, ITEM_TYPE, TItem } from '../../../core/common/GameConst';
  3. import BasePanel from '../../../core/component/BasePanel';
  4. import Data from '../../../core/manager/Data';
  5. import Utils from '../../../core/utils/Utils';
  6. import { constants } from 'db://assets/data/constants';
  7. import audioManager from 'db://assets/manager/audioManager';
  8. const { ccclass, property } = _decorator;
  9. @ccclass('BoxLayer')
  10. export class BoxLayer extends BasePanel {
  11. start() {
  12. this.GetGameObject("item").active = false;
  13. this.GetGameObject("item").setPosition(Vec3.ZERO);
  14. this.GetGameObject("box").setPosition(Vec3.ZERO);
  15. }
  16. //最多奖励4个
  17. private list: TItem[];
  18. private boxType;
  19. setInfo(boxType: BOX_TYPE, list: TItem[]) {
  20. this.boxType = boxType;
  21. this.list = list;
  22. for (var i = 0; i < 4; ++i) {
  23. let tmp = this.GetGameObject("item").children[i];
  24. if (list[i]) {
  25. tmp.active = true;
  26. tmp.getChildByName("Time").active = list[i].type == ITEM_TYPE.Time;
  27. tmp.getChildByName("Frozen").active = list[i].type == ITEM_TYPE.Frozen;
  28. tmp.getChildByName("Hint").active = list[i].type == ITEM_TYPE.Hint;
  29. tmp.getChildByName("Magnet").active = list[i].type == ITEM_TYPE.Magnet;
  30. tmp.getChildByName("Coin").active = list[i].type == ITEM_TYPE.Coin;
  31. tmp.getChildByName("Label").getComponent(Label).string = "+" + list[i].num;
  32. }
  33. else {
  34. tmp.active = false;
  35. }
  36. }
  37. this.GetGameObject("chest-level-reward-close").active = boxType == BOX_TYPE.LEVEL;
  38. this.GetGameObject("chest-team-reward-close").active = boxType == BOX_TYPE.TEAM;
  39. this.GetGameObject("chest-star-reward-close").active = boxType == BOX_TYPE.STAR;
  40. this.GetGameObject("chest-level-reward-open").active = false
  41. this.GetGameObject("chest-star-reward-open").active = false
  42. this.GetGameObject("chest-team-reward-open").active = false
  43. }
  44. private open = false;
  45. onBtnClicked(event: any, customEventData: any) {
  46. audioManager.playOneShot(constants.audios.btnclick);
  47. var btnName = event.target.name;
  48. switch (btnName) {
  49. case "btn_close":
  50. this.close();
  51. break;
  52. case "btn_open":
  53. if (this.open) {
  54. this.list.map(a => {
  55. Data.user.addItem(a.type, a.num);
  56. if (a.type == ITEM_TYPE.Coin) {
  57. Utils.flyAnim(ITEM_TYPE.Coin, this.GetNode("item"), this.GetNodeFromCanvas("coin-icon"), 5, 50, (b) => {
  58. });
  59. }
  60. })
  61. if (this.boxType == BOX_TYPE.LEVEL) {
  62. Data.user.lvboxDrawInfo[Math.floor(Data.user.lv / 10)] = 1;
  63. }
  64. else if (this.boxType == BOX_TYPE.STAR) {
  65. Data.user.starboxDrawInfo[Math.floor(Data.user.star / 1000)] = 1;
  66. }
  67. Data.save();
  68. this.close();
  69. }
  70. else {
  71. this.open = true;
  72. this.GetGameObject("item").active = true;
  73. tween(this.GetGameObject("item")).by(0.3, { worldPosition: v3(0, 90, 0) }).start();
  74. tween(this.GetGameObject("box")).by(0.3, { worldPosition: v3(0, -60, 0) }).start();
  75. this.GetGameObject("chest-level-reward-close").active = false
  76. this.GetGameObject("chest-team-reward-close").active = false
  77. this.GetGameObject("chest-star-reward-close").active = false
  78. this.GetGameObject("chest-level-reward-open").active = this.boxType == BOX_TYPE.LEVEL;
  79. this.GetGameObject("chest-team-reward-open").active = this.boxType == BOX_TYPE.TEAM;
  80. this.GetGameObject("chest-star-reward-open").active = this.boxType == BOX_TYPE.STAR;
  81. this.SetText("lbl_btn", "领取")
  82. }
  83. break;
  84. }
  85. }
  86. }