NodeFood.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. this.foodName = ["steak", "fruit juice", "cuke", "tomato", "cheese", "catchup"];
  49. }
  50. start() {
  51. this.findUpGrade();
  52. }
  53. // 默认选择
  54. findUpGrade(){
  55. this.selectUpIndex = -1;
  56. for (let i = App.DataManager.foodLevel.length - 1; i >= 0; i--) {
  57. let foodCurLevel = App.DataManager.foodLevel[i];
  58. if(foodCurLevel == App.DataManager.maxFoodLevel){
  59. this.btnList[i].interactable = false;
  60. this.LabelCoinNum[i].node.active = false;
  61. }
  62. else{
  63. this.selectUpIndex = i;
  64. this.LabelCoinNum[i].string = App.DataManager.upgradeFoodCoin[i][foodCurLevel] + "";
  65. }
  66. }
  67. this.btnClickCallBack(this.selectUpIndex);
  68. }
  69. btnClickCallBack(index: number){
  70. if(index < 0){
  71. return;
  72. }
  73. this.selectUpIndex = index;
  74. this.labelExp.string = this.foodName[index];
  75. this.selectSprite.spriteFrame = this.foodSpriteFrame[index];
  76. let foodCurLevel = App.DataManager.foodLevel[index];
  77. this.labelCurSoldPrice.string = App.DataManager.foodSoldCoin[index][foodCurLevel] + "";
  78. this.labelNexSoldPrice.string = App.DataManager.foodSoldCoin[index][foodCurLevel + 1] + "";
  79. }
  80. clickBtnUpgrade(){
  81. if(this.selectUpIndex < 0){
  82. App.Facade.popView(TipPanelMediator, TipPanel, "It's the highest level", false);
  83. return;
  84. }
  85. let foodCurLevel = App.DataManager.foodLevel[this.selectUpIndex];
  86. let upgradeCoin = App.DataManager.upgradeFoodCoin[this.selectUpIndex][foodCurLevel];
  87. if(App.DataManager.UserCoin < upgradeCoin){
  88. App.Facade.popView(TipPanelMediator, TipPanel, "Not enough gold coins", false);//Not enough gold coins." 金币不足
  89. }
  90. else{
  91. App.DataManager.foodLevel[this.selectUpIndex]++;
  92. App.DataManager.UserCoin -= upgradeCoin;
  93. // 保存到本地
  94. App.LocalStorageUtil.setNumber(App.LocalStorageUtil.lst_Coin, App.DataManager.UserCoin);
  95. App.LocalStorageUtil.setJsonObj(App.LocalStorageUtil.list_foodLevel, App.DataManager.foodLevel);
  96. App.Facade.popView(TipPanelMediator, TipPanel, "update successfully", false);
  97. App.Facade.getModel(hallModel).updateCoinNumber();
  98. for (let i = 0; i < 6; i++) {
  99. let level = App.DataManager.foodLevel[i];
  100. this.shengjiTishi[i].active = false;
  101. if(level < App.DataManager.maxFoodLevel){
  102. // 升级所需金币
  103. this.LabelCoinNum[i].string = App.DataManager.upgradeFoodCoin[i][level] + "";
  104. if(App.DataManager.UserCoin > App.DataManager.upgradeFoodCoin[i][level]){
  105. //提示可以升级
  106. this.shengjiTishi[i].active = true;
  107. }
  108. }
  109. else{
  110. this.btnList[i].interactable = false;
  111. this.LabelCoinNum[i].node.active = false;
  112. }
  113. }
  114. this.findUpGrade();
  115. }
  116. }
  117. // update (dt) {}
  118. }