const DWTool = require('../utils/DWTool');
const { RoomState, GameNotificationKey, LevelHomeArtistItemStyle } = require("../utils/GameEnum");
const GameModule = require("../utils/GameModule");
const ThemeManager = require("../utils/ThemeManger");
const HomeApi = require("../net/HomeApi");
// const StateMachine = require('../lib/StateMachine');
// const StateMachineHistory = require('../lib/StateMachineHistory');
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,
/** 建筑昵称 */
buildNameLabel: cc.Label,
/** 这里当做升级建筑按钮 */
updateBtn: cc.Sprite,
/** 解锁按钮 */
lockBtn: cc.Sprite,
/** 将要解锁建筑的类型图 */
unlockBuildingType: cc.Sprite,
lockBottomNode: cc.Node,
/** 等级节点 */
levelProgressNode: cc.Node,
/** 等级进度条 */
levelProgressBar: cc.ProgressBar,
/** 等级 */
levelProgressLabel: cc.Label,
/** 生产金币节点 */
rateProgressNode: cc.Node,
/** 生产金币进度条 */
rateProgressBar: cc.ProgressBar,
/** 生产了多少金币 */
rateProgressLabel: cc.Label,
/** 倒计时 */
countdownLabel: cc.Label,
/** 金币满了提示 */
coinFullTip: cc.Node,
artistList: cc.Node,
artistListItem: cc.Prefab,
openDoorSkeletion: sp.Skeleton,
updateSkeletion: sp.Skeleton,
// 显示加成的节点
additionNode: cc.Node,
// 显示加成倍数
additionLabel: cc.Label,
// 加成的骨骼
additionSkeleton: sp.Skeleton,
// 满级提示
maxNode: cc.Node,
//入驻艺人提醒
residentTipNode: cc.Node,
// 加成倍数,默认倍率为x1,即无加成
addition: {
get: function () {
if (!this._addition) {
this._addition = 1;
}
return this._addition;
},
set: function (value) {
this._addition = value;
if (this._addition === 1) {
this.additionNode.active = false;
// this.additionSkeleton.setAnimation(0, 'jiasutiao_2', true);
// this.rateProgressBar.barSprite.node.active = true;
} else {
this.additionNode.active = true;
this.additionLabel.string = `X${this._addition}`;
// this.rateProgressBar.barSprite.node.active = false;
}
}
},
coinArrayMax: {
default: 3,
type: cc.Integer,
tooltip: "当前楼层最大存储金币数"
},
coinWrap: cc.Node,
propWrap: cc.Node,
coinPrefab: cc.Prefab,
propPrefab: cc.Prefab,
countDown: {
get: function () {
if (!this._countDown) {
this._countDown = 0;
}
return this._countDown;
},
set: function (value) {
this._countDown = value;
this.countdownLabel.string = DWTool.calculateTime(this._countDown);
this._preCountDown = this._countDown;
}
},
rate: {
get: function () {
if (!this._rate) {
this._rate = 0;
}
return this._rate;
},
set: function (value) {
this._rate = value * this.addition;
this.rateProgressLabel.string = DWTool.coinParse(this._rate);
}
},
notPickupCount: {
get: function () {
if (!this._notPickupCount) {
this._notPickupCount = 0;
}
return this._notPickupCount;
},
set: function (value) {
this._notPickupCount = value;
this.buildingInfo.coinCount = value;
this.isNeedToReport = true;
}
},
},
// LIFE-CYCLE CALLBACKS:
onLoad() {
this.isNeedToReport = false;
this.coinArray = [];
this._currentTime = 0;
this.isFirstLoad = true;
this.humanList = [];
this.isHasProp = false;
this.updateSkeletion.node.active = false;
this._rateProgressWidth = this.rateProgressBar.barSprite.node.width;
this.openDoorSkeletion.setCompleteListener(() => {
// 升级建筑
this.updateBuilding();
GameEvent.fire(GameNotificationKey.PlaySuccessAnimation);
});
let self = this;
this.unlockBuildingEvent = _.debounce(() => {
// 如果当前不够钱, 点击不生效
if (GameModule.userInfo.grossIncome < self.buildingInfo.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));
// this.lockBottomNode.active = false;
}, 1000, true);
// 监听自定义事件
this.setEventListener();
// 升级建筑事件
this.updateBtn.node.on(cc.Node.EventType.TOUCH_END, () => {
this.updateBuildingEvent();
}, this);
this.schedule(() => {
if (this.isNeedToReport) {
this.isNeedToReport = false;
GameModule.userInfo.updateRecordModify(this.buildingInfo);
}
}, 2.0);
this.guideEvent = _.debounce(() => {
GameModule.homeGuide.getComponent('HomeGuide').handleGuideState17(this.artists);
}, 1000, true);
// 配置界面上的金币
this.configCoins();
},
onDestroy() {
GameEvent.off(GameNotificationKey.ResidentArtist, this);
GameEvent.off(GameNotificationKey.RefreshLevelHomeArtistList, this);
GameEvent.off(GameNotificationKey.LevelHomeItemBuildingAllFull, this);
},
setEventListener() {
// 这个是入驻艺人成功时调用的
GameEvent.on(GameNotificationKey.ResidentArtist, this, (uid, buildingId) => {
if (this.node.active && this.uid === uid && this.buildingInfo.buildingId === buildingId) {
HomeApi.friendGetArtistsInBuilding(this.uid, this.buildingInfo.buildingId, (data) => {
// 为了不让当前的金币出现多种不同金额的币种, 当列表刷新时, 要收取一次
if (this.state === RoomState.Update || this.state === RoomState.Full) {
// 清空当前用来存储金币节点
this.currentCoin = null;
for (let i = 0; i < this.coinArrayMax; i++) {
this.pickupCoin(this.coinArray[i], false);
}
}
}, (code, msg) => {
console.log(msg);
})
}
});
// 这个是召回驱赶艺人时调用的
GameEvent.on(GameNotificationKey.RefreshLevelHomeArtistList, this, (uid, buildingId) => {
if (this.node.active && this.uid === uid && this.buildingInfo.buildingId === buildingId) {
HomeApi.friendGetArtistsInBuilding(this.uid, this.buildingInfo.buildingId, (data) => {
this.artists = data.list || [];
this.artistListLayout();
}, (code, msg) => {
console.log(msg);
})
}
});
// 所有建筑满级之后调用
GameEvent.on(GameNotificationKey.LevelHomeItemBuildingAllFull, this, () => {
if (this.isFirstLoad) { // 代表游戏一打开, 就是满级的
this.currentCoin = null;
this.coinWrap.active = false;
this.artistList.active = false;
this.coinFullTip.active = false;
} else {
GameModule.userInfo.updateRecordModify(this.buildingInfo);
this.currentCoin = null;
for (let i = 0; i < this.coinArrayMax; i++) {
this.pickupCoin(this.coinArray[i]);
}
// 需要把艺人也都清空了
for (let child of this.humanList) { child.destroy(); }
this.coinWrap.active = false;
this.artistList.active = false;
this.coinFullTip.active = false;
}
})
// 离线收益金币动画
// 只显示最底部一层的金币收取
// 其他楼层金币直接隐藏
GameEvent.on(GameNotificationKey.HandleOfflineIncomeAnim, this, () => {
// 清空当前用来存储金币节点
this.currentCoin = null;
let showAnim = this.index == 5 ? true : false
for (let i = 0; i < this.coinArrayMax; i++) {
this.pickupCoinWithNoIncom(this.coinArray[i], showAnim);
}
})
},
// 解锁建筑事件
unlockBuilding() {
this.unlockBuildingEvent();
},
updateBuildingEvent() {
if (this.state === RoomState.Update) {
// 清空当前用来存储金币节点
this.currentCoin = null;
for (let i = 0; i < this.coinArrayMax; i++) {
this.pickupCoin(this.coinArray[i]);
}
let position = this.updateBtn.node.convertToWorldSpace(cc.v2(this.updateBtn.node.width / 2, this.updateBtn.node.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;
});
// }
// 如果好友列表功能已经开放
if (GameModule.tab) {
this.artists = GameModule.tab.getComponent('Tab').artists;
// 尝试触发艺人入驻引导
// this.guideEvent();
}
}
},
configCoins() {
for (let i = 0; i < this.coinArrayMax; i++) {
// 初始化
let coinNode = cc.instantiate(this.coinPrefab)
this.coinWrap.addChild(coinNode);
coinNode = coinNode.getComponent("LevelHomeCoin");
coinNode.index = i;
this.coinHide(coinNode)
this.coinArray[i] = coinNode;
coinNode.node.on(cc.Node.EventType.TOUCH_END, (event) => {
this.pickupCoin(coinNode);
}, this);
}
},
configProp(propData) {
if (!this.isHasProp) {
let propNode = cc.instantiate(this.propPrefab)
this.propScript = propNode.getComponent('LevelHomePropItem')
this.isHasProp = true;
this.propScript.init(this.buildingInfo.buildingId, propData, () => {
this.propScript = null;
this.isHasProp = false;
});
this.propWrap.addChild(propNode);
} else {
this.propScript.updateProp(propData)
}
},
/**
* Public Method, 用来设置建筑背景图
* @param {number} cityId 城市id
* @param {number} index 楼层
*/
init(cityId, index) {
if (arguments.length < 1) {
throw new Error("init Missing parameter...");
}
this.cityId = cityId;
this.index = index;
this.isFirstLoad = true;
ThemeManager.setBuildItemColor(this.cityId, this.bgNode);
ThemeManager.setItemBuildSpriteFrame(this.cityId, this.buildSprite, index);
ThemeManager.setItemPillarTopSpriteFrame(this.cityId, this.pillarTop);
ThemeManager.setItemPillarBottomSpriteFrame(this.cityId, this.pillarBottom);
ThemeManager.setItemPillarRightSpriteFrame(this.cityId, this.pillarRight);
ThemeManager.setItemPillarLeftSpriteFrame(this.cityId, this.pillarLeft);
ThemeManager.setItemDownSpriteFrame(this.cityId, this.bottomBg);
let lockBottomSprite = this.lockBottomNode.getComponent(cc.Sprite);
ThemeManager.setItemLockDownSpriteFrame(this.cityId, lockBottomSprite);
},
/**
* Public Method, 配置建筑的内部样式
* @param {*} buildingInfo 建筑信息
* @param {*} uid 当前用户的uid
*/
config(buildingInfo, uid, unlockBuildingRecord, resetCallback) {
// console.log(buildingInfo.coinCount);
this.unlockBuildingRecord = unlockBuildingRecord;
if (resetCallback) {
this.resetCallback = resetCallback;
}
if (buildingInfo.buildingId == 1) {
if (buildingInfo.level == 0) {
// 监听完成引导系统state1的事件
GameModule.homeGuide.on('Fire_state1', this.unlockBuildingEvent, this)
// 触发引导系统state1状态
GameModule.homeGuide.getComponent('HomeGuide').handleState('state1')
}
}
this.buildingInfo = buildingInfo;
this.uid = uid;
this.artists = this.buildingInfo.artists;
// 这里设置一些只需要设置一次的数据
this.countDown = buildingInfo.rateUnit;
this.buildNameLabel.string = buildingInfo.name;
this._notPickupCount = buildingInfo.coinCount;
// console.log(buildingInfo.coinCount, this.coinArrayMax);
if (buildingInfo.coinCount === this.coinArrayMax * 10) {
this.rateProgressBar.progress = 1;
this.countdownLabel.string = DWTool.calculateTime(0);
}
this.levelProgressBar.progress = buildingInfo.level / Global.BuildingManager.getLevelCount(buildingInfo.buildingId);
this.levelProgressLabel.string = `LV.${buildingInfo.level}`;
if (this.isFirstLoad) {
this.isFirstLoad = false;
this.artistListLayout();
}
//城市开发完毕的时候不显示艺人入驻按钮
if (GameModule.userInfo.levelHomeItemFullCount != 5 && this.cityId === Global.devCityId) {
this.artistList.active = true;
} else {
this.artistList.active = false;
}
this.initializeCoinPosition();
this.layout(buildingInfo);
},
/** 初始化金币的位置 */
initializeCoinPosition() {
// 2018年08月18日15:25, 子奇要求我去好友家园时, 不显示未收取金币
// 城市已经开发完毕不显示金币
if (this.cityId === Global.devCityId && GameModule.userInfo.levelHomeItemFullCount != 5) {
this.coinWrap.active = true;
} else {
this.coinWrap.active = false;
return;
}
if (!this.buildingInfo.coinCount) {
for (let i = 0; i < this.coinArrayMax; i++) {
let coin = this.coinArray[i];
this.coinHide(coin);
}
return;
}
for (let i = 0; i < this.coinArrayMax; i++) {
let coin = this.coinArray[i];
while (this.buildingInfo.coinCount != 0 && coin.coinCount != 10) {
coin.coinCount += 1;
this.buildingInfo.coinCount -= 1;
}
if (coin.coinCount != 0) {
let x = -140 + (85 * i);
coin.isPlay = true;
coin.node.position = cc.v2(x, -28);
coin.node.active = true;
if (this.addition > 1) {
let rate = this.buildingInfo.rate * this.addition;
coin.totalRate = rate * coin.coinCount;
} else {
coin.totalRate = this.buildingInfo.rate * coin.coinCount;
}
coin.initStatic(i);
}
}
},
artistListLayout() {
for (let child of this.artistList.children) { child.destroy(); }
for (let child of this.humanList) { child.destroy(); }
let self = this;
let addAddItemToList = function (showTop = false, showBottom = false) {
let addArtistItem = cc.instantiate(self.artistListItem);
self.artistList.addChild(addArtistItem);
let addArtistScript = addArtistItem.getComponent('LevelHomeArtistItem');
addArtistScript.initWithBuildingInfo(self.buildingInfo, self.uid, true, showTop, showBottom);
}
let addArtistItemToList = function (artist, showTop = false, showBottom = false) {
let artistItem = cc.instantiate(self.artistListItem);
self.artistList.addChild(artistItem);
let artistScript = artistItem.getComponent('LevelHomeArtistItem');
artistScript.initWithArtistData(self.buildingInfo, self.uid, true, artist, showTop, showBottom);
}
let addHuman = function (artist, index) {
DWTool.loadResPrefab("./prefabs/artist_man")
.then((prefab) => {
let human = cc.instantiate(prefab);
human.getComponent('ArtistMan').init(artist);
human.getComponent('ArtistMan').direction = (index > 0) ? 1 : -1;
self.node.addChild(human);
self.humanList.push(human);
});
}
// 当没有自己艺人, 也没有好友艺人入驻时, 这里还得区分主态和客态
if (this.artists.length === 0) {
addAddItemToList(true);
// 没有艺人不显示倍数
this.addition = 1;
} else {
// 有艺人入驻要显示倍数
this.additionNode.active = true;
if (this.artists.length === 1) {
let artist = this.artists[0];
this.addition += artist.stationJobLevel;
// 这里要区分主态和客态, 去别人家园时, 如果有一个是客态自己的艺人, 那么另一个就是可以入驻的按钮
if (artist.role === 2) {
addArtistItemToList(artist, true);
addHuman(artist, (Math.random() - 0.5) * 2);
} else {
addAddItemToList(true, true);
addArtistItemToList(artist);
addHuman(artist, (Math.random() - 0.5) * 2);
}
} else {
for (let i = 0; i < this.artists.length; i++) {
let artist = this.artists[i];
this.addition += artist.stationJobLevel;
if (i === 0) {
addArtistItemToList(artist, true, true);
} else {
addArtistItemToList(artist);
}
addHuman(artist, i);
}
}
}
},
layout(buildingInfo) {
// 判断是否有下一级, 没有的话就是满级
if (buildingInfo.hasNext === 1 && buildingInfo.level <= Global.BuildingManager.getLevelCount(buildingInfo.buildingId)) {
// 判断是否已经解锁
if (buildingInfo.isUnlocked) {
this.artistList.active = true;
this.lockNode.active = false;
this.costLabel.string = DWTool.coinParse(buildingInfo.nextUpScore);
this.rate = buildingInfo.rate;
// 判断是否有足够的金额解锁
if (GameModule.userInfo.grossIncome >= buildingInfo.nextUpScore) {
if (buildingInfo.buildingId === 1 && buildingInfo.level === 1) {
// 第三个引导
GameModule.homeGuide.on('Fire_state3', this.updateBuildingEvent, this)
// 触发引导系统state3状态
GameModule.homeGuide.getComponent('HomeGuide').handleState('state3')
}
this.setState(RoomState.Update);
} else {
this.setState(RoomState.UnLock);
}
} else {
this.artistList.active = false;
this.countDown = 0;
this._currentTime = 0;
this.rateProgressBar.progress = 0;
this.totalRate = 0;
this.lockNode.active = true;
this.costLabel.string = 0;
this.unLockBuildName.string = `${buildingInfo.name}`;
this.unlockRichText.string = ` ${DWTool.coinParse(buildingInfo.unlockScore)}`;
// 判断是否有足够的金额解锁
if (GameModule.userInfo.grossIncome >= buildingInfo.unlockScore) {
if (this.unlockBuildingRecord[0] === 1 && this.buildingInfo.buildingId === 2) { // 如果第一层已经解锁, 才能执行下一个引导
setTimeout(() => {
// 监听完成引导系统state4的事件
GameModule.homeGuide.on('Fire_state4', this.unlockBuildingEvent, this)
// 触发引导系统state4状态
GameModule.homeGuide.getComponent('HomeGuide').handleState('state4')
}, 500);
} else if (this.unlockBuildingRecord[1] === 1 && this.buildingInfo.buildingId === 3) {
setTimeout(() => {
// 监听完成引导系统state5的事件
GameModule.homeGuide.on('Fire_state5', this.unlockBuildingEvent, this)
// 触发引导系统state5状态
GameModule.homeGuide.getComponent('HomeGuide').handleState('state5')
}, 500)
}
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;
}
this.setState(RoomState.Lock);
}
} else {
this.rate = buildingInfo.rate;
this.setState(RoomState.Full);
}
},
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.node.active = false;
this.updateBtn.spriteFrame = this.updateBtnFrames[0];
this.updateBtn.node.getComponent(cc.Button).interactable = false;
this.maxNode.active = false;
break;
case RoomState.UnLock:
this.openDoorSkeletion.node.active = false;
this.lockBtn.node.active = false;
this.lockBottomNode.active = false;
this.updateBtn.node.active = true;
this.lockNode.active = false;
this.updateBtn.spriteFrame = this.updateBtnFrames[0];
this.updateBtn.node.getComponent(cc.Button).interactable = false;
this.maxNode.active = false;
break;
case RoomState.Update:
this.openDoorSkeletion.node.active = false;
this.lockBtn.node.active = false;
this.lockBottomNode.active = false;
this.updateBtn.node.active = true;
this.lockNode.active = false;
this.updateBtn.spriteFrame = this.updateBtnFrames[1];
this.updateBtn.node.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.node.active = false;
default:
break;
}
this.state = state;
},
// 升级建筑
updateBuilding() {
// 从配置文件里获取
let maxLevel = Global.BuildingManager.getLevelCount(this.buildingInfo.buildingId);
let nextLevel = this.buildingInfo.level + 1;
let level = nextLevel > maxLevel ? maxLevel : nextLevel;
let buildModel = Global.BuildingManager.getBuildingInfo(this.cityId, this.buildingInfo.buildingId, level);
this.isLevelSpeedUp(buildModel);
// 将已有入驻的艺人赋值给新的model, 保持一个引用
buildModel.artists = this.buildingInfo.artists;
if (this.buildingInfo.isUnlocked) {
// 当前楼层已解锁
GameModule.userInfo.grossIncome -= this.buildingInfo.nextUpScore;
GameModule.userInfo.updateRecordModify(buildModel);
} else {
// 当前楼层未解锁
this.unlockBuildingRecord[this.buildingInfo.buildingId - 1] = 1;
GameModule.userInfo.grossIncome -= this.buildingInfo.unlockScore;
GameModule.userInfo.recordUnlockModify.push(buildModel);
// 成功解锁后立刻调用上报,提交ub
GameModule.userInfo.doReport()
}
GameModule.userInfo.stars += 1;
if (this.resetCallback) {
this.resetCallback(buildModel);
}
this.config(buildModel, this.uid);
// 如果满级了, 就发通知告诉userinfo
if (buildModel.hasNext != 1) {
GameEvent.fire(GameNotificationKey.LevelHomeItemBuildingFull);
}
if (buildModel.level === 1) {
GameEvent.fire(GameNotificationKey.LevelHomeItemUnlock);
}
},
/**
* 升级到某个特定等级,根据配置表的金币生产速度和时间,判断是否需要弹出金币x2或者时间-50%的通知。
*/
isLevelSpeedUp(buildingInfo) {
if (buildingInfo.level <= 2) {
return;
}
if (buildingInfo.rate >= this.buildingInfo.rate * 2) {
GameEvent.fire(GameNotificationKey.LevelHomeSpeedUp, buildingInfo.name, 1);
} else if (buildingInfo.rateUnit === this.buildingInfo.rateUnit / 2) {
GameEvent.fire(GameNotificationKey.LevelHomeSpeedUp, buildingInfo.name, 2);
}
},
// 收取金币
pickupCoin(coin, isShowAnimation = true) {
if (this.currentCoin === coin) {
this.currentCoin = null;
}
if (isShowAnimation) {
let pos = coin.node.convertToWorldSpace(cc.v2(coin.node.width / 2, coin.node.height / 2));
this.showCollectAnim(pos, coin.coinCount)
}
// 点击一次金币, 就将notPickupCount减去coinNode.coinCount
this.notPickupCount -= coin.coinCount;
// console.log(`收入: ${buildingInfo.rate * coin.coinCount}`);
GameModule.userInfo.grossIncome += (this.buildingInfo.rate * coin.coinCount * this._addition);
this.coinHide(coin);
},
// 收取金币,不增加收益
pickupCoinWithNoIncom(coin, isShowAnimation = true) {
if (this.currentCoin === coin) {
this.currentCoin = null;
}
if (isShowAnimation) {
let pos = coin.node.convertToWorldSpace(cc.v2(coin.node.width / 2, coin.node.height / 2));
this.showCollectAnim(pos, coin.coinCount)
}
// 点击一次金币, 就将notPickupCount减去coinNode.coinCount
this.notPickupCount -= coin.coinCount;
this.coinHide(coin);
},
showCollectAnim(pos, colNums) {
let canvasNode = cc.find("Canvas");
let grossCoin = GameModule.userInfo.grossCoin;
let grossCoinPos = grossCoin.node.convertToWorldSpace(cc.v2(grossCoin.node.width / 2, grossCoin.node.height / 2));
// let colNums = 5
let vSize = cc.view.getVisibleSize();
let target = cc.v2(grossCoinPos.x - vSize.width / 2, grossCoinPos.y - vSize.height / 2)
let i = 0;
let runSt = setInterval(() => {
if (i == colNums) {
clearInterval(runSt)
} else {
let ran = (Math.random() - 0.5) * 2 * 3;
let newCoin = cc.instantiate(this.coinPrefab);
let posX = pos.x - vSize.width / 2;
let posY = pos.y - vSize.height / 2;
canvasNode.addChild(newCoin)
newCoin.x = posX + ran * 15;
newCoin.y = posY + 30 + ran * 15;
newCoin.active = true;
newCoin = newCoin.getComponent("LevelHomeCoin")
newCoin.initAnim()
let cbNotiStart = cc.callFunc(() => {
GameEvent.fire(GameNotificationKey.UserCollectCoin, true);
})
let cbDestroy = cc.callFunc(() => {
newCoin.node.destroy();
})
let act = cc.sequence(cc.moveTo(1, target), cbDestroy, cbNotiStart)
newCoin.node.runAction(act.easing(cc.easeIn(2.1)));
i++
}
}, 100);
},
coinHide(coin) {
// 算出金币的x值, 70是金币宽度, -130是第一个金币的x值
let x = -140 + (85 * coin.index);
// 重置金币状态
coin.node.position = cc.v2(x, -108);
coin.node.active = false;
coin.isPlay = false;
coin.isPlaying = false;
coin.coinCount = 0;
coin.totalRate = 0;
},
getFreeCoin() {
for (let i = 0; i < this.coinArrayMax; i++) {
let coinNode = this.coinArray[i];
if (coinNode.coinCount != 10) {
return coinNode;
}
}
return null;
},
generateCoin() {
if (this.currentCoin && this.currentCoin.coinCount < 10) {
this.currentCoin.totalRate += this.rate;
this.currentCoin.coinCount += 1;
this.currentCoin.updateAnimation();
} else {
this.currentCoin = this.getFreeCoin();
if (this.currentCoin) {
this.currentCoin.totalRate += this.rate;
this.currentCoin.coinCount += 1;
if (this.buildingInfo.buildingId === 1 && this.buildingInfo.level === 1 && this.notPickupCount === 1) {
// 监听完成引导系统state2的事件
GameModule.homeGuide.on('Fire_state2', () => {
this.pickupCoin(this.currentCoin);
}, this);
// 触发引导系统state1状态
GameModule.homeGuide.getComponent('HomeGuide').handleState('state2');
}
if (!this.currentCoin.isPlay) {
this.currentCoin.showAnimation();
} else {
this.currentCoin.updateAnimation();
}
}
}
},
update(dt) {
if (this.buildingInfo) {
// 不断刷新界面
this.layout(this.buildingInfo);
// 只有已经解锁进度条才会走
if (this.buildingInfo.isUnlocked) {
// 进度条走完, 开始生产金币
if (Math.floor(this.rateProgressBar.progress) === 1) {
this.rateProgressBar.progress = 1;
if (GameModule.userInfo.levelHomeItemFullCount != 5 && // 是否满级
this.cityId === Global.devCityId) { // 当前访问的是不是这个city
if (this.notPickupCount >= this.coinArrayMax * 10) {
this.rateProgressLabel.node.active = false;
this.coinFullTip.active = true;
} else {
this.coinFullTip.active = false;
this.notPickupCount += 1;
this.generateCoin();
this.rateProgressBar.progress = 0;
this._currentTime = 0;
}
} else { // 满级后的状态
this.rateProgressBar.progress = 0;
this._currentTime = 0;
this.coinFullTip.active = false;
this.rateProgressLabel.node.active = true;
}
} else {
this.rateProgressLabel.node.active = true;
this.coinFullTip.active = false;
this._currentTime += dt;
this.rateProgressBar.progress = this._currentTime / this.countDown;
if (Math.floor(this.rateProgressBar.progress) === 1) {
this.rateProgressBar.progress = 1;
}
let resultCountDown = this.countDown - Math.floor(this._currentTime);
if (this._preCountDown !== resultCountDown) {
this.countdownLabel.string = DWTool.calculateTime(resultCountDown);
this._preCountDown = resultCountDown;
}
}
}
}
},
setResidentTip(show) {
this.residentTipNode.active = show;
}
});