ManagerControl.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. },
  51. start() {
  52. var isTouch = false
  53. var self = this, parent = this.node.parent; this.MainCamera = GlobalD.game.MainCamera; this.Canvas = GlobalD.game.Canvas;
  54. self.target = self.MainCamera.node;
  55. self.target.setScale(1);
  56. // self.target.setScale(1.2);
  57. self.MainCamera.zoomRatio = 1;//0.7;
  58. self.myx = 432 / self.MainCamera.zoomRatio;
  59. self.myy = 768 / self.MainCamera.zoomRatio;
  60. this.node.on(cc.Node.EventType.TOUCH_END, function (event) {
  61. //设置为不移动
  62. self.cameraMoveType = cameraMoveType.none;
  63. self.isUICameraMove = false;
  64. if (self.isUICameraMove) {
  65. //这里处理一下ui重置效果,
  66. GlobalD.game._ManageUIScript.onResetUI();
  67. //记录当前选中的buildInfo
  68. GlobalD.game.onSetCurrentSelectLeaseLandInfo(null);
  69. this.node._touchListener.setSwallowTouches(true);
  70. event.stopPropagation();
  71. } else {
  72. this.node._touchListener.setSwallowTouches(false);
  73. }
  74. }, this);
  75. this.node.on(cc.Node.EventType.TOUCH_CANCEL, function (event) {
  76. //设置为不移动
  77. self.cameraMoveType = cameraMoveType.none;
  78. self.isUICameraMove = false;
  79. }, this);
  80. this.node.on(cc.Node.EventType.TOUCH_START, function (event) {
  81. self.isUICameraMove = false;
  82. // 起始触摸位置
  83. // this.touchLocation = event.touch.getLocation();
  84. if (event.type === cc.Node.EventType.TOUCH_START && isTouch == false) {
  85. //穿透正常响应UI事件
  86. // this.BuildNodeArray[0]._touchListener.setSwallowTouches(false);
  87. this.node._touchListener.setSwallowTouches(false);
  88. } else {
  89. //吞掉事件 不让他继续传下去
  90. this.node._touchListener.setSwallowTouches(true);
  91. event.stopPropagation();
  92. }
  93. }, this);
  94. self.node.on(cc.Node.EventType.TOUCH_MOVE, function (event) {
  95. if (self.UIMask && self.UIMask.active || self.isTaskInProgress) return;
  96. if (self._isBuildingMove) {
  97. // // cc.log(event.touch.getDelta());
  98. // //如果是编辑建筑物时候
  99. // let uiTouchPos = event.touch.getLocation();
  100. // // cc.log(uiTouchPos);
  101. // if (uiTouchPos.x <= self.boundarylimitValue) {
  102. // // cc.log('屏幕左边', self.cameraMoveType);
  103. // if (self.cameraMoveType != cameraMoveType.moveLeft) {
  104. // self._currentDelta = event.touch.getDelta();
  105. // }
  106. // self.cameraMoveType = cameraMoveType.moveLeft;
  107. // } else if (uiTouchPos.y <= self.boundarylimitValue) {
  108. // // cc.log('屏幕下边');
  109. // if (self.cameraMoveType != cameraMoveType.moveDown) {
  110. // self._currentDelta = event.touch.getDelta();
  111. // }
  112. // self.cameraMoveType = cameraMoveType.moveDown;
  113. // } else if (uiTouchPos.x >= self.node.width - self.boundarylimitValue) {
  114. // // cc.log('屏幕右边');
  115. // if (self.cameraMoveType != cameraMoveType.moveRight) {
  116. // self._currentDelta = event.touch.getDelta();
  117. // }
  118. // self.cameraMoveType = cameraMoveType.moveRight;
  119. // } else if (uiTouchPos.y >= self.node.height - self.boundarylimitValue) {
  120. // // cc.log('屏幕上边');
  121. // if (self.cameraMoveType != cameraMoveType.moveUp) {
  122. // self._currentDelta = event.touch.getDelta();
  123. // }
  124. // self.cameraMoveType = cameraMoveType.moveUp;
  125. // } else {
  126. // if (self.cameraMoveType != cameraMoveType.none) {
  127. // self._currentDelta = 0;
  128. // }
  129. // self.cameraMoveType = cameraMoveType.none;
  130. // }
  131. return
  132. };
  133. // cc.log(self.isUICameraMove);
  134. var touches = event.getTouches();
  135. // cc.log(touches);
  136. if (touches.length >= 2) {
  137. var touch1 = touches[0], touch2 = touches[1];
  138. var delta1 = touch1.getDelta(), delta2 = touch2.getDelta();
  139. var touchPoint1 = parent.convertToNodeSpaceAR(touch1.getLocation());
  140. var touchPoint2 = parent.convertToNodeSpaceAR(touch2.getLocation());
  141. //缩放
  142. var distance = touchPoint1.sub(touchPoint2);
  143. var delta = delta1.sub(delta2);
  144. var scale = 1;
  145. if (Math.abs(distance.x) > Math.abs(distance.y)) {
  146. scale = (distance.x + delta.x) / distance.x * self.target.scale;
  147. }
  148. else {
  149. scale = (distance.y + delta.y) / distance.y * self.target.scale;
  150. }
  151. // self.target.scale = scale < 0.1 ? 0.1 : scale;
  152. self.target.scale = scale = scale <= 0.4 ? 0.4 : scale;
  153. self.target.scale = scale = scale >= 1 ? 1 : scale;
  154. // self.Show.string = "(" + scale + ")";
  155. self.MainCamera.zoomRatio = scale;
  156. this.myx = 432 / self.MainCamera.zoomRatio;
  157. this.myy = 768 / self.MainCamera.zoomRatio;
  158. self.isUICameraMove = true;
  159. self._deteItem();
  160. } else {
  161. // 640 360
  162. //533.33 300
  163. // self.MainCamera.zoomRatio = 0.8;
  164. this.myx = 432 / self.MainCamera.zoomRatio;
  165. this.myy = 768 / self.MainCamera.zoomRatio;
  166. var delta = event.touch.getDelta();
  167. self.MainCamera.node.x -= delta.x / self.MainCamera.zoomRatio;
  168. self.MainCamera.node.y -= delta.y / self.MainCamera.zoomRatio;
  169. if (Math.abs(delta.x) > 2 || Math.abs(delta.y) > 2)
  170. self.isUICameraMove = true;
  171. // cc.log(Math.abs(delta.x),'Math.abs(delta.y)',Math.abs(delta.y));
  172. // 控制节点移不出屏幕;
  173. //Constants.MoveX 2700
  174. var minX = -(Constants.MoveLeftX + 1000 * self.MainCamera.zoomRatio);//;//最小X坐标;
  175. var maxX = Constants.MoveRightX + 1000 * self.MainCamera.zoomRatio;//Constants.MoveMaxX;
  176. var minY = -(Constants.MoveDownY + 1550 * self.MainCamera.zoomRatio); //最小Y坐标;
  177. var maxY = Constants.MoveUpY + 1550 * self.MainCamera.zoomRatio;
  178. var nPos = self.MainCamera.node.position; //节点实时坐标;
  179. if (nPos.x < minX) {
  180. nPos.x = minX;
  181. };
  182. if (nPos.x > maxX) {
  183. nPos.x = maxX;
  184. };
  185. if (nPos.y < minY) {
  186. nPos.y = minY;
  187. };
  188. if (nPos.y > maxY) {
  189. nPos.y = maxY;
  190. };
  191. self.MainCamera.node.setPosition(nPos);
  192. // self.Show.string = "(" + self.MainCamera.node.position + ")";
  193. self._deteItem();
  194. }
  195. }.bind(this), self.node);
  196. return;
  197. //给所有的点注册事件,会自动冒泡到此节点
  198. for (var i = 0; i < this.BuildNodeArray.length; i++) {
  199. var BuildNodes = this.BuildNodeArray;
  200. //闭包传递i值
  201. (function (i) {
  202. var BuildNode = BuildNodes[i];
  203. BuildNode.on(cc.Node.EventType.TOUCH_START, function (event) {
  204. cc.log('BuildNode TOUCH_START');
  205. }, BuildNode);
  206. BuildNode.on(cc.Node.EventType.TOUCH_MOVE, function (event) {
  207. cc.log('BuildNode TOUCH_MOVE');
  208. }, BuildNode);
  209. BuildNode.on(cc.Node.EventType.TOUCH_END, function (event) {
  210. cc.log('BuildNode TOUCH_END');
  211. }, BuildNode);
  212. BuildNode.on(cc.Node.EventType.TOUCH_CANCEL, function (event) {
  213. cc.log('BuildNode TOUCH_CANCEL');
  214. }, BuildNode);
  215. })(i)
  216. }
  217. },
  218. update(dt) {
  219. if (cameraMoveType.none != this.cameraMoveType) {
  220. let deltaX = this._currentDelta.x > 0 ? 1 : -1;
  221. let deltaY = this._currentDelta.y > 0 ? 1 : -1;
  222. let speedX = (deltaX / this.MainCamera.zoomRatio) * dt * this.boundaryMoveSpeed;
  223. let speedY = (deltaY / this.MainCamera.zoomRatio) * dt * this.boundaryMoveSpeed;
  224. if (GlobalD.game.currentEditorBuilding) {
  225. if ('TouchMoveBuilding' == GlobalD.game.currentEditorBuilding.name) {
  226. GlobalD.game.currentEditorBuilding.getComponent('DynamicBuilding').onAutoMove(cc.v2(speedX, speedY));
  227. } else {
  228. GlobalD.game.currentEditorBuilding.getComponent('buildingsTouch').onAutoMove(cc.v2(speedX, speedY));
  229. }
  230. this.MainCamera.node.x += speedX;
  231. this.MainCamera.node.y += speedY;
  232. // 控制节点移不出屏幕;
  233. //Constants.MoveX 2700
  234. var minX = -(Constants.MoveX + 500 * this.MainCamera.zoomRatio);//;//最小X坐标;
  235. var maxX = Constants.MoveX + 500 * this.MainCamera.zoomRatio;//Constants.MoveMaxX;
  236. var minY = -(Constants.MoveY + 775 * this.MainCamera.zoomRatio); //最小Y坐标;
  237. var maxY = Constants.MoveY + 775 * this.MainCamera.zoomRatio;
  238. var nPos = this.MainCamera.node.position; //节点实时坐标;
  239. if (nPos.x < minX) {
  240. nPos.x = minX;
  241. };
  242. if (nPos.x > maxX) {
  243. nPos.x = maxX;
  244. };
  245. if (nPos.y < minY) {
  246. nPos.y = minY;
  247. };
  248. if (nPos.y > maxY) {
  249. nPos.y = maxY;
  250. };
  251. this.MainCamera.node.setPosition(nPos);
  252. }
  253. }
  254. },
  255. getNode: function (name, parent) {
  256. if (parent == null) {
  257. return this.node.getChildByName(name);
  258. } else {
  259. return parent.getChildByName(name);
  260. }
  261. },
  262. _deteItem() {
  263. //建筑
  264. let buidingsLength = GlobalD.game.buildingsTiledMapUnit.length;
  265. for (let index = 0; index < buidingsLength; index++) {
  266. let _buildinginfo = GlobalD.game.buildingsTiledMapUnit[index].getBuildingsInfo;
  267. let _isShow = this._detecting(_buildinginfo.node);
  268. if (_buildinginfo.buildInfo.BuildingSprite) {
  269. _buildinginfo.buildInfo.BuildingSprite.node.active = _isShow;
  270. }
  271. // else {
  272. // console.log("预制中不存在的BuildingSprite", _buildinginfo.node.name)
  273. // }
  274. }
  275. //公路
  276. // let _HighwayLayer = GlobalD.TiledMap._tiledMap.getLayer('Highway');
  277. // let _highwayChild = _HighwayLayer.node.children;
  278. // for (let index = 0; index < _highwayChild.length; index++) {
  279. // let _highwayNode = _highwayChild[index];
  280. // _highwayNode.active = this._detecting(_highwayNode);
  281. // }
  282. },
  283. _detecting(_targetNode) {
  284. let _worldpos = _targetNode.parent.convertToWorldSpaceAR(_targetNode.position);
  285. //取得的节点减去canvas的偏移
  286. let tempPos = cc.v2(_worldpos.x - this.Canvas.x, _worldpos.y - this.Canvas.y);
  287. // let _MaxY = this.MainCamera.node.position.y + 640;
  288. // let _minY = this.MainCamera.node.position.y - 640;
  289. // let _MaxX = this.MainCamera.node.position.x + 360;
  290. // let _minX = this.MainCamera.node.position.x - 360;
  291. // console.log("是啥x",this.myx);
  292. // console.log("是啥y",this.myy);
  293. let _MaxY = this.MainCamera.node.position.y + this.myy;
  294. let _minY = this.MainCamera.node.position.y - this.myy;
  295. let _MaxX = this.MainCamera.node.position.x + this.myx;
  296. let _minX = this.MainCamera.node.position.x - this.myx;
  297. if (tempPos.y < _MaxY
  298. && tempPos.y > _minY
  299. && tempPos.x < _MaxX
  300. && tempPos.x > _minX
  301. ) {
  302. // console.log("摄像机里面");
  303. return true;
  304. } else {
  305. // console.log('摄像机外面');
  306. return false;
  307. }
  308. }
  309. });