import { App } from "../../Manager/App"; import hallModel from "../../model/hallModel"; import TipPanel from "../tipPanel/TipPanel"; import TipPanelMediator from "../tipPanel/TipPanelMediator"; const {ccclass, property} = cc._decorator; @ccclass export default class NodeFood extends cc.Component { @property(cc.Label) labelExp: cc.Label = null; btnList: cc.Button[] = []; LabelCoinNum: cc.Label[] = []; shengjiTishi: cc.Node[] = []; @property([cc.SpriteFrame]) foodSpriteFrame: cc.SpriteFrame[] = []; private foodName: string[] = []; @property(cc.Sprite) selectSprite: cc.Sprite = null; @property(cc.Label) labelCurSoldPrice: cc.Label = null; @property(cc.Label) labelNexSoldPrice: cc.Label = null; private selectUpIndex = -1; @property(cc.Button) btnUpgrade: cc.Button = null; onLoad () { for (let i = 0; i < 6; i++) { this.btnList[i] = this.node.getChildByName("btnFood" + i).getComponent(cc.Button); this.btnList[i].node.on("click", this.btnClickCallBack.bind(this, i), this); this.LabelCoinNum[i] = this.btnList[i].node.getChildByName("LabelCoinNum").getComponent(cc.Label); this.shengjiTishi[i] = this.btnList[i].node.getChildByName("tip_shenji"); this.shengjiTishi[i].active = false; let level = App.DataManager.foodLevel[i]; if(level < App.DataManager.maxFoodLevel){ // 升级所需金币 this.LabelCoinNum[i].string = App.DataManager.upgradeFoodCoin[i][level] + ""; if(App.DataManager.UserCoin > App.DataManager.upgradeFoodCoin[i][level]){ // 提示可以升级 this.shengjiTishi[i].active = true; } } else{ this.btnList[i].interactable = false; this.LabelCoinNum[i].node.active = false; } } this.btnUpgrade.node.on("click", this.clickBtnUpgrade.bind(this), this); //this.foodName = ["牛排", "果汁", "黄瓜", "番茄", "芝士", "番茄酱"]; this.foodName = ["steak", "fruit juice", "cuke", "tomato", "cheese", "catchup"]; } start() { this.findUpGrade(); } // 默认选择 findUpGrade(){ this.selectUpIndex = -1; for (let i = App.DataManager.foodLevel.length - 1; i >= 0; i--) { let foodCurLevel = App.DataManager.foodLevel[i]; if(foodCurLevel == App.DataManager.maxFoodLevel){ this.btnList[i].interactable = false; this.LabelCoinNum[i].node.active = false; } else{ this.selectUpIndex = i; this.LabelCoinNum[i].string = App.DataManager.upgradeFoodCoin[i][foodCurLevel] + ""; } } this.btnClickCallBack(this.selectUpIndex); } btnClickCallBack(index: number){ if(index < 0){ return; } this.selectUpIndex = index; this.labelExp.string = this.foodName[index]; this.selectSprite.spriteFrame = this.foodSpriteFrame[index]; let foodCurLevel = App.DataManager.foodLevel[index]; this.labelCurSoldPrice.string = App.DataManager.foodSoldCoin[index][foodCurLevel] + ""; this.labelNexSoldPrice.string = App.DataManager.foodSoldCoin[index][foodCurLevel + 1] + ""; } clickBtnUpgrade(){ if(this.selectUpIndex < 0){ App.Facade.popView(TipPanelMediator, TipPanel, "It's the highest level", false); return; } let foodCurLevel = App.DataManager.foodLevel[this.selectUpIndex]; let upgradeCoin = App.DataManager.upgradeFoodCoin[this.selectUpIndex][foodCurLevel]; if(App.DataManager.UserCoin < upgradeCoin){ App.Facade.popView(TipPanelMediator, TipPanel, "Not enough gold coins", false);//Not enough gold coins." 金币不足 } else{ App.DataManager.foodLevel[this.selectUpIndex]++; App.DataManager.UserCoin -= upgradeCoin; // 保存到本地 App.LocalStorageUtil.setNumber(App.LocalStorageUtil.lst_Coin, App.DataManager.UserCoin); App.LocalStorageUtil.setJsonObj(App.LocalStorageUtil.list_foodLevel, App.DataManager.foodLevel); App.Facade.popView(TipPanelMediator, TipPanel, "update successfully", false); App.Facade.getModel(hallModel).updateCoinNumber(); for (let i = 0; i < 6; i++) { let level = App.DataManager.foodLevel[i]; this.shengjiTishi[i].active = false; if(level < App.DataManager.maxFoodLevel){ // 升级所需金币 this.LabelCoinNum[i].string = App.DataManager.upgradeFoodCoin[i][level] + ""; if(App.DataManager.UserCoin > App.DataManager.upgradeFoodCoin[i][level]){ //提示可以升级 this.shengjiTishi[i].active = true; } } else{ this.btnList[i].interactable = false; this.LabelCoinNum[i].node.active = false; } } this.findUpGrade(); } } // update (dt) {} }