var cityLocationList = require('./CityLocationList');
var cityList = require('../data/city');
var cityIncomeList = require('../data/cityIncome');
var GameModule = require('../utils/GameModule');
const GameNotificationKey = require('../utils/GameEnum').GameNotificationKey;
const DWTool = require("../utils/DWTool");
const CityMapApi = require('../net/CityMapApi');
cc.Class({
extends: cc.Component,
properties: {
scrollView: cc.ScrollView,
view: cc.Node,
mapbg: cc.Node,
content: cc.Node,
cityItemPrefab: cc.Prefab,
myMoneyLayout: cc.Node,
coinNode: cc.Node,
coinSkeleton: sp.Skeleton,
myMoneyLabel: cc.Label,
moneyCatNode: cc.Node,
backNode: cc.Node,
myCompanyNode: cc.Node,
companyText: cc.RichText,
incomeText: cc.RichText,
progressBar: cc.ProgressBar,
currentIncomeLabel: cc.Label,
airplane: cc.Node,
coinPrefab: cc.Prefab,
grossIncome: {
get: function () {
return this._grossIncome;
},
set: function (value) {
this._grossIncome = value;
this.myMoneyLabel.string = DWTool.coinParse(this._grossIncome);
}
},
},
onLoad() {
this.moneyCatNode.on(cc.Node.EventType.TOUCH_END, _.debounce(() => {
this.showCollectAnim(50);
}, 1000, true), this);
this.citySpriteList = [];
let originalHeight = this.mapbg.height;
let originalWidth = this.mapbg.width;
let scaleSize = cc.view.getVisibleSize().height / this.scrollView.node.height;
this.scrollView.node.height = this.view.height = cc.view.getVisibleSize().height;
this.mapbg.height = cc.view.getVisibleSize().height + 300;
let value = this.mapbg.height / originalHeight;
this.mapbg.width = value * originalWidth;
this.myMoneyLayout.y = scaleSize * this.myMoneyLayout.y;
this.moneyCatNode.y = scaleSize * this.moneyCatNode.y;
this.backNode.y = scaleSize * this.backNode.y;
this.myCompanyNode.y = scaleSize * this.myCompanyNode.y;
for (let i = 0; i < cityLocationList.length; i++) {
let cityInfo = cityList[i];
if (cityInfo.id < Global.cityId) {
cityInfo.unlock = true;
} else if (cityInfo.id == Global.cityId) {
cityInfo.selected = true;
this.incomeSpeed = cityIncomeList[i].income;
this.incomeMax = cityIncomeList[i].incomeLimit;
this.companyText.string = '当前拥有' + (i + 1) + '家公司';
this.incomeText.string = '' + this.incomeSpeed + '/小时';
}
let item = cc.instantiate(this.cityItemPrefab);
item.x = cityLocationList[i].x * value;
item.y = cityLocationList[i].y * value;
item.on(cc.Node.EventType.TOUCH_END, () => {
if (cityInfo.id <= Global.cityId) {
this.gameFSM.visitcity(cityInfo.id);
}
}, this);
let citySprite = item.getComponent('CityItem');
citySprite.init(cityInfo);
this.content.addChild(item);
this.citySpriteList.push(citySprite);
}
},
init(game) {
this.game = game
this.gameFSM = game.gameFSM
console.log(this.gameFSM);
},
show(move) {
this.node.x = 400;
this.node.active = true;
let finish = cc.callFunc(() => {
if (move) {
this._showMoveAnimation();
}
});
this.node.runAction(cc.sequence(cc.moveTo(0.3, 0, 0).easing(cc.easeBackOut()), finish));
if (move) {
this.myCompanyNode.active = false;
this.moneyCatNode.active = false;
this.backNode.active = false;
for (let i = 0; i < this.citySpriteList.length; i++) {
let cityInfo = cityList[i];
if (cityInfo.id == Global.cityId) {
let lastCity = this.citySpriteList[i - 1];
let currentCity = this.citySpriteList[i];
lastCity.fakeCurrent();
currentCity.fakeLock();
}
}
} else {
this.myCompanyNode.active = true;
this.moneyCatNode.active = true;
this.backNode.active = true;
CityMapApi.getMapInfo(
(response) => {
this.income = response.cityIncomeTime * this.incomeSpeed / 1000;
this._setProgressbar();
}
, (code, msg) => {
});
for (let i = 0; i < cityLocationList.length; i++) {
let cityInfo = cityList[i];
if (cityInfo.id == Global.cityId) {
cityInfo.selected = true;
this.incomeSpeed = cityIncomeList[i].income;
this.incomeMax = cityIncomeList[i].incomeLimit;
this.companyText.string = '当前拥有' + (i + 1) + '家公司';
this.incomeText.string = '' + this.incomeSpeed + '/小时';
}
}
this.myMoneyLabel.string = GameModule.userInfo.grossIncomeLabel.string;
}
},
_setProgressbar() {
if (this.income > this.incomeMax) {
this.income = this.incomeMax;
}
this.currentIncomeLabel.string = DWTool.coinParse(this.income);
this.progressBar.progress = this.income / this.incomeMax * this.progressBar.totalLength;
},
_showMoveAnimation() {
for (let i = 0; i < this.citySpriteList.length; i++) {
let cityInfo = cityList[i];
if (cityInfo.id == Global.cityId) {
let lastCity = this.citySpriteList[i - 1];
let currentCity = this.citySpriteList[i];
currentCity.fakeLock();
var lastPosition = this.getPositionInView(lastCity.node);
let scrollOffset = this.scrollView.getScrollOffset().y - lastPosition.y;
this.scrollView.scrollToOffset(cc.v2(0, scrollOffset));
this.scrollView.vertical = false;
this.scheduleOnce(() => {
this.startMoveAnimation(lastCity, currentCity);
}, 0.4);
}
}
},
startMoveAnimation(lastCity, currentCity) {
let lastPosition = this.getPositionInView(lastCity.node);
var currentPosition = this.getPositionInView(currentCity.node);
let self = this;
lastCity.showFinishAnimation(() => {
this.airplaneAnimation(lastPosition, currentPosition, () => {
self.airplane.x = 600;
currentCity.fakeUnLock();
this.scheduleOnce(this.close, 0.5);
this.scrollView.vertical = true;
this.gameFSM.visitcity(Global.cityId);
});
});
},
getPositionInView(item) { // get item position in scrollview's node space
let worldPos = item.parent.convertToWorldSpaceAR(item.position);
let viewPos = this.scrollView.node.convertToNodeSpaceAR(worldPos);
return viewPos;
},
airplaneAnimation(position1, position2, cb) {
var angle = this.getAngle(position1, position2);
this.airplane.setRotation(angle);
this.airplane.setPosition(position1);
let end = cc.callFunc(cb, this);
this.airplane.runAction(cc.sequence(cc.moveTo(3, position2), end));
},
close() {
this.gameFSM.historyBack();
},
getAngle(p1, p2) {
// 直角的边长
var x = p2.x - p1.x;
var y = p2.y - p1.y;
// 斜边长
var z = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
// 余弦
var cos = y / z;
// 弧度
var radina = Math.acos(cos);
// 角度
var angle = 180 / (Math.PI / radina);
if (x < 0) {
angle = - angle;
}
return angle;
},
showCollectAnim(colNums) {
if (this.income > 0) {
GameModule.userInfo.grossIncome += this.income*10000;
this.grossIncome = GameModule.userInfo.grossIncome;
this.income = 0;
this._setProgressbar();
CityMapApi.reportCityIncome().then(()=>{
});
let canvasNode = cc.find("Canvas");
let coinNode = this.coinNode;
let grossCoinPos = coinNode.convertToWorldSpace(cc.v2(coinNode.width / 2, coinNode.height / 2));
let pos = this.moneyCatNode.convertToWorldSpace(cc.v2(this.moneyCatNode.width / 2, this.moneyCatNode.height / 2 - 80));
// 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 ranX = (Math.random() - 0.5) * 2 * 8;
let ranY = (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 + ranX * 15;
newCoin.y = posY + 30 + ranY * 15;
newCoin.active = true;
newCoin = newCoin.getComponent("LevelHomeCoin")
newCoin.initAnim()
let cbNotiStart = cc.callFunc(() => {
this.coinSkeleton.setAnimation(0, 'jinbi_huoqu2', false);
})
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++
}
}, 25);
}
},
});