NodeFood.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import { App } from "../../Manager/App";
  2. import hallModel from "../../model/hallModel";
  3. import TipPanel from "../tipPanel/TipPanel";
  4. import TipPanelMediator from "../tipPanel/TipPanelMediator";
  5. const {ccclass, property} = cc._decorator;
  6. @ccclass
  7. export default class NodeFood extends cc.Component {
  8. @property(cc.Label)
  9. labelExp: cc.Label = null;
  10. btnList: cc.Button[] = [];
  11. LabelCoinNum: cc.Label[] = [];
  12. shengjiTishi: cc.Node[] = [];
  13. @property([cc.SpriteFrame])
  14. foodSpriteFrame: cc.SpriteFrame[] = [];
  15. private foodName: string[] = [];
  16. @property(cc.Sprite)
  17. selectSprite: cc.Sprite = null;
  18. @property(cc.Label)
  19. labelCurSoldPrice: cc.Label = null;
  20. @property(cc.Label)
  21. labelNexSoldPrice: cc.Label = null;
  22. private selectUpIndex = -1;
  23. @property(cc.Button)
  24. btnUpgrade: cc.Button = null;
  25. onLoad () {
  26. for (let i = 0; i < 6; i++) {
  27. this.btnList[i] = this.node.getChildByName("btnFood" + i).getComponent(cc.Button);
  28. this.btnList[i].node.on("click", this.btnClickCallBack.bind(this, i), this);
  29. this.LabelCoinNum[i] = this.btnList[i].node.getChildByName("LabelCoinNum").getComponent(cc.Label);
  30. this.shengjiTishi[i] = this.btnList[i].node.getChildByName("tip_shenji");
  31. this.shengjiTishi[i].active = false;
  32. let level = App.DataManager.foodLevel[i];
  33. if(level < App.DataManager.maxFoodLevel){
  34. // 升级所需金币
  35. this.LabelCoinNum[i].string = App.DataManager.upgradeFoodCoin[i][level] + "";
  36. if(App.DataManager.UserCoin > App.DataManager.upgradeFoodCoin[i][level]){
  37. // 提示可以升级
  38. this.shengjiTishi[i].active = true;
  39. }
  40. }
  41. else{
  42. this.btnList[i].interactable = false;
  43. this.LabelCoinNum[i].node.active = false;
  44. }
  45. }
  46. this.btnUpgrade.node.on("click", this.clickBtnUpgrade.bind(this), this);
  47. this.foodName = ["牛排", "果汁", "黄瓜", "番茄", "芝士", "番茄酱"];
  48. }
  49. start() {
  50. this.findUpGrade();
  51. }
  52. // 默认选择
  53. findUpGrade(){
  54. this.selectUpIndex = -1;
  55. for (let i = App.DataManager.foodLevel.length - 1; i >= 0; i--) {
  56. let foodCurLevel = App.DataManager.foodLevel[i];
  57. if(foodCurLevel == App.DataManager.maxFoodLevel){
  58. this.btnList[i].interactable = false;
  59. this.LabelCoinNum[i].node.active = false;
  60. }
  61. else{
  62. this.selectUpIndex = i;
  63. this.LabelCoinNum[i].string = App.DataManager.upgradeFoodCoin[i][foodCurLevel] + "";
  64. }
  65. }
  66. this.btnClickCallBack(this.selectUpIndex);
  67. }
  68. btnClickCallBack(index: number){
  69. if(index < 0){
  70. return;
  71. }
  72. this.selectUpIndex = index;
  73. this.labelExp.string = this.foodName[index];
  74. this.selectSprite.spriteFrame = this.foodSpriteFrame[index];
  75. let foodCurLevel = App.DataManager.foodLevel[index];
  76. this.labelCurSoldPrice.string = App.DataManager.foodSoldCoin[index][foodCurLevel] + "";
  77. this.labelNexSoldPrice.string = App.DataManager.foodSoldCoin[index][foodCurLevel + 1] + "";
  78. }
  79. clickBtnUpgrade(){
  80. if(this.selectUpIndex < 0){
  81. App.Facade.popView(TipPanelMediator, TipPanel, "已经是最高级了", false);
  82. return;
  83. }
  84. let foodCurLevel = App.DataManager.foodLevel[this.selectUpIndex];
  85. let upgradeCoin = App.DataManager.upgradeFoodCoin[this.selectUpIndex][foodCurLevel];
  86. if(App.DataManager.UserCoin < upgradeCoin){
  87. App.Facade.popView(TipPanelMediator, TipPanel, "金币不足", false);
  88. }
  89. else{
  90. App.DataManager.foodLevel[this.selectUpIndex]++;
  91. App.DataManager.UserCoin -= upgradeCoin;
  92. // 保存到本地
  93. App.LocalStorageUtil.setNumber(App.LocalStorageUtil.lst_Coin, App.DataManager.UserCoin);
  94. App.LocalStorageUtil.setJsonObj(App.LocalStorageUtil.list_foodLevel, App.DataManager.foodLevel);
  95. App.Facade.popView(TipPanelMediator, TipPanel, "升级成功", false);
  96. App.Facade.getModel(hallModel).updateCoinNumber();
  97. for (let i = 0; i < 6; i++) {
  98. let level = App.DataManager.foodLevel[i];
  99. this.shengjiTishi[i].active = false;
  100. if(level < App.DataManager.maxFoodLevel){
  101. // 升级所需金币
  102. this.LabelCoinNum[i].string = App.DataManager.upgradeFoodCoin[i][level] + "";
  103. if(App.DataManager.UserCoin > App.DataManager.upgradeFoodCoin[i][level]){
  104. //提示可以升级
  105. this.shengjiTishi[i].active = true;
  106. }
  107. }
  108. else{
  109. this.btnList[i].interactable = false;
  110. this.LabelCoinNum[i].node.active = false;
  111. }
  112. }
  113. this.findUpGrade();
  114. }
  115. }
  116. // update (dt) {}
  117. }