const ThemeManager = require("../utils/ThemeManger");
const { RoomState, GameNotificationKey, WechatShareType } = require("../utils/GameEnum");
const GameModule = require("../utils/GameModule");
const TapTapTool = require("../utils/TapTapTool");
const WeChat = require('../net/WeChat');
const HomeApi = require('../net/HomeApi');
const DWTool = require('../utils/DWTool');
cc.Class({
extends: cc.Component,
properties: {
// Public Properties
bgNode: cc.Node,
/** 当前显示的图片 */
buildSprite: cc.Sprite,
/** 柱子 */
pillarTop: cc.Sprite,
pillarBottom: cc.Sprite,
pillarRight: cc.Sprite,
pillarLeft: cc.Sprite,
bottomBg: cc.Sprite,
lockBtnFrames: [cc.SpriteFrame],
/** 升级按钮的两种状态图 */
updateBtnFrames: [cc.SpriteFrame],
/** 未解锁状态的节点 */
lockNode: cc.Node,
/** 需要花费所有金币 */
costLabel: cc.Label,
/** 未解锁的建筑名称 */
unLockBuildName: cc.RichText,
/** 未解锁需要花费多少金币 */
unlockRichText: cc.RichText,
/** 未解锁需要总部等级 */
unlockLevelLabel: cc.Label,
/** 建筑昵称 */
buildNameLabel: cc.Label,
/** 这里当做升级建筑按钮 */
updateBtn: cc.Node,
/** 看广告或者分享群免费升级 */
videoBtn: cc.Button,
videoRichText: cc.RichText,
/** 解锁按钮 */
lockBtn: cc.Sprite,
/** 将要解锁建筑的类型图 */
unlockBuildingType: cc.Sprite,
lockBottomNode: cc.Node,
/** 等级进度条 */
levelProgressBar: cc.ProgressBar,
/** 生产了多少金币 */
rateProgressLabel: cc.Label,
/** 下一级生产多少金币 */
nextRateLabel: cc.Label,
openDoorSkeletion: sp.Skeleton,
updateSkeletion: sp.Skeleton,
// 满级提示
maxNode: cc.Node,
//
artistNode: cc.Node,
//
artistMan: cc.Prefab,
awardWrap: cc.Node,
awardPrefab: cc.Prefab,
_itemId: 0,
skillEfcNode: cc.Node,
coupletLeftSrpite: cc.Sprite,
coupletRightSrpite: cc.Sprite,
//对联数组
coupletFrames: [cc.SpriteFrame]
},
// LIFE-CYCLE CALLBACKS:
onLoad () {
this.setupUI();
this.openDoorSkeletion.setCompleteListener(() => {
// 升级建筑
this.updateBuilding();
GameEvent.fire(GameNotificationKey.PlaySuccessAnimation, true);
});
//点击解锁建筑
let self = this;
this.unlockBuildingEvent = _.debounce(() => {
// 如果当前不够钱, 点击不生效
if (!TapTapTool.compare(GameModule.userInfo.gold, self.data.unlockScore)) {
return;
}
// 点完立刻隐藏
self.lockBtn.node.active = false;
let position = self.lockBtn.node.convertToWorldSpace(cc.v2(self.lockBtn.node.width / 2, self.lockBtn.node.height / 2));
GameEvent.fire(GameNotificationKey.PlayUpdateCoinAnimation, position);
self.openDoorSkeletion.setAnimation(1, "changjing_kaiqi2");
self.lockBottomNode.runAction(cc.fadeOut(0.7));
self.lockBottomNode.active = false;
}, 1000, true);
this.isFullUpdate = false;
this.longCount = 0;
//点击升级建筑
this.updateBtn.on(cc.Node.EventType.TOUCH_START, () => {
if (!this._isTouch) {
this._isTouch = true;
this.schedule(this.longUpdateBuilding, 0.3);
}
}, this);
this.updateBtn.on(cc.Node.EventType.TOUCH_CANCEL, () => {
this._isTouch = false;
this.unschedule(this.longUpdateBuilding, this);
this.isFullUpdate = false;
this.longCount = 0
}, this);
this.updateBtn.on(cc.Node.EventType.TOUCH_END, () => {
this._isTouch = false;
this.updateBuildingEvent();
this.unschedule(this.longUpdateBuilding, this);
this.isFullUpdate = false;
this.longCount = 0
}, this);
this.setupEventListener();
},
//长按升级建筑
longUpdateBuilding() {
this.updateBuildingEvent();
this.longCount += 1;
if (this.longCount > 5 && !this.isFullUpdate) {
this.unschedule(this.longUpdateBuilding, this);
this.isFullUpdate = true;
this.schedule(this.longUpdateBuilding, 0.2);
}
},
setupUI() {
this._adState = 0;
this.humanList = [];
this.humanPool = new cc.NodePool();
for (let i = 0; i < 5; i++) {
let artist = cc.instantiate(this.artistMan);
this.humanPool.put(artist);
this.humanList.push(artist);
}
},
setupEventListener() {
GameEvent.on(GameNotificationKey.AdUpdateStateNotification, this, (adState, callBack) => {
if (adState === 3 && callBack.type != undefined && callBack.type == 'updateRoom') {
this.finishVideoOrShare(callBack.roomId);
}
if (adState === 0 || adState === 1) {
this.initRoomAd();
}
this.videoBtn.interactable = true;
});
},
initRoomAd() {
if (!CC_WECHATGAME) {
return;
}
//// 说明有广告
if (GameGlobal._adVideoState == 0) {
this._adState = 2;
} else if (GameGlobal._adVideoState === 1) {
this._adState = 1;
}
this.refreshWatchVideoUI();
},
///观看广告或者分享获取升级建筑
watchVideoOrShare() {
if (CC_WECHATGAME) {
if (this._adState == 1) {
this.videoBtn.interactable = false;
GameEvent.on(GameNotificationKey.ShowShareAction, this, (type, isOk) => {
if (type == WechatShareType.UpRoomLevel) {
if (isOk) {
this.finishVideoOrShare(this.data.roomId);
} else {
this.videoBtn.interactable = true;
}
GameEvent.off(GameNotificationKey.ShowShareAction, this);
}
});
WeChat.shareAction(WechatShareType.UpRoomLevel, () => {
}, () => {
this.videoBtn.interactable = true;
console.log('分享失败或取消');
});
} else if (this._adState == 2) {
this.videoBtn.interactable = false;
let adObject = new Object();
adObject.type = 'updateRoom';
adObject.roomId = this.data.roomId;
GameGlobal._adVideo.showVideo(adObject);
}
}
},
finishVideoOrShare(roomId) {
if (this.data.roomId == roomId) {
HomeApi.reportInformation(2, () => {
GameGlobal._upRoomInfo.count += 1;
/// 时间重置为5分钟
GameGlobal._upRoomInfo.cdTime = 5 * 60 * 1000;
this.updateBuildingEvent(true);
this.refreshWatchVideoUI();
this.videoBtn.interactable = true;
}, (code, msg) => {
this.videoBtn.interactable = true;
});
}
},
//刷新观看视频或者分享文案
refreshWatchVideoUI() {
if (this._adState == 1) {
this.videoRichText.string = '分享到群\n免费升级';
} else if (this._adState == 2) {
this.videoRichText.string = '看广告\n免费升级';
}
},
//每帧更新数据
update (dt) {
if (this.data) {
// 不断刷新界面
this.layout(this.data);
}
},
updateItem(data, itemId, resetCallback) {
if (!isNaN(itemId)) {
// if (this._itemId == itemId && itemId != 0) {
// return;
// }
this._itemId = itemId;
}
if (typeof data != 'object') {
return;
}
if (resetCallback) {
this.resetCallback = resetCallback;
}
this.data = data;
this.buildNameLabel.string = `等级${data.level} ${data.name}` ;
this.refreshTheme();
this.layout(data);
// 配置界面上的奖励礼包
this.configAward();
//升级房间时不用刷新人物
if (!isNaN(itemId)) {
this.artistListLayout();
}
},
layout(data) {
// console.log(GameModule.skill.isUsingSkill3);
// 判断是否有下一级, 没有的话就是满级
if (data.hasNext === 1 && data.level < GameGlobal.BuildingManager.getLevelCount(data.roomId)) {
// 判断是否已经解锁
if (data.isUnlocked) {
let ratio = data.level % 25;
this.levelProgressBar.progress = ratio / 25;
//
let gold1 = TapTapTool.goldStrToClass(data.gold1);
let gold2 = TapTapTool.goldStrToClass(data.gold2);
let roomMt = TapTapTool.goldStrToClass(data.roomMt);
var secondGold = TapTapTool.multiple(gold1,data.level);
secondGold = TapTapTool.add(secondGold, gold2);
var nextGold = TapTapTool.multiple(gold1,(data.level+1));
nextGold = TapTapTool.add(nextGold, gold2);
let nextDiffer = TapTapTool.sub(nextGold, secondGold);
secondGold = TapTapTool.multiple(secondGold,roomMt);
secondGold = TapTapTool.multiple(secondGold, GameModule.skill.multiple);
secondGold = TapTapTool.multiple(secondGold, GameModule.userInfo.perpetualMt);
secondGold = TapTapTool.multiple(secondGold, {'n': GameModule.shop.multiple, 'e': 0});
this.rateProgressLabel.string = `${TapTapTool.parseToString(secondGold)}/秒`;
nextDiffer = TapTapTool.multiple(nextDiffer,roomMt);
nextDiffer = TapTapTool.multiple(nextDiffer, GameModule.skill.multiple);
nextDiffer = TapTapTool.multiple(nextDiffer, {'n': GameModule.shop.multiple, 'e': 0});
nextDiffer = TapTapTool.multiple(nextDiffer, GameModule.userInfo.perpetualMt);
this.nextRateLabel.string = `+${TapTapTool.parseToString(nextDiffer)}/秒`;
this.lockNode.active = false;
this.costLabel.string = TapTapTool.parseToString(data.nextUpGold);
// 判断是否有足够的金额解锁
if (TapTapTool.compare(GameModule.userInfo.gold, data.nextUpGold)) {
this.setState(RoomState.Update);
} else {
let isAd = DWTool.checkIsOldUser() && GameGlobal._upRoomInfo != undefined && GameGlobal._upRoomInfo.count < GameGlobal._upRoomInfo.maxCount && GameGlobal._upRoomInfo.cdTime <= 0;
if (isAd) {
this.setState(RoomState.WatchVideo);
} else {
this.setState(RoomState.UnLock);
}
}
} else {
this.lockNode.active = true;
this.costLabel.string = 0;
this.unLockBuildName.string = `${data.name}`;
let unLockData = TapTapTool.goldStrToClass(data.unlockScore);
this.unlockRichText.string = ` ${TapTapTool.parseToString(unLockData)}`;
// 判断是否有足够的金额解锁
if (TapTapTool.compare(GameModule.userInfo.gold, unLockData) && GameModule.userInfo.buildingLevel >= data.buildingLevel) {
this.unlockBuildingType.node.active = true;
this.lockBtn.spriteFrame = this.lockBtnFrames[1];
this.lockBtn.node.getComponent(cc.Button).interactable = true;
} else {
this.unlockBuildingType.node.active = false;
this.lockBtn.spriteFrame = this.lockBtnFrames[0];
this.lockBtn.node.getComponent(cc.Button).interactable = false;
}
if (data.buildingLevel > 0) {
this.unlockLevelLabel.node.active = true;
this.unlockLevelLabel.string = `需要总部等级${data.buildingLevel}级`;
} else {
this.unlockLevelLabel.node.active = false;
this.unlockLevelLabel.string = '';
}
this.setState(RoomState.Lock);
}
} else {
this.rate = data.rate;
this.levelProgressBar.progress = 1.0;
this.setState(RoomState.Full);
let gold1 = TapTapTool.goldStrToClass(data.gold1);
let gold2 = TapTapTool.goldStrToClass(data.gold2);
let roomMt = TapTapTool.goldStrToClass(data.roomMt);
var secondGold = TapTapTool.multiple(gold1,data.level);
secondGold = TapTapTool.add(secondGold, gold2);
secondGold = TapTapTool.multiple(secondGold,roomMt);
this.rateProgressLabel.string = `${TapTapTool.parseToString(secondGold)}/秒`;
}
//使用技能3的时候楼层增加对联效果
if (data.isUnlocked && GameModule.skill.isUsingSkill3) {
//大于0为单层显示对联
if (GameModule.skill.skill3Floor > 0) {
if (this.data.roomId % 2 == 0) {
this.skillEfcNode.active = false;
} else {
if (this.skillEfcNode.active == false) {
this.skillEfcNode.active = true;
let arr = this.randNum(0,(this.coupletFrames.length - 1),2);
this.coupletLeftSrpite.spriteFrame = this.coupletFrames[arr[0]];
this.coupletRightSrpite.spriteFrame = this.coupletFrames[arr[1]];
}
}
} else {
if (this.data.roomId % 2 == 0) {
if (this.skillEfcNode.active == false) {
this.skillEfcNode.active = true;
let arr = this.randNum(0,(this.coupletFrames.length - 1),2);
this.coupletLeftSrpite.spriteFrame = this.coupletFrames[arr[0]];
this.coupletRightSrpite.spriteFrame = this.coupletFrames[arr[1]];
}
} else {
this.skillEfcNode.active = false;
}
}
} else {
this.skillEfcNode.active = false;
}
},
randNum(min, max, num) {
var arr = [],
t;
function fn(i) {
for (i; i < num; i++) {
t = parseInt(Math.random() * (max - min + 1) + min);
for(var k in arr) {
if (arr[k] == t) {
fn(i);
break;
}
}
arr[i] = t;
}
}
fn(0);
return arr
},
//根据房间不同状态显示不同界面
setState(state) {
if (this.state === state) { return; }
switch (state) {
case RoomState.Lock:
this.openDoorSkeletion.node.active = true;
this.openDoorSkeletion.clearTracks();
this.openDoorSkeletion.setToSetupPose();
this.lockBtn.node.active = true;
this.lockBottomNode.stopAllActions();
this.lockBottomNode.opacity = 255;
this.lockBottomNode.active = true;
this.lockNode.active = true;
this.updateBtn.active = false;
this.updateBtn.getComponent(cc.Button).interactable = false;
this.updateBtn.getComponent(cc.Sprite).spriteFrame = this.updateBtnFrames[0];
this.maxNode.active = false;
this.videoBtn.node.active = false;
this._isTouch = false;
this.unschedule(this.longUpdateBuilding, this);
break;
case RoomState.UnLock:
this.openDoorSkeletion.node.active = false;
this.lockBtn.node.active = false;
this.lockBottomNode.active = false;
this.updateBtn.active = true;
this.lockNode.active = false;
this.updateBtn.getComponent(cc.Button).interactable = false;
this.updateBtn.getComponent(cc.Sprite).spriteFrame = this.updateBtnFrames[0];
this.maxNode.active = false;
this.videoBtn.node.active = false;
this._isTouch = false;
this.unschedule(this.longUpdateBuilding, this);
break;
case RoomState.Update:
this.openDoorSkeletion.node.active = false;
this.lockBtn.node.active = false;
this.lockBottomNode.active = false;
this.updateBtn.active = true;
this.lockNode.active = false;
this.updateBtn.getComponent(cc.Sprite).spriteFrame = this.updateBtnFrames[1];
this.updateBtn.getComponent(cc.Button).interactable = true;
this.maxNode.active = false;
this.videoBtn.node.active = false;
break;
case RoomState.Full:
this.openDoorSkeletion.node.active = false;
this.lockBtn.node.active = false;
this.lockBottomNode.active = false;
this.lockNode.active = false;
this.maxNode.active = true;
this.updateBtn.active = false;
this.videoBtn.node.active = false;
this._isTouch = false;
this.unschedule(this.longUpdateBuilding, this);
break;
case RoomState.WatchVideo:
this.openDoorSkeletion.node.active = false;
this.lockBtn.node.active = false;
this.lockBottomNode.active = false;
this.updateBtn.active = true;
this.lockNode.active = false;
this.updateBtn.getComponent(cc.Button).interactable = false;
this.updateBtn.getComponent(cc.Sprite).spriteFrame = this.updateBtnFrames[0];
this.maxNode.active = false;
this.updateBtn.active = false;
this.videoBtn.node.active = true;
this._isTouch = false;
this.unschedule(this.longUpdateBuilding, this);
break;
default:
break;
}
this.state = state;
},
refreshTheme() {
ThemeManager.setItemBuildSpriteFrame(this.data.roomId, this.buildSprite);
let lockBottomSprite = this.lockBottomNode.getComponent(cc.Sprite);
ThemeManager.setItemLockDownSpriteFrame(lockBottomSprite);
},
setListViewAdapter(listViewAdapter) {
this._listViewAdapter = listViewAdapter;
},
deleteItem() {
this._listViewAdapter.removeItem(this);
},
// 解锁建筑事件
unlockBuilding() {
this.unlockBuildingEvent();
},
updateBuildingEvent(isFree = false) {
if (this.state === RoomState.Update || this.state === RoomState.WatchVideo) {
let position = this.updateBtn.convertToWorldSpace(cc.v2(this.updateBtn.width / 2, this.updateBtn.height / 2));
GameEvent.fire(GameNotificationKey.PlayUpdateCoinAnimation, position);
this.updateBuilding(isFree);
if (!this.updateSkeletion.node.active) {
this.updateSkeletion.node.active = true;
this.updateSkeletion.setAnimation(0, "changjing_sj");
this.updateSkeletion.setCompleteListener(() => {
this.updateSkeletion.node.active = false;
});
}
}
},
// 升级建筑
updateBuilding(isFree) {
//判断是否免费升级
if (!isFree) {
//升级前判断金币是否足够
if (this.data.isUnlocked) {
if (!TapTapTool.compare(GameModule.userInfo.gold, this.data.nextUpGold)) {
return;
}
} else {
if (!TapTapTool.compare(GameModule.userInfo.gold, this.data.unlockScore)) {
return;
}
}
}
GameModule.audioMng.playUpdateBuilding();
// 从配置文件里获取
let maxLevel = GameGlobal.BuildingManager.getLevelCount(this.data.roomId);
let nextLevel = this.data.level + 1;
let level = nextLevel > maxLevel ? maxLevel : nextLevel;
let buildModel = GameGlobal.BuildingManager.getBuildingInfo(this.data.roomId, level);
buildModel.roomStars = this.data.roomStars;
buildModel.roomMt = this.data.roomMt;
buildModel.awardCount = this.data.awardCount;
buildModel.isUnlocked = 1;
if (this.resetCallback) {
this.resetCallback(buildModel, this._itemId);
}
if (this.awardScript) {
this.awardScript.roomLevel = level;
}
if (this.data.isUnlocked) {
// 当前楼层已解锁
if (!isFree) {
GameModule.userInfo.gold = TapTapTool.sub(GameModule.userInfo.gold, this.data.nextUpGold);
}
GameModule.userInfo.updateRecordModify(buildModel);
} else {
// 当前楼层未解锁
GameModule.userInfo.gold = TapTapTool.sub(GameModule.userInfo.gold, this.data.unlockScore);
GameModule.userInfo.recordUnlockModify.push(buildModel);
// 成功解锁后立刻调用上报,提交数据
GameModule.userInfo.doReport();
GameEvent.fire(GameNotificationKey.UnlockLevelHome,buildModel);
}
this.updateItem(buildModel);
GameEvent.fire(GameNotificationKey.RefreshBuildingData, buildModel);
},
artistListLayout() {
for (let child of this.humanList) {
this.humanPool.put(child);
}
let self = this;
let addHuman = function (artist, index) {
let human = null;
if (self.humanPool.size() > 0) {
human = self.humanPool.get();
} else {
human = cc.instantiate(self.artistMan);
self.humanList.push(human);
}
self.artistNode.addChild(human);
let direction = (index > 0) ? 1 : -1;
human.getComponent('ArtistMan').init(artist, direction);
};
if (this.data.isUnlocked && this.data.roomStars && this.data.roomStars.length > 0) {
let direction = (Math.random() - 0.5) * 2;
for(let i = 0; i < this.data.roomStars.length; ++i) {
let starId = this.data.roomStars[i];
let artist = new Object();
artist.starId = starId;
addHuman(artist, direction);
direction = -direction;
}
}
},
configAward() {
//每25级可以领取一次里程碑加成奖励
let awardCount = this.data.awardCount;
let totalCount = Math.floor(this.data.level / 25);
if (awardCount < totalCount) {
if (!this.isHasAward) {
let awardNode = cc.instantiate(this.awardPrefab);
this.awardScript = awardNode.getComponent('LevelHomeAward');
this.awardWrap.addChild(awardNode);
this.showProp();
} else {
if (parseInt(this.data.level) % 25 == 0) {
this.showProp();
}
}
} else {
if (this.awardScript) {
this.isHasAward = false;
this.awardScript.node.position = cc.v2(150, -10);
this.awardScript.node.active = false;
this.awardScript.isPlay = false;
this.awardScript.isPlaying = false;
}
}
},
hideAward() {
this.isHasAward = false;
this.awardScript.node.position = cc.v2(150, -10);
this.awardScript.node.active = false;
this.awardScript.isPlay = false;
this.awardScript.isPlaying = false;
this.configAward();
},
showProp(isPlayAnimation=true) {
if (!this.isHasAward) {
this.isHasAward = true;
this.awardScript.init(this.data.roomId, this.data.level, (awardCount) => {
// 显示领取动画
this.data.awardCount = awardCount;
this.hideAward();
});
if (isPlayAnimation) {
this.awardScript.showAnimation();
} else {
this.awardScript.node.position = cc.v2(150, -10);
this.awardScript.node.active = true;
}
} else {
this.awardScript.updateAnimation();
}
},
});