CityMapCtrl.js 11 KB

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