PopupUI.ts 4.2 KB

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