const DWTool = require("../utils/DWTool");
const SkillApi = require('../net/SkillApi');
const GameModule = require("../utils/GameModule");
const {GameNotificationKey, WechatShareType } = require('../utils/GameEnum');
const WeChat = require('../net/WeChat');
const TapTapTool = require("../utils/TapTapTool");
const AlertManager = require('../utils/AlertManager');
const GameRedDot = require('../utils/GameEnum').GameRedDot;
const ADVideo = require('../utils/ADVideo');
var Promise = require('../lib/es6-promise').Promise;
const SkillState = cc.Enum({
/// 使用中
usingState: 1,
/// 技能cd状态
cdState: 2,
/// 技能可以使用的状态
canUseState: 3,
/// 技能锁住的状态
lockedState: 4
});
cc.Class({
extends: cc.Component,
properties: {
titleRichText: {
tooltip: '技能时间time ,锁住状态的text',
default: null,
type: cc.RichText
},
skillBg: {
tooltip: '技能的黑色遮罩',
default: null,
type: cc.Sprite
},
skillBgOutSide: {
tooltip: '技能的黑色外遮罩,这里只有是三个哦,最后一个刷新技能的是没有的',
default: null,
type: cc.Sprite
},
skillBgBottom: {
tooltip: '技能的底部黑色遮罩',
default: null,
type: cc.Sprite
},
button: cc.Button,
},
// LIFE-CYCLE CALLBACKS:
onLoad () {
///技能解锁啦
GameEvent.on("TopSkill_unLocked", this, (skillId) => {
if (this.skillInfo != undefined && skillId === this.skillInfo.skillId) {
this.setUpSkillState(SkillState.canUseState);
/// 技能等级 1
this.skillInfo.skillLevel = 1;
/// 设置为可以使用
this.skillInfo.skillStatus = SkillState.canUseState;
this.updataData();
let iconPath = './textures/skill/4000' + skillId;
AlertManager.showCommonAlert(iconPath, this._skillData.desc, this._skillData.name);
let iconId = 900005 + skillId
let objct = {'cdTime': 0, 'infoDesc': `${this._skillData.name}可使用`, 'name': this._skillData.name, 'icon': iconId, 'sId': skillId, 'type': 2, 'skillStatus': 0};
GameGlobal._timeInformations.push(objct);
GameEvent.fire(GameNotificationKey.GameUpdateMessageList, 1, true);
/// 设置重置技能可用
} else if (this.skillInfo == undefined && this._isAlllLocked) {
this._isAlllLocked = false;
this.setUpSkillState(SkillState.canUseState);
}
});
},
start () {
},
onDestroy() {
GameEvent.off("TopSkill_unLocked", this);
GameEvent.off(GameNotificationKey.ResetSkill, this);
GameEvent.off(GameNotificationKey.UpBuildingLevel, this);
GameEvent.off("skill_six_use", this);
GameEvent.off(GameNotificationKey.ShowShareAction, this);
this.unschedule(this.updateTime, this);
},
init(skillInfo) {
if (this.skillInfo == undefined) {
this.setupTopSkillNotification();
}
this.skillInfo = skillInfo;
let skillData = GameGlobal.BuildingManager.getSkillInfo(skillInfo.skillId);
this._skillData = skillData;
if (skillInfo.isHave == 0) {
this.setUpSkillState(SkillState.lockedState);
this.titleRichText.string = "
等级" + skillData.buildingLevel + '';
return;
}
/// 如果不是第二个技能那么去更新信息流时间
if (skillInfo.skillId != 2) {
for (let i = 0; i < GameGlobal._timeInformations.length; ++ i) {
let information = GameGlobal._timeInformations[i]
/// 如果是第一个技能或者第三个技能则更新它的时间
if (information.type == 2 && information.sId == skillInfo.skillId) {
/// 技能使用cd中
if (this.skillInfo.skillStatus == 1) {
information.cdTime = skillInfo.cdStarTime;
} else {
information.cdTime = 0;
}
}
}
}
this.updataData();
},
setupTopSkillNotification() {
GameEvent.on(GameNotificationKey.ResetSkill, this, () => {
// console.log(this.skillInfo);
if (this.skillState !== SkillState.lockedState) {
this.setUpSkillState(SkillState.canUseState);
}
let isNew = true;
for (let i = 0; i < GameGlobal._timeInformations.length; ++ i) {
let information = GameGlobal._timeInformations[i]
/// 如果是第一个技能或者第三个技能则更新它的时间
if (information.type == 2 && information.sId == this.skillInfo.skillId) {
information.cdTime = 0;
/// 可以使用
information.skillStatus = 0;
information.infoDesc = `${this.skillInfo.name}可使用`;
isNew = false;
///那么就是没有用过嘛 直接固定在那里
}
}
if (isNew) {
let iconId = 900005 + this.skillInfo.skillId
let objct = {'cdTime': 0, 'infoDesc': `${this.skillInfo.name}可使用`, 'name': this.skillInfo.name, 'icon': iconId, 'sId': this.skillInfo.skillId, 'type': 2, 'skillStatus': 0};
GameGlobal._timeInformations.push(objct);
GameEvent.fire(GameNotificationKey.GameUpdateMessageList, 1, true);
}
});
/// 第六个技能使用 需要更新cd时间
GameEvent.on("skill_six_use", this, () => {
let lastLevelInfo = GameGlobal.BuildingManager.getSkillLevelInfo(6, GameGlobal.rcdSkillLevel);
this.cd = this._levelInfo.cd * (1 - lastLevelInfo.rcd / 100);
});
GameEvent.on(GameNotificationKey.UpdateTimeSkill, this, (skillLevelInfo) => {
if (this.skillState === SkillState.canUseState && this.skillInfo.skillId == skillLevelInfo.skillId) {
this.updataData();
}
});
},
updataData() {
let levelInfo = GameGlobal.BuildingManager.getSkillLevelInfo(this.skillInfo.skillId, this.skillInfo.skillLevel);
this._levelInfo = levelInfo;
if (GameGlobal.rcdSkillLevel == 0) {
this.cd = levelInfo.cd;
} else {
let lastLevelInfo = GameGlobal.BuildingManager.getSkillLevelInfo(6, GameGlobal.rcdSkillLevel);
this.cd = levelInfo.cd * (1 - lastLevelInfo.rcd / 100);
}
/// 技能cd
/// 持续时间
let td = levelInfo.td;
let hasCd = this.skillInfo.cdStarTime / 1000;
this.td = td;
/// 技能使用cd中
if (this.skillInfo.skillStatus == 2) {
this.setUpSkillState(SkillState.cdState);
this.timeLabelCount = hasCd;
this.titleRichText.string = DWTool.calculateTime(hasCd);
this.skillBg.fillRange = - hasCd / this.cd;
/// 技能使用中
} else if (this.skillInfo.skillStatus == 1) {
this.setUpSkillState(SkillState.usingState);
this.timeLabelCount = hasCd;
this.titleRichText.string = DWTool.calculateTime(hasCd);
this.skillBgOutSide.fillRange = - hasCd / td;
} else {
this.setUpSkillState(SkillState.canUseState);
}
},
/// 初始化刷新技能的item
initUpdateSkill(isAllLocked) {
///是否所有的技能锁住
this._isAlllLocked = isAllLocked;
// 20分钟
this.cd = 15 * 60;
if (isAllLocked) {
this.setUpSkillState(SkillState.lockedState);
return;
}
let resetSkillTime = GameGlobal.userData.resetSkillTime;
let timestamp = new Date().getTime();
let totalCd = (timestamp - resetSkillTime) / 1000;
if (this.cd <= totalCd) {
this.setUpSkillState(SkillState.canUseState);
} else {
this.setUpSkillState(SkillState.cdState);
this.skillBg.fillRange = totalCd / this.cd - 1;
this.timeLabelCount = this.cd - totalCd;
this.titleRichText.string = DWTool.calculateTime(this.timeLabelCount);
}
},
updateTime() {
if (this.skillState === SkillState.canUseState || this.skillState === SkillState.lockedState) {
return;
}
/// 使用中的状态
if (this.skillState === SkillState.usingState) {
this.timeLabelCount -= 1;
this.skillBgOutSide.fillRange += 1 / this.td;
if(this.skillBgOutSide.fillRange >= 0 || this.timeLabelCount <= 0) {
this.setUpSkillState(SkillState.cdState);
this.timeLabelCount = this.cd;
this.skillBg.fillRange = - 1;
this.titleRichText.string = DWTool.calculateTime(this.timeLabelCount);
} else {
this.titleRichText.string = DWTool.calculateTime(this.timeLabelCount);
}
/// 技能cd 的状态
} else {
this.skillBg.fillRange += 1 / this.cd;
this.timeLabelCount -= 1;
if (this.skillBg.fillRange >= 0 || this.timeLabelCount <= 0) {
this.setUpSkillState(SkillState.canUseState);
}else {
this.titleRichText.string = DWTool.calculateTime(this.timeLabelCount);
}
}
},
setUpSkillState(skillState) {
this.skillState = skillState;
this.skillBg.node.active = skillState !== SkillState.canUseState;
if (this.skillBgOutSide != null) {
this.skillBgOutSide.node.active = skillState === SkillState.usingState;
}
this.skillBgBottom.node.active = skillState !== SkillState.canUseState;
this.titleRichText.node.active = skillState !== SkillState.canUseState;
this.button.interactable = skillState === SkillState.canUseState;
},
sendNotification() {
let skillId = this.skillInfo.skillId;
let skillLevelInfo = GameGlobal.BuildingManager.getSkillLevelInfo(skillId, this.skillInfo.skillLevel);
GameEvent.fire(GameNotificationKey.UseTimeSkill, skillLevelInfo);
// cdTime 剩余时间 是 [long] 查看
// 5 desc 描述 是 [string] 查看
// 6 name 名称 是 [string] 查看
if (skillId == 2) {
//显示使用技能效果
let gold = TapTapTool.multiple(GameModule.userInfo.coinTap, {'n': skillLevelInfo.mt, 'e': 0});
let text = TapTapTool.parseToString(gold);
AlertManager.showSkill2Efc(text);
let isNew = true;
for (let i = 0; i < GameGlobal._timeInformations.length; ++ i) {
let information = GameGlobal._timeInformations[i];
if (information.type == 2 && information.sId == 2) {
/// 如果是已经使用过的直接刷新时间就可以啦
information.cdTime = -6 * 1000;
information.skillStatus = 1;
information.infoDesc = `获得金币${text}`;
isNew = false;
break;
}
}
if (isNew) {
let objct = {'cdTime': -6 * 1000, 'infoDesc': `获得金币${text}`, 'name': this.skillInfo.name, 'icon': 900007, 'sId': 2, 'type': 2, 'skillStatus': 1};
GameGlobal._fixInformations.push(objct);
GameEvent.fire(GameNotificationKey.GameUpdateMessageList, 1, false);
}
} else {
this.handelSkillTimeInformation(skillLevelInfo, skillId);
}
},
//// 处理第一个和第三个实时技能的信息流数据
handelSkillTimeInformation(skillLevelInfo, skillId) {
let isNew = true;
let infoDesc = this._skillData.infoDesc;
let skillLevel = this.skillInfo.skillLevel;
/// 第一个技能
if (skillId === 1) {
let num = skillLevel > 11 ? (10 + skillLevel - 11) : 10;
infoDesc = infoDesc.replace('{num}', num);
/// 第三个技能
} else {
infoDesc = infoDesc.replace('${num}', skillLevelInfo.mt * 100);
}
for (let i = 0; i < GameGlobal._timeInformations.length; ++ i) {
let information = GameGlobal._timeInformations[i];
if (information.type == 2 && information.sId == skillId) {
/// 如果是已经使用过的直接刷新时间就可以啦
information.cdTime = skillLevelInfo.td * 1000;
information.skillStatus = 1;
information.infoDesc = infoDesc;
isNew = false;
break;
}
}
if (isNew){
let objct = {'cdTime': skillLevelInfo.td * 1000, 'infoDesc': infoDesc, 'name': this.skillInfo.name, 'icon': 900005 + skillId, 'sId': skillId, 'type': 2, 'skillStatus': 1};
GameGlobal._timeInformations.push(objct);
GameEvent.fire(GameNotificationKey.GameUpdateMessageList, 1, true);
}
},
skillAction1() {
GameModule.audioMng.playClickButton();
this.button.interactable = false;
this.useSkill(this.skillInfo.skillId).then(() => {
this.button.interactable = true;
this.setUpSkillState(SkillState.usingState);
this.timeLabelCount = this.td;
this.titleRichText.string = DWTool.calculateTime(this.td);
this.skillBgOutSide.fillRange = -1;
this.skillBg.fillRange = -1;
this.sendNotification();
}).catch(({code, msg}) => {
console.log(code, msg);
this.button.interactable = true;
});
},
skillAction2() {
GameModule.audioMng.playClickButton();
this.button.interactable = false;
this.useSkill(this.skillInfo.skillId).then(() => {
this.button.interactable = true;
this.setUpSkillState(SkillState.cdState);
this.timeLabelCount = this.cd;
this.titleRichText.string = DWTool.calculateTime(this.cd);
this.skillBg.fillRange = -1;
this.sendNotification();
}).catch(({code, msg}) => {
console.log(code, msg);
this.button.interactable = true;
});
},
skillAction3() {
GameModule.audioMng.playClickButton();
this.button.interactable = false;
this.useSkill(this.skillInfo.skillId).then(() => {
this.button.interactable = true;
this.setUpSkillState(SkillState.usingState);
this.timeLabelCount = this.td;
this.titleRichText.string = DWTool.calculateTime(this.td);
this.skillBgOutSide.fillRange = -1;
this.skillBg.fillRange = -1;
this.sendNotification();
GameEvent.fire("skill3_use_begain");
}).catch(({code, msg}) => {
console.log(code, msg);
this.button.interactable = true;
});
},
skillAgainAction() {
GameModule.audioMng.playClickButton();
if (CC_WECHATGAME) {
this.button.interactable = false;
ADVideo.wxPlayADVideo('adunit-72f6285169164939', () => {
this.button.interactable = true;
}, (isFinished) => {
if (isFinished) {
this.shareActionCallback();
} else {
this.button.interactable = true;
}
});
} else if (CC_QQPLAY) {
this.button.interactable = false;
ADVideo.qqPlayADVideo(() => {
this.button.interactable = true;
}, (isFinished) => {
if (isFinished) {
this.shareActionCallback();
} else {
this.button.interactable = true;
}
});
}
},
shareActionCallback() {
this.resetSkill().then((respondData) => {
GameGlobal.userData.resetSkillTime = respondData.curSystemTime;
this.setUpSkillState(SkillState.cdState);
this.timeLabelCount = this.cd;
this.titleRichText.string = DWTool.calculateTime(this.cd);
this.skillBg.fillRange = -1;
GameEvent.fire(GameNotificationKey.ResetSkill);
}).catch(({code, msg}) => {
console.log(code, msg);
this.button.interactable = true;
});
GameEvent.off(GameNotificationKey.ShowShareAction, this);
},
/// 网络请求
useSkill(skillId) {
return new Promise((resolve, reject) => {
// 获取目标用户的建筑
SkillApi.useSkill(skillId, (respondData) => {
/// 如果取消了红点显示
// if (respondData.isCancel) {
// /// 去掉因为技能 可以用的红点条件
// TapTapTool.removeRedDot(GameRedDot.skill);
// }
resolve(respondData);
}, (code, msg) => {
reject({code, msg});
});
});
},
resetSkill() {
return new Promise((resolve, reject) => {
// 获取目标用户的建筑
SkillApi.resetSkill((respondData) => {
resolve(respondData);
}, (code, msg) => {
reject({code, msg});
});
});
},
});