BoxLayer.ts 4.2 KB

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