PopupUI.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import { _decorator, instantiate, Node } from 'cc';
  2. import { UIBase } from '../GameFrameWork/UIBase';
  3. import { resMgr } from '../../Frames/ResourcesMgr';
  4. import { PopupUIDataConfig } from '../MyApp/GameScene/Data/PopupUIDataConfig';
  5. import { UIMgr } from '../../Frames/UIManager';
  6. import { GameInfo } from '../../GameInfo';
  7. import { Tip } from './Tip';
  8. import { messageMgr } from '../../Frames/MessageMgr';
  9. const { ccclass, property } = _decorator;
  10. @ccclass('PopupUI')
  11. export class PopupUI extends UIBase {
  12. private _UIName: string = null;
  13. private _typeImg: string = null;
  14. private _typeMoney: string = null;
  15. private _label_1: string = null;
  16. private _label_2: string = null;
  17. private _label_3: string = null;
  18. private _consume: string = null;
  19. protected onEnable(): void {
  20. this._UIName = PopupUIDataConfig.Instance.getUIName()
  21. this._typeImg = PopupUIDataConfig.Instance.getTypeImg()
  22. this._typeMoney = PopupUIDataConfig.Instance.getTypeMoney();
  23. this._label_1 = PopupUIDataConfig.Instance.getLabel()[0];
  24. this._label_2 = PopupUIDataConfig.Instance.getLabel()[1];
  25. this._label_3 = PopupUIDataConfig.Instance.getLabel()[2];
  26. this._consume = PopupUIDataConfig.Instance.getLabel()[3];
  27. }
  28. onStart() {
  29. this.onBtnClick("_btnBack", this.onBtnBack, this);
  30. this.onBtnClick("_btnYes", this.onBtnYes, this);
  31. this.onBtnClick("_btnCancel", this.onBtnCancel, this);
  32. this.uiName();
  33. this.typeImg();
  34. this.label()
  35. }
  36. uiName() {
  37. this.getLabel("_uiName").string = this._UIName;
  38. }
  39. typeImg() {
  40. this.getSprite("_typeImg").spriteFrame = resMgr.getSpriteFrame(this._typeImg);
  41. this.getSprite("_typeMoney").spriteFrame = resMgr.getSpriteFrame(this._typeMoney);
  42. }
  43. label() {
  44. this.getLabel("_label-1").string = this._label_1;
  45. this.getLabel("_label-2").string = this._label_2;
  46. this.getLabel("_label-3").string = this._label_3;
  47. this.getLabel("_consume").string = `${this._consume}`;
  48. }
  49. //返回
  50. private onBtnBack() {
  51. this.hide(false);
  52. }
  53. //确定
  54. private onBtnYes() {
  55. // if (this._typeMoney === "Gold") {
  56. // if (Number(this._consume) > GameInfo.Instance.getGold()) {
  57. // const tip: Node = instantiate(resMgr.getPrefab("Tip"));
  58. // tip.getComponent(Tip).setContent("金币不足!");
  59. // tip.parent = this.node;
  60. // return;
  61. // }
  62. // } else if(this._typeMoney === "Diamond"){
  63. // if (Number(this._consume) > GameInfo.Instance.getDiamond()) {
  64. // const tip: Node = instantiate(resMgr.getPrefab("Tip"));
  65. // tip.getComponent(Tip).setContent("钻石不足!")
  66. // tip.parent = this.node;
  67. // return;
  68. // }
  69. // }
  70. const resourceMap = {
  71. "Gold": {
  72. getAmount: GameInfo.Instance.getGold(),
  73. message: "金币不足!",
  74. },
  75. "Diamond": {
  76. getAmount: GameInfo.Instance.getDiamond(),
  77. message: "钻石不足!",
  78. }
  79. };
  80. const resource = resourceMap[this._typeMoney];
  81. //消耗
  82. const consume: number = Number(this._consume);
  83. //总共
  84. const totalGold: number = GameInfo.Instance.getGold();
  85. if (resource && Number(this._consume) > resource.getAmount) {
  86. const tip: Node = instantiate(resMgr.getPrefab("Tip"));
  87. tip.getComponent(Tip).setContent(resource.message);
  88. tip.parent = this.node;
  89. return;
  90. }
  91. messageMgr.dispatch("reduceGold", (totalGold - consume));
  92. GameInfo.Instance.setGold((totalGold - consume));
  93. (PopupUIDataConfig.Instance.getFunction())();
  94. UIMgr.closeUI("PopupUI", true);
  95. PopupUIDataConfig.Instance.clearDt();
  96. }
  97. //取消
  98. private onBtnCancel() {
  99. this.hide(false);
  100. }
  101. }