1
0

CityMapCtrl.js 10 KB

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