CityMapCtrl.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. if (this.income > this.incomeMax) {
  162. this.income = this.incomeMax;
  163. }
  164. this.currentIncomeLabel.string = DWTool.coinParse(this.income);
  165. this.progressBar.progress = this.income / this.incomeMax;
  166. }
  167. },
  168. progressBarRunning() {
  169. this.income = Math.round((this.income + this.incomSpeedSeconds) * 1000) / 1000;
  170. this._setProgressbar();
  171. },
  172. _showMoveAnimation() {
  173. this.showCollectAnim(30);
  174. for (let i = 0; i < this.cityScriptList.length; i++) {
  175. let cityInfo = cityList[i];
  176. if (cityInfo.id == this.devCityId) {
  177. let lastCity = this.cityScriptList[i];
  178. let currentCity = this.cityScriptList[i + 1];
  179. var lastPosition = this.getPositionInView(lastCity.node);
  180. let scrollOffset = this.scrollView.getScrollOffset().y - lastPosition.y + 300;
  181. let scrollTime = Math.abs(Math.round((this.content.height - scrollOffset) / 50) * 0.04);
  182. this.scrollView.scrollToOffset(cc.v2(0, scrollOffset), scrollTime);
  183. this.scheduleOnce(() => {
  184. this.startMoveAnimation(lastCity, currentCity);
  185. this.scrollView.vertical = false;
  186. }, scrollTime);
  187. }
  188. }
  189. },
  190. startMoveAnimation(lastCity, currentCity) {
  191. let self = this;
  192. lastCity.showFinishAnimation(() => {
  193. currentCity.setState(CityState.dev);
  194. self.scrollView.vertical = true;
  195. self.scheduleOnce(() => {
  196. this.devCityId++;
  197. self.gameFSM.visitcity(this.devCityId);
  198. }, 1);
  199. });
  200. },
  201. getPositionInView(item) { // get item position in scrollview's node space
  202. let worldPos = item.parent.convertToWorldSpaceAR(item.position);
  203. let viewPos = this.scrollView.node.convertToNodeSpaceAR(worldPos);
  204. return viewPos;
  205. },
  206. // airplaneAnimation(position1, position2, cb) {
  207. // var angle = this.getAngle(position1, position2);
  208. // this.airplane.setRotation(angle);
  209. // this.airplane.setPosition(position1);
  210. // let end = cc.callFunc(cb, this);
  211. // this.airplane.runAction(cc.sequence(cc.moveTo(3, position2), end));
  212. // },
  213. close() {
  214. clearInterval(this.timer);
  215. this.gameFSM.historyBack();
  216. },
  217. getAngle(p1, p2) {
  218. // 直角的边长
  219. var x = p2.x - p1.x;
  220. var y = p2.y - p1.y;
  221. // 斜边长
  222. var z = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
  223. // 余弦
  224. var cos = y / z;
  225. // 弧度
  226. var radina = Math.acos(cos);
  227. // 角度
  228. var angle = 180 / (Math.PI / radina);
  229. if (x < 0) {
  230. angle = - angle;
  231. }
  232. return angle;
  233. },
  234. showCollectAnim(colNums) {
  235. if (this.income) {
  236. GameModule.audioMng.playGetcoin();
  237. this.moneyCatSkeleton.setAnimation(0, 'maomi_hit', false);
  238. this.moneyCatSkeleton.setCompleteListener(() => {
  239. this.moneyCatSkeleton.setAnimation(0, 'maomi', true);
  240. })
  241. GameModule.userInfo.grossIncome += this.income;
  242. this.grossIncome = GameModule.userInfo.grossIncome;
  243. this.income = 0;
  244. this._setProgressbar();
  245. CityMapApi.reportCityIncome().then(() => {
  246. });
  247. let canvasNode = cc.find("Canvas");
  248. let coinNode = this.coinNode;
  249. let grossCoinPos = coinNode.convertToWorldSpace(cc.v2(coinNode.width / 2, coinNode.height / 2));
  250. let pos = this.moneyCatNode.convertToWorldSpace(cc.v2(this.moneyCatNode.width / 2, this.moneyCatNode.height / 2 - 80));
  251. // let colNums = 5
  252. let vSize = cc.view.getVisibleSize();
  253. let target = cc.v2(grossCoinPos.x - vSize.width / 2, grossCoinPos.y - vSize.height / 2);
  254. let i = 0;
  255. let runSt = setInterval(() => {
  256. if (i == colNums) {
  257. clearInterval(runSt)
  258. } else {
  259. let ranX = (Math.random() - 0.5) * 2 * 8;
  260. let ranY = (Math.random() - 0.5) * 2 * 3;
  261. let newCoin = cc.instantiate(this.coinPrefab);
  262. let posX = pos.x - vSize.width / 2;
  263. let posY = pos.y - vSize.height / 2;
  264. canvasNode.addChild(newCoin)
  265. newCoin.x = posX + ranX * 15;
  266. newCoin.y = posY + 30 + ranY * 15;
  267. newCoin.active = true;
  268. newCoin = newCoin.getComponent("LevelHomeCoin")
  269. newCoin.initAnim()
  270. let cbNotiStart = cc.callFunc(() => {
  271. this.coinSkeleton.setAnimation(0, 'jinbi_huoqu2', false);
  272. })
  273. let cbDestroy = cc.callFunc(() => {
  274. newCoin.node.destroy();
  275. })
  276. let act = cc.sequence(cc.moveTo(1, target), cbDestroy, cbNotiStart)
  277. newCoin.node.runAction(act.easing(cc.easeIn(2.1)));
  278. i++
  279. }
  280. }, 25);
  281. }
  282. },
  283. });