UI_Item.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { _decorator, Button, Component, instantiate, Label, Node, Sprite, SpriteFrame, tween, v3 } from 'cc';
  2. import { Enum } from 'cc';
  3. import { ch } from '../../../ch/ch';
  4. import { gui } from '../../../core/ui/ui';
  5. import { UIAlert } from '../../../module_basic/ui_alert/UIAlert';
  6. import { UINotify } from '../../../module_basic/ui_notify/UINotify';
  7. import { Hall } from 'db://assets/Scripts/Hall';
  8. import { UI_Main } from '../UI_Main/UI_Main';
  9. import { Layout_Main } from '../UI_Main/Layout_Main';
  10. // import { GameLink } from '../../game/GameLink';
  11. // import { UI_ad_get } from '../ad_get/UI_ad_get';
  12. // import { LvDir } from '../../game/Lv';
  13. const { ccclass, property } = _decorator;
  14. @ccclass('UI_Item')
  15. export class UI_Item extends Component {
  16. @property
  17. type: number = 0;
  18. @property(Label)
  19. count: Label;
  20. @property(Node)
  21. ad: Node;
  22. private _c: number = 0;
  23. protected start(): void {
  24. if (this.type == 0) return;
  25. this.show(this.type, Hall.getInstance().player.get_item(this.type));
  26. if (this.node.getComponent(Button)) {
  27. this.node.on(Node.EventType.TOUCH_END, this.onClick, this);
  28. }
  29. Hall.getInstance().player.event.on(Hall.getInstance().player.event.key.item_count, this.show, this);
  30. }
  31. protected onDestroy(): void {
  32. Hall.getInstance().player.event.off(Hall.getInstance().player.event.key.item_count, this.show, this);
  33. }
  34. show(type: number, count: number): void {
  35. if (type != this.type) return;
  36. if (this._c != count) {
  37. tween(this.node).to(0.05, { scale: v3(1.2, 1.2, 1) }).to(0.1, { scale: v3(1, 1, 1) }).start();
  38. if (count < this._c) {
  39. // if (type == 1) {
  40. // ch.audio.playOneShot('sounds/sfx_item1');
  41. // } else if (type == 2) {
  42. // ch.audio.playOneShot('sounds/sfx_item2');
  43. // } else {
  44. // ch.audio.playOneShot('sounds/sfx_item3');
  45. // }
  46. }
  47. }
  48. this._c = count;
  49. this.count.string = (count == 0) ? '+' : count.toString();
  50. if (type == 1) {
  51. // if (GameLink.getInst().lv.dir == LvDir.none) {
  52. // this.node.active = false;
  53. // } else {
  54. // this.node.active = true;
  55. // }
  56. }
  57. //this.ad.active = count <= 0;
  58. }
  59. //
  60. private async onClick(evt: any) {
  61. if (this._c <= 0) {
  62. if (this.type == 3) {
  63. let flag=false;
  64. for (let element of gui.get(UI_Main).getLayout<Layout_Main>().Container.node_isIdiom) {
  65. console.log(element);
  66. if (element == true) {
  67. flag=true;
  68. break;
  69. }
  70. }
  71. if(!flag)
  72. UINotify.show('槽中无可清除的方块');
  73. }
  74. const ret = await chsdk.playRewardAd('获得道具' + this.type);
  75. if (ret) {
  76. // ch.audio.playOneShot('sounds/sfx_add_item');
  77. Hall.getInstance().player.add_item(this.type, 1);
  78. ch.audio.resume();
  79. }
  80. // gui.show(UI_ad_get, this.type);
  81. } else {
  82. if (this.type == 3) {
  83. let flag=false;
  84. for (let element of gui.get(UI_Main).getLayout<Layout_Main>().Container.node_isIdiom) {
  85. console.log(element);
  86. if (element == true) {
  87. Hall.getInstance().player.use_item(this.type);
  88. flag=true;
  89. break;
  90. }
  91. }
  92. if(!flag)
  93. UINotify.show('槽中无可清除的方块');
  94. }
  95. else {
  96. Hall.getInstance().player.use_item(this.type);
  97. }
  98. }
  99. }
  100. }