const DWTool = require('../utils/DWTool');
const ThemeManager = require("../utils/ThemeManger");
const { RoomState, GameNotificationKey } = require("../utils/GameEnum");
const GameModule = require("../utils/GameModule");
const TapTapTool = require("../utils/TapTapTool");
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,
/** 解锁按钮 */
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.updateBtn.on(cc.Node.EventType.TOUCH_START, () => {
if (!this._isTouch) {
/// 最起码要按住1秒
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);
this.updateBtn.on(cc.Node.EventType.TOUCH_END, () => {
this._isTouch = false;
this.updateBuildingEvent();
this.unschedule(this.longUpdateBuilding, this);
}, this);
},
//长按升级建筑
longUpdateBuilding() {
this.updateBuildingEvent();
},
setupUI() {
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);
}
},
start () {
},
//每帧更新数据
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);
secondGold = TapTapTool.multiple(secondGold,roomMt);
secondGold = TapTapTool.multiple(secondGold, GameModule.skill.multiple);
secondGold = TapTapTool.multiple(secondGold, GameModule.userInfo.perpetualMt);
this.rateProgressLabel.string = `${TapTapTool.parseToString(secondGold)}/秒`;
var nextGold = TapTapTool.multiple(gold1,(data.level+1));
nextGold = TapTapTool.add(nextGold, gold2);
nextGold = TapTapTool.multiple(nextGold,roomMt);
nextGold = TapTapTool.multiple(nextGold, GameModule.skill.multiple);
nextGold = TapTapTool.multiple(nextGold, GameModule.userInfo.perpetualMt);
let nextDiffer = nextGold = TapTapTool.sub(nextGold, secondGold);
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 {
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;
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;
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;
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;
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() {
if (this.state === RoomState.Update) {
let position = this.updateBtn.convertToWorldSpace(cc.v2(this.updateBtn.width / 2, this.updateBtn.height / 2));
GameEvent.fire(GameNotificationKey.PlayUpdateCoinAnimation, position);
this.updateBuilding();
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() {
//升级前判断金币是否足够
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.data.isUnlocked) {
// 当前楼层已解锁
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, (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();
}
},
});