ManagerControl.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. const Constants = require('Constants');
  2. //移动方向的枚举
  3. var cameraMoveType = cc.Enum({
  4. none: 0,
  5. moveUp: 1,
  6. moveRight: 2,
  7. moveDown: 3,
  8. moveLeft: 4,
  9. });
  10. cc.Class({
  11. extends: cc.Component,
  12. properties: {
  13. // Show: {
  14. // default: null,
  15. // type: cc.Label,
  16. // },
  17. BuildNodeArray: {
  18. default: [],
  19. type: cc.Node,
  20. },
  21. //如果进入编辑状态
  22. _isBuildingCanEdit: { default: false, visible: false },
  23. _isBuildingMove: { default: false, visible: false },
  24. //如果是UICamera移动
  25. isUICameraMove: { default: false, visible: false },
  26. UIMask: cc.Node,
  27. // 移动的边界值
  28. boundarylimitValue: {
  29. default: 10,
  30. type: cc.Integer,
  31. tooltip: '移动边界的限制值', // 名字注释
  32. },
  33. boundaryMoveSpeed: {
  34. default: 100,
  35. type: cc.Integer,
  36. tooltip: '边界值移动的速度', // 名字注释
  37. },
  38. //camera 移动类
  39. cameraMoveType: {
  40. default: cameraMoveType.none,
  41. type: cc.Enum(cameraMoveType),
  42. },
  43. //碰到边界时候的delta
  44. _currentDelta: cc.v2(),
  45. //任务进行
  46. isTaskInProgress: false,
  47. },
  48. onLoad() {
  49. GlobalD.GameControl = this;
  50. this.initShare();
  51. },
  52. initShare: function () {
  53. this.UICamera = this.node;
  54. this._showShare(function () {
  55. this.sharegosharebtn.on(cc.Node.EventType.TOUCH_END, function (event) {
  56. // console.log("点击分享按钮");
  57. UtilsWX.sharebtn();
  58. }.bind(this));
  59. }.bind(this));
  60. },
  61. _showShare: function (callback) {
  62. return;
  63. cc.loader.loadRes("prefab/shareview", function (err, texture) {
  64. this.shareview = cc.instantiate(texture);
  65. if (this.UICamera.getChildByName(this.shareview.name) != null) {
  66. this.UICamera.getChildByName(this.shareview.name).destroy();
  67. }
  68. this.UICamera.addChild(this.shareview, -99);
  69. // this.dialogLuckView.active = false;
  70. // this.shareview.zIndex=-99;
  71. this.sharegosharebtn = this.getNode("gosharebtn", this.shareview);
  72. // this.close = this.getNode("close",this.dialogLuckView);
  73. this.sharegosharebtn.off(cc.Node.EventType.TOUCH_END);
  74. this.shareview.y = 500;
  75. this.shareview.x = -200;
  76. if (callback != null) {
  77. callback();
  78. }
  79. }.bind(this));
  80. },
  81. start() {
  82. var isTouch = false
  83. var self = this, parent = this.node.parent; this.MainCamera = GlobalD.game.MainCamera; this.Canvas = GlobalD.game.Canvas;
  84. self.target = self.MainCamera.node;
  85. // self.target.setScale(0.7);
  86. self.target.setScale(1.2);
  87. self.MainCamera.zoomRatio = 1.2;//0.7;
  88. self.myx = 432 / self.MainCamera.zoomRatio;
  89. self.myy = 768 / self.MainCamera.zoomRatio;
  90. this.node.on(cc.Node.EventType.TOUCH_END, function (event) {
  91. //设置为不移动
  92. self.cameraMoveType = cameraMoveType.none;
  93. // self.isUICameraMove = false;
  94. }, this);
  95. this.node.on(cc.Node.EventType.TOUCH_CANCEL, function (event) {
  96. //设置为不移动
  97. self.cameraMoveType = cameraMoveType.none;
  98. // self.isUICameraMove = false;
  99. }, this);
  100. this.node.on(cc.Node.EventType.TOUCH_START, function (event) {
  101. self.isUICameraMove = false;
  102. // 起始触摸位置
  103. // this.touchLocation = event.touch.getLocation();
  104. if (event.type === cc.Node.EventType.TOUCH_START && isTouch == false) {
  105. //穿透正常响应UI事件
  106. // this.BuildNodeArray[0]._touchListener.setSwallowTouches(false);
  107. this.node._touchListener.setSwallowTouches(false);
  108. } else {
  109. //吞掉事件 不让他继续传下去
  110. this.node._touchListener.setSwallowTouches(true);
  111. event.stopPropagation();
  112. }
  113. }, this);
  114. self.node.on(cc.Node.EventType.TOUCH_MOVE, function (event) {
  115. if (self.UIMask && self.UIMask.active || self.isTaskInProgress) return;
  116. if (self._isBuildingMove) {
  117. // cc.log(event.touch.getDelta());
  118. //如果是编辑建筑物时候
  119. let uiTouchPos = event.touch.getLocation();
  120. // cc.log(uiTouchPos);
  121. if (uiTouchPos.x <= self.boundarylimitValue) {
  122. // cc.log('屏幕左边', self.cameraMoveType);
  123. if (self.cameraMoveType != cameraMoveType.moveLeft) {
  124. self._currentDelta = event.touch.getDelta();
  125. }
  126. self.cameraMoveType = cameraMoveType.moveLeft;
  127. } else if (uiTouchPos.y <= self.boundarylimitValue) {
  128. // cc.log('屏幕下边');
  129. if (self.cameraMoveType != cameraMoveType.moveDown) {
  130. self._currentDelta = event.touch.getDelta();
  131. }
  132. self.cameraMoveType = cameraMoveType.moveDown;
  133. } else if (uiTouchPos.x >= self.node.width - self.boundarylimitValue) {
  134. // cc.log('屏幕右边');
  135. if (self.cameraMoveType != cameraMoveType.moveRight) {
  136. self._currentDelta = event.touch.getDelta();
  137. }
  138. self.cameraMoveType = cameraMoveType.moveRight;
  139. } else if (uiTouchPos.y >= self.node.height - self.boundarylimitValue) {
  140. // cc.log('屏幕上边');
  141. if (self.cameraMoveType != cameraMoveType.moveUp) {
  142. self._currentDelta = event.touch.getDelta();
  143. }
  144. self.cameraMoveType = cameraMoveType.moveUp;
  145. } else {
  146. if (self.cameraMoveType != cameraMoveType.none) {
  147. self._currentDelta = 0;
  148. }
  149. self.cameraMoveType = cameraMoveType.none;
  150. }
  151. return
  152. };
  153. // cc.log(self.isUICameraMove);
  154. var touches = event.getTouches();
  155. // cc.log(touches);
  156. if (touches.length >= 2) {
  157. var touch1 = touches[0], touch2 = touches[1];
  158. var delta1 = touch1.getDelta(), delta2 = touch2.getDelta();
  159. var touchPoint1 = parent.convertToNodeSpaceAR(touch1.getLocation());
  160. var touchPoint2 = parent.convertToNodeSpaceAR(touch2.getLocation());
  161. //缩放
  162. var distance = touchPoint1.sub(touchPoint2);
  163. var delta = delta1.sub(delta2);
  164. var scale = 1;
  165. if (Math.abs(distance.x) > Math.abs(distance.y)) {
  166. scale = (distance.x + delta.x) / distance.x * self.target.scale;
  167. }
  168. else {
  169. scale = (distance.y + delta.y) / distance.y * self.target.scale;
  170. }
  171. // self.target.scale = scale < 0.1 ? 0.1 : scale;
  172. self.target.scale = scale = scale <= 0.4 ? 0.4 : scale;
  173. self.target.scale = scale = scale >= 1 ? 1 : scale;
  174. // self.Show.string = "(" + scale + ")";
  175. self.MainCamera.zoomRatio = scale;
  176. this.myx = 432 / self.MainCamera.zoomRatio;
  177. this.myy = 768 / self.MainCamera.zoomRatio;
  178. self.isUICameraMove = true;
  179. self._deteItem();
  180. } else {
  181. // 640 360
  182. //533.33 300
  183. // self.MainCamera.zoomRatio = 0.8;
  184. this.myx = 432 / self.MainCamera.zoomRatio;
  185. this.myy = 768 / self.MainCamera.zoomRatio;
  186. var delta = event.touch.getDelta();
  187. self.MainCamera.node.x -= delta.x / self.MainCamera.zoomRatio;
  188. self.MainCamera.node.y -= delta.y / self.MainCamera.zoomRatio;
  189. if (Math.abs(delta.x) > 2 || Math.abs(delta.y) > 2)
  190. self.isUICameraMove = true;
  191. // cc.log(Math.abs(delta.x),'Math.abs(delta.y)',Math.abs(delta.y));
  192. // 控制节点移不出屏幕;
  193. //Constants.MoveX 2700
  194. var minX = -(Constants.MoveLeftX + 1000 * self.MainCamera.zoomRatio);//;//最小X坐标;
  195. var maxX = Constants.MoveRightX + 1000 * self.MainCamera.zoomRatio;//Constants.MoveMaxX;
  196. var minY = -(Constants.MoveDownY + 1550 * self.MainCamera.zoomRatio); //最小Y坐标;
  197. var maxY = Constants.MoveUpY + 1550 * self.MainCamera.zoomRatio;
  198. var nPos = self.MainCamera.node.position; //节点实时坐标;
  199. if (nPos.x < minX) {
  200. nPos.x = minX;
  201. };
  202. if (nPos.x > maxX) {
  203. nPos.x = maxX;
  204. };
  205. if (nPos.y < minY) {
  206. nPos.y = minY;
  207. };
  208. if (nPos.y > maxY) {
  209. nPos.y = maxY;
  210. };
  211. self.MainCamera.node.setPosition(nPos);
  212. // self.Show.string = "(" + self.MainCamera.node.position + ")";
  213. self._deteItem();
  214. }
  215. }.bind(this), self.node);
  216. return;
  217. //给所有的点注册事件,会自动冒泡到此节点
  218. for (var i = 0; i < this.BuildNodeArray.length; i++) {
  219. var BuildNodes = this.BuildNodeArray;
  220. //闭包传递i值
  221. (function (i) {
  222. var BuildNode = BuildNodes[i];
  223. BuildNode.on(cc.Node.EventType.TOUCH_START, function (event) {
  224. cc.log('BuildNode TOUCH_START');
  225. }, BuildNode);
  226. BuildNode.on(cc.Node.EventType.TOUCH_MOVE, function (event) {
  227. cc.log('BuildNode TOUCH_MOVE');
  228. }, BuildNode);
  229. BuildNode.on(cc.Node.EventType.TOUCH_END, function (event) {
  230. cc.log('BuildNode TOUCH_END');
  231. }, BuildNode);
  232. BuildNode.on(cc.Node.EventType.TOUCH_CANCEL, function (event) {
  233. cc.log('BuildNode TOUCH_CANCEL');
  234. }, BuildNode);
  235. })(i)
  236. }
  237. },
  238. update(dt) {
  239. if (cameraMoveType.none != this.cameraMoveType) {
  240. let deltaX = this._currentDelta.x > 0 ? 1 : -1;
  241. let deltaY = this._currentDelta.y > 0 ? 1 : -1;
  242. let speedX = (deltaX / this.MainCamera.zoomRatio) * dt * this.boundaryMoveSpeed;
  243. let speedY = (deltaY / this.MainCamera.zoomRatio) * dt * this.boundaryMoveSpeed;
  244. if (GlobalD.game.currentEditorBuilding) {
  245. if ('TouchMoveBuilding' == GlobalD.game.currentEditorBuilding.name) {
  246. GlobalD.game.currentEditorBuilding.getComponent('DynamicBuilding').onAutoMove(cc.v2(speedX, speedY));
  247. } else {
  248. GlobalD.game.currentEditorBuilding.getComponent('buildingsTouch').onAutoMove(cc.v2(speedX, speedY));
  249. }
  250. this.MainCamera.node.x += speedX;
  251. this.MainCamera.node.y += speedY;
  252. // 控制节点移不出屏幕;
  253. //Constants.MoveX 2700
  254. var minX = -(Constants.MoveX + 500 * this.MainCamera.zoomRatio);//;//最小X坐标;
  255. var maxX = Constants.MoveX + 500 * this.MainCamera.zoomRatio;//Constants.MoveMaxX;
  256. var minY = -(Constants.MoveY + 775 * this.MainCamera.zoomRatio); //最小Y坐标;
  257. var maxY = Constants.MoveY + 775 * this.MainCamera.zoomRatio;
  258. var nPos = this.MainCamera.node.position; //节点实时坐标;
  259. if (nPos.x < minX) {
  260. nPos.x = minX;
  261. };
  262. if (nPos.x > maxX) {
  263. nPos.x = maxX;
  264. };
  265. if (nPos.y < minY) {
  266. nPos.y = minY;
  267. };
  268. if (nPos.y > maxY) {
  269. nPos.y = maxY;
  270. };
  271. this.MainCamera.node.setPosition(nPos);
  272. }
  273. }
  274. },
  275. getNode: function (name, parent) {
  276. if (parent == null) {
  277. return this.node.getChildByName(name);
  278. } else {
  279. return parent.getChildByName(name);
  280. }
  281. },
  282. _deteItem() {
  283. //建筑
  284. let buidingsLength = GlobalD.game.buildingsTiledMapUnit.length;
  285. for (let index = 0; index < buidingsLength; index++) {
  286. let _buildinginfo = GlobalD.game.buildingsTiledMapUnit[index].getBuildingsInfo;
  287. let _isShow = this._detecting(_buildinginfo.node);
  288. if (_buildinginfo.buildInfo.BuildingSprite) {
  289. _buildinginfo.buildInfo.BuildingSprite.node.active = _isShow;
  290. }
  291. // else {
  292. // console.log("预制中不存在的BuildingSprite", _buildinginfo.node.name)
  293. // }
  294. }
  295. //公路
  296. let _HighwayLayer = GlobalD.TiledMap._tiledMap.getLayer('Highway');
  297. let _highwayChild = _HighwayLayer.node.children;
  298. for (let index = 0; index < _highwayChild.length; index++) {
  299. let _highwayNode = _highwayChild[index];
  300. _highwayNode.active = this._detecting(_highwayNode);
  301. }
  302. },
  303. _detecting(_targetNode) {
  304. let _worldpos = _targetNode.parent.convertToWorldSpaceAR(_targetNode.position);
  305. //取得的节点减去canvas的偏移
  306. let tempPos = cc.v2(_worldpos.x - this.Canvas.x, _worldpos.y - this.Canvas.y);
  307. // let _MaxY = this.MainCamera.node.position.y + 640;
  308. // let _minY = this.MainCamera.node.position.y - 640;
  309. // let _MaxX = this.MainCamera.node.position.x + 360;
  310. // let _minX = this.MainCamera.node.position.x - 360;
  311. // console.log("是啥x",this.myx);
  312. // console.log("是啥y",this.myy);
  313. let _MaxY = this.MainCamera.node.position.y + this.myy;
  314. let _minY = this.MainCamera.node.position.y - this.myy;
  315. let _MaxX = this.MainCamera.node.position.x + this.myx;
  316. let _minX = this.MainCamera.node.position.x - this.myx;
  317. if (tempPos.y < _MaxY
  318. && tempPos.y > _minY
  319. && tempPos.x < _MaxX
  320. && tempPos.x > _minX
  321. ) {
  322. // console.log("摄像机里面");
  323. return true;
  324. } else {
  325. // console.log('摄像机外面');
  326. return false;
  327. }
  328. }
  329. });