CityMapCtrl.js 10 KB

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