123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- 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);
- }
- },
-
- 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);
- }
- }
- },
- });
|