const DWTool = require("../utils/DWTool"); const DWAlert = require("../utils/DWAlert"); const AlertManager = require('../utils/AlertManager'); const { GameNotificationKey, ArtistTrainItemSkillStyle } = require('../utils/GameEnum'); const ArtistTrainApi = require('../net/ArtistTrainApi'); const GameModule = require('../utils/GameModule') var AristTrainState = cc.Enum({ Invalid: 0, Stay: 1, Training: 2, Completion: 3 }); cc.Class({ extends: cc.Component, properties: { trainBgSprite: cc.Sprite, trainSprite: cc.Sprite, trainBtn: cc.Button, trainBtnSprite: cc.Sprite, trainBtnSpriteFrames: [cc.SpriteFrame], contentLabel: cc.Label, trainCostNode: cc.Node, costLabel: cc.Label, recuperateLabel: cc.Label, sandclockLabel: cc.Label, speedUpBtn: cc.Button, confirmBtn: cc.Button, trainSlider: cc.Slider, trainSliderTimeLabel: cc.Label, trainProgress: cc.Sprite, // 技能 skillNode: cc.Node, diamondRefreshLabel: cc.Label, countDown: { get: function() { if (!this._countDown) { this._countDown = 0; } return this._countDown; }, set: function (value) { this._countDown = value; this.trainSliderTimeLabel.string = DWTool.calculateTime(this._countDown); this._preCountDown = this._countDown; } }, }, init(parent, data, levelZIndex) { console.log(data); if (arguments.length < 3) { throw new Error("init Missing parameter..."); } this.parent = parent; this.targetUid = parent.targetUid; this.data = data; this.missionId = data.id this.diamond = data.diamond; this.ticket = data.ticket; this.coin = data.coin; this.levelZIndex = levelZIndex; this.contentLabel.string = data.msg; this.recuperateLabel.string = data.ticket; if(data.ticket > GameModule.userInfo.ticket) { this.recuperateLabel.node.color = new cc.Color(255, 0, 0); } this.costLabel.string = DWTool.coinParse(data.coin); if(data.coin > GameModule.userInfo.grossIncome) { this.costLabel.node.color = new cc.Color(255, 0, 0); } this.sandclockLabel.string = DWTool.calculateTime(data.cd / 1000); let trainEnable = GameModule.userInfo.grossIncome > data.coin && GameModule.userInfo.ticket > data.ticket if (trainEnable) { this.trainBtn.getComponent(cc.Button).interactable = true; this.trainBtnSprite.spriteFrame = this.trainBtnSpriteFrames[1]; } else { this.trainBtn.getComponent(cc.Button).interactable = false; this.trainBtnSprite.spriteFrame = this.trainBtnSpriteFrames[0]; } let timestamp = Date.parse(new Date()); if (timestamp > data.cdEnd) { this.trainSlider.progress = 1.0; this.trainSliderTimeLabel.string = DWTool.calculateTime(0); } else { let startTime = data.cdEnd - data.cd; this._currentTime = (timestamp - startTime) / 1000; this.countDown = data.cd / 1000; } this.setSkillLayout(); this.diamondRefreshLabel.string = `${data.diamond} 加速`; DWTool.loadResSpriteFrame(`./artistTrain/${data.quality}`) .then((result) => { this.trainBgSprite.spriteFrame = result; }); DWTool.loadResSpriteFrame(`./artistTrain/${data.picId}`) .then((result) => { this.trainSprite.spriteFrame = result; }); if (data.status == 0) { this.setState(AristTrainState.Stay); } else if (data.status == 1) { this.setState(AristTrainState.Training); } else if (data.status == 2) { this.setState(AristTrainState.Completion); } else { this.setState(AristTrainState.Invalid); } }, // LIFE-CYCLE CALLBACKS: onLoad () { this._width = this.trainProgress.node.width; this.showRechargeEvent = _.debounce(() => { AlertManager.showRechargeAlert(this.levelZIndex); }, 1000, true); }, startTrain() { if (this.state === AristTrainState.Stay) { GameModule.userInfo.updateUserRes({ grossIncome: -this.coin, ticket: -this.ticket }) this.parent.clickStartTrain(this.missionId) } }, setSkillLayout() { DWTool.loadResPrefab("./prefabs/artist_train_item_skill") .then((prefab) => { if (this.data.addCharm != 0) { let item = cc.instantiate(prefab); this.skillNode.addChild(item); item.getComponent('ArtistTrainItemSkill').init(ArtistTrainItemSkillStyle.Charm, this.data.addCharm); } if (this.data.addAbility != 0) { let item = cc.instantiate(prefab); this.skillNode.addChild(item); item.getComponent('ArtistTrainItemSkill').init(ArtistTrainItemSkillStyle.Ability, this.data.addAbility); } if (this.data.addEffect != 0) { let item = cc.instantiate(prefab); this.skillNode.addChild(item); item.getComponent('ArtistTrainItemSkill').init(ArtistTrainItemSkillStyle.Effect, this.data.addEffect); } }); }, handleSpeedUp() { ArtistTrainApi.missionSpeedUp(this.targetUid, this.missionId, () => { // 加速成功 this.countDown = this._currentTime + 0.2; GameModule.userInfo.updateUserRes({ diamond: -this.diamond }) }, (errCode, errMsg) => { if(CC_WECHATGAME) { wx.showModal({ title: "提示", content: errMsg }) } else { alert(errMsg) } }); }, confirm() { this.parent.clickCompleteTrain(this.missionId) AlertManager.showArtistTrainCompletion(this.data, this.levelZIndex); }, setState(state) { if (this.state === state) { return; } switch (state) { case AristTrainState.Stay: this.trainBtn.node.active = true; this.speedUpBtn.node.active = false; this.trainSlider.node.active = false; this.trainCostNode.active = true; this.confirmBtn.node.active = false; this.trainSliderTimeLabel.node.active = false; break; case AristTrainState.Training: this.trainBtn.node.active = false; this.speedUpBtn.node.active = true; this.trainSlider.node.active = true; this.trainCostNode.active = false; this.confirmBtn.node.active = false; this.trainSliderTimeLabel.node.active = true; break; case AristTrainState.Completion: this.trainBtn.node.active = false; this.speedUpBtn.node.active = false; this.trainCostNode.active = false; this.trainSlider.node.active = true; this.confirmBtn.node.active = true; this.trainSliderTimeLabel.node.active = false; break; case AristTrainState.Invalid: this.trainBtn.node.active = false; this.speedUpBtn.node.active = false; this.trainCostNode.active = false; this.trainSlider.node.active = false; this.confirmBtn.node.active = false; this.trainSliderTimeLabel.node.active = false; break; default: break; } this.state = state; }, update(dt) { if (this.state === AristTrainState.Training) { // 进度条走完, 开始生产金币 if (Math.floor(this.trainSlider.progress) != 1) { this._currentTime += dt; if (Math.floor(this._currentTime) > this.countDown) { this._currentTime = this.countDown; } let progress = this._currentTime / this.countDown; if (Math.floor(progress) === 1) { this.trainSlider.progress = 1; } else { this.trainSlider.progress = progress; } let resultCountDown = this.countDown - Math.floor(this._currentTime); if (this._preCountDown !== resultCountDown) { this.trainSliderTimeLabel.string = DWTool.calculateTime(resultCountDown); this._preCountDown = resultCountDown; } this.trainProgress.node.width = this._width * this.trainSlider.progress; } else { this.setState(AristTrainState.Completion); } } }, });