CityMapCtrl.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. var cityList = require('../data/city');
  2. var cityIncomeList = require('../data/cityIncome');
  3. var GameModule = require('../utils/GameModule');
  4. const GameNotificationKey = require('../utils/GameEnum').GameNotificationKey;
  5. const DWTool = require("../utils/DWTool");
  6. const CityMapApi = require('../net/CityMapApi');
  7. const CityState = {
  8. lock: 0,
  9. dev: 1,
  10. unlock: 2
  11. }
  12. cc.Class({
  13. extends: cc.Component,
  14. properties: {
  15. scrollView: cc.ScrollView,
  16. view: cc.Node,
  17. mapbg: cc.Node,
  18. content: cc.Node,
  19. cityItemPrefab: cc.Prefab,
  20. myMoneyLayout: cc.Node,
  21. coinNode: cc.Node,
  22. coinSkeleton: sp.Skeleton,
  23. myMoneyLabel: cc.Label,
  24. moneyCatNode: cc.Node,
  25. moneyCatSkeleton: sp.Skeleton,
  26. backNode: cc.Node,
  27. myCompanyNode: cc.Node,
  28. companyText: cc.RichText,
  29. incomeText: cc.RichText,
  30. incomSpeedLabel: cc.Label,
  31. incomeLimitLabel: cc.Label,
  32. progressBar: cc.ProgressBar,
  33. currentIncomeLabel: cc.Label,
  34. // airplane: cc.Node,
  35. coinPrefab: cc.Prefab,
  36. topBg: cc.Node,
  37. grossIncome: {
  38. get: function () {
  39. return this._grossIncome;
  40. },
  41. set: function (value) {
  42. this._grossIncome = value;
  43. this.myMoneyLabel.string = DWTool.coinParse(this._grossIncome);
  44. }
  45. },
  46. cover: cc.Node,
  47. },
  48. onLoad() {
  49. this.moneyCatNode.on(cc.Node.EventType.TOUCH_END, _.debounce(() => {
  50. this.showCollectAnim(30);
  51. }, 1000, true), this);
  52. this.cityScriptList = [];
  53. let scaleSize = cc.view.getVisibleSize().height / this.mapbg.height;
  54. this.mapbg.height = this.node.height = cc.view.getVisibleSize().height;
  55. this.view.height = this.scrollView.node.height = this.node.height - 440;
  56. this.scrollView.node.y = this.node.height / 2 - this.scrollView.node.height;
  57. // if (this.node.height >= 1624) {
  58. // this.view.height = this.scrollView.node.height = this.node.height - 240;
  59. // } else {
  60. // this.view.height = this.scrollView.node.height = this.node.height - 440;
  61. // }
  62. // this.scrollView.node.height = this.view.height = cc.view.getVisibleSize().height;
  63. // this.topBg.y = this.topBg.y * scaleSize;
  64. this.myMoneyLayout.y = scaleSize * this.myMoneyLayout.y;
  65. this.moneyCatNode.y = scaleSize * this.moneyCatNode.y;
  66. this.backNode.y = scaleSize * this.backNode.y;
  67. this.myCompanyNode.y = scaleSize * this.myCompanyNode.y;
  68. this.myCompanyNode.active = false;
  69. for (let i = 0; i < cityList.length; i++) {
  70. let cityInfo = cityList[i];
  71. // if (cityInfo.id < Global.devCityId) {
  72. // cityInfo.state = CityState.unlock;
  73. // } else if (cityInfo.id === Global.devCityId) {
  74. // cityInfo.state = CityState.dev;
  75. // } else {
  76. // cityInfo.state = CityState.lock;
  77. // }
  78. let item = cc.instantiate(this.cityItemPrefab);
  79. item.x = i % 2 === 0 ? 110 : -110;
  80. /**
  81. * 暂时取消城市点击跳转功能
  82. */
  83. // item.on(cc.Node.EventType.TOUCH_END, () => {
  84. // if (cityInfo.id <= Global.devCityId) {
  85. // this.gameFSM.visitcity(cityInfo.id);
  86. // }
  87. // }, this);
  88. let cityScript = item.getComponent('CityItem');
  89. // cityScript.init(cityInfo);
  90. this.content.addChild(item);
  91. this.cityScriptList.push(cityScript);
  92. }
  93. },
  94. init(game) {
  95. this.game = game
  96. this.gameFSM = game.gameFSM
  97. console.log(this.gameFSM);
  98. },
  99. show(move, devCityId) {
  100. if (devCityId) {
  101. this.devCityId = devCityId;
  102. } else {
  103. this.devCityId = Global.devCityId;
  104. }
  105. this.node.x = 400;
  106. this.node.active = true;
  107. let finish = cc.callFunc(() => {
  108. // if (move) {
  109. // this._showMoveAnimation();
  110. // }
  111. });
  112. this.node.runAction(cc.sequence(cc.moveTo(0.3, 0, 0).easing(cc.easeBackOut()), finish));
  113. let totalIncomeSpeed = 0;
  114. for (let i = 0; i < cityList.length; i++) {
  115. let cityInfo = cityList[i];
  116. let cityScript = this.cityScriptList[i];
  117. let incomeSpeed = 0;
  118. if (cityInfo.id < this.devCityId) {
  119. cityInfo.state = CityState.unlock;
  120. incomeSpeed = cityIncomeList[i].income;
  121. totalIncomeSpeed += incomeSpeed;
  122. this.incomeMax = cityIncomeList[i].incomeLimit;
  123. this.incomSpeedLabel.string = DWTool.coinParse(totalIncomeSpeed) + "/天";
  124. this.incomSpeedSeconds = totalIncomeSpeed / 86400;
  125. this.incomeLimitLabel.string = DWTool.coinParse(this.incomeMax);
  126. } else if (cityInfo.id === this.devCityId) {
  127. cityInfo.state = CityState.dev;
  128. } else {
  129. cityInfo.state = CityState.lock;
  130. }
  131. cityScript.init(cityInfo, incomeSpeed);
  132. }
  133. if (move) {
  134. this.myCompanyNode.active = false;
  135. this.cover.active = true;
  136. this.backNode.active = false;
  137. } else {
  138. this.myCompanyNode.active = false;
  139. this.cover.active = false;
  140. this.backNode.active = true;
  141. this.moneyCatSkeleton.setAnimation(0, 'maomi', true);
  142. }
  143. let mapself = this;
  144. CityMapApi.getMapInfo(
  145. (response) => {
  146. this.income = Math.round(response.cityIncomeTime * this.incomSpeedSeconds) / 1000;
  147. mapself._setProgressbar();
  148. if (move) {
  149. this._showMoveAnimation();
  150. }
  151. this.timer = setInterval(() => {
  152. mapself.progressBarRunning();
  153. }, 1000);
  154. }
  155. , (code, msg) => {
  156. });
  157. this.myMoneyLabel.string = GameModule.userInfo.grossIncomeLabel.string;
  158. },
  159. _setProgressbar() {
  160. if (this.income > this.incomeMax) {
  161. this.income = this.incomeMax;
  162. }
  163. this.currentIncomeLabel.string = DWTool.coinParse(this.income);
  164. this.progressBar.progress = this.income / this.incomeMax;
  165. },
  166. progressBarRunning() {
  167. this.income = Math.round((this.income + this.incomSpeedSeconds) * 1000) / 1000;
  168. this._setProgressbar();
  169. },
  170. _showMoveAnimation() {
  171. this.showCollectAnim(30);
  172. for (let i = 0; i < this.cityScriptList.length; i++) {
  173. let cityInfo = cityList[i];
  174. if (cityInfo.id == this.devCityId) {
  175. let lastCity = this.cityScriptList[i];
  176. let currentCity = this.cityScriptList[i + 1];
  177. var lastPosition = this.getPositionInView(lastCity.node);
  178. let scrollOffset = this.scrollView.getScrollOffset().y - lastPosition.y + 300;
  179. let scrollTime = Math.abs(Math.round((this.content.height - scrollOffset) / 50) * 0.04);
  180. this.scrollView.scrollToOffset(cc.v2(0, scrollOffset), scrollTime);
  181. this.scheduleOnce(() => {
  182. this.startMoveAnimation(lastCity, currentCity);
  183. this.scrollView.vertical = false;
  184. }, scrollTime);
  185. }
  186. }
  187. },
  188. startMoveAnimation(lastCity, currentCity) {
  189. let self = this;
  190. lastCity.showFinishAnimation(() => {
  191. currentCity.setState(CityState.dev);
  192. self.scrollView.vertical = true;
  193. self.scheduleOnce(() => {
  194. this.devCityId++;
  195. self.gameFSM.visitcity(this.devCityId);
  196. }, 1);
  197. });
  198. },
  199. getPositionInView(item) { // get item position in scrollview's node space
  200. let worldPos = item.parent.convertToWorldSpaceAR(item.position);
  201. let viewPos = this.scrollView.node.convertToNodeSpaceAR(worldPos);
  202. return viewPos;
  203. },
  204. // airplaneAnimation(position1, position2, cb) {
  205. // var angle = this.getAngle(position1, position2);
  206. // this.airplane.setRotation(angle);
  207. // this.airplane.setPosition(position1);
  208. // let end = cc.callFunc(cb, this);
  209. // this.airplane.runAction(cc.sequence(cc.moveTo(3, position2), end));
  210. // },
  211. close() {
  212. clearInterval(this.timer);
  213. this.gameFSM.historyBack();
  214. },
  215. getAngle(p1, p2) {
  216. // 直角的边长
  217. var x = p2.x - p1.x;
  218. var y = p2.y - p1.y;
  219. // 斜边长
  220. var z = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
  221. // 余弦
  222. var cos = y / z;
  223. // 弧度
  224. var radina = Math.acos(cos);
  225. // 角度
  226. var angle = 180 / (Math.PI / radina);
  227. if (x < 0) {
  228. angle = - angle;
  229. }
  230. return angle;
  231. },
  232. showCollectAnim(colNums) {
  233. if (this.income > 0) {
  234. this.moneyCatSkeleton.setAnimation(0, 'maomi_hit', false);
  235. this.moneyCatSkeleton.setCompleteListener(() => {
  236. this.moneyCatSkeleton.setAnimation(0, 'maomi', true);
  237. })
  238. GameModule.userInfo.grossIncome += this.income;
  239. this.grossIncome = GameModule.userInfo.grossIncome;
  240. this.income = 0;
  241. this._setProgressbar();
  242. CityMapApi.reportCityIncome().then(() => {
  243. });
  244. let canvasNode = cc.find("Canvas");
  245. let coinNode = this.coinNode;
  246. let grossCoinPos = coinNode.convertToWorldSpace(cc.v2(coinNode.width / 2, coinNode.height / 2));
  247. let pos = this.moneyCatNode.convertToWorldSpace(cc.v2(this.moneyCatNode.width / 2, this.moneyCatNode.height / 2 - 80));
  248. // let colNums = 5
  249. let vSize = cc.view.getVisibleSize();
  250. let target = cc.v2(grossCoinPos.x - vSize.width / 2, grossCoinPos.y - vSize.height / 2);
  251. let i = 0;
  252. let runSt = setInterval(() => {
  253. if (i == colNums) {
  254. clearInterval(runSt)
  255. } else {
  256. let ranX = (Math.random() - 0.5) * 2 * 8;
  257. let ranY = (Math.random() - 0.5) * 2 * 3;
  258. let newCoin = cc.instantiate(this.coinPrefab);
  259. let posX = pos.x - vSize.width / 2;
  260. let posY = pos.y - vSize.height / 2;
  261. canvasNode.addChild(newCoin)
  262. newCoin.x = posX + ranX * 15;
  263. newCoin.y = posY + 30 + ranY * 15;
  264. newCoin.active = true;
  265. newCoin = newCoin.getComponent("LevelHomeCoin")
  266. newCoin.initAnim()
  267. let cbNotiStart = cc.callFunc(() => {
  268. this.coinSkeleton.setAnimation(0, 'jinbi_huoqu2', false);
  269. })
  270. let cbDestroy = cc.callFunc(() => {
  271. newCoin.node.destroy();
  272. })
  273. let act = cc.sequence(cc.moveTo(1, target), cbDestroy, cbNotiStart)
  274. newCoin.node.runAction(act.easing(cc.easeIn(2.1)));
  275. i++
  276. }
  277. }, 25);
  278. }
  279. },
  280. });