ManagerControl.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. onLoad() {
  47. GlobalD.GameControl = this;
  48. this.initShare();
  49. // var Building = cc.find("Canvas/UICamera/BelowTheMask/Building");
  50. },
  51. initShare: function () {
  52. this.UICamera = this.node;
  53. this._showShare(function () {
  54. this.sharegosharebtn.on(cc.Node.EventType.TOUCH_END, function (event) {
  55. // console.log("点击分享按钮");
  56. UtilsWX.sharebtn();
  57. }.bind(this));
  58. }.bind(this));
  59. },
  60. _showShare: function (callback) {
  61. return;
  62. cc.loader.loadRes("prefab/shareview", function (err, texture) {
  63. this.shareview = cc.instantiate(texture);
  64. if (this.UICamera.getChildByName(this.shareview.name) != null) {
  65. this.UICamera.getChildByName(this.shareview.name).destroy();
  66. }
  67. this.UICamera.addChild(this.shareview, -99);
  68. // this.dialogLuckView.active = false;
  69. // this.shareview.zIndex=-99;
  70. this.sharegosharebtn = this.getNode("gosharebtn", this.shareview);
  71. // this.close = this.getNode("close",this.dialogLuckView);
  72. this.sharegosharebtn.off(cc.Node.EventType.TOUCH_END);
  73. this.shareview.y = 500;
  74. this.shareview.x = -200;
  75. if (callback != null) {
  76. callback();
  77. }
  78. }.bind(this));
  79. },
  80. start() {
  81. var isTouch = false
  82. var self = this, parent = this.node.parent; this.MainCamera = GlobalD.game.MainCamera; this.Canvas = GlobalD.game.Canvas;
  83. self.target = self.MainCamera.node;
  84. // self.target.setScale(0.7);
  85. self.target.setScale(1.2);
  86. self.MainCamera.zoomRatio = 1.2;//0.7;
  87. self.myx = 432 / self.MainCamera.zoomRatio;
  88. self.myy = 768 / self.MainCamera.zoomRatio;
  89. this.node.on(cc.Node.EventType.TOUCH_END, function (event) {
  90. //设置为不移动
  91. self.cameraMoveType = cameraMoveType.none;
  92. // self.isUICameraMove = false;
  93. }, this);
  94. this.node.on(cc.Node.EventType.TOUCH_CANCEL, function (event) {
  95. //设置为不移动
  96. self.cameraMoveType = cameraMoveType.none;
  97. // self.isUICameraMove = false;
  98. }, this);
  99. this.node.on(cc.Node.EventType.TOUCH_START, function (event) {
  100. self.isUICameraMove = false;
  101. // 起始触摸位置
  102. // this.touchLocation = event.touch.getLocation();
  103. if (event.type === cc.Node.EventType.TOUCH_START && isTouch == false) {
  104. //穿透正常响应UI事件
  105. // this.BuildNodeArray[0]._touchListener.setSwallowTouches(false);
  106. this.node._touchListener.setSwallowTouches(false);
  107. } else {
  108. //吞掉事件 不让他继续传下去
  109. this.node._touchListener.setSwallowTouches(true);
  110. event.stopPropagation();
  111. }
  112. }, this);
  113. self.node.on(cc.Node.EventType.TOUCH_MOVE, function (event) {
  114. if (self.UIMask && self.UIMask.active) return;
  115. if (self._isBuildingMove) {
  116. // cc.log(event.touch.getDelta());
  117. //如果是编辑建筑物时候
  118. let uiTouchPos = event.touch.getLocation();
  119. // cc.log(uiTouchPos);
  120. if (uiTouchPos.x <= self.boundarylimitValue) {
  121. // cc.log('屏幕左边', self.cameraMoveType);
  122. if (self.cameraMoveType != cameraMoveType.moveLeft) {
  123. self._currentDelta = event.touch.getDelta();
  124. }
  125. self.cameraMoveType = cameraMoveType.moveLeft;
  126. } else if (uiTouchPos.y <= self.boundarylimitValue) {
  127. // cc.log('屏幕下边');
  128. if (self.cameraMoveType != cameraMoveType.moveDown) {
  129. self._currentDelta = event.touch.getDelta();
  130. }
  131. self.cameraMoveType = cameraMoveType.moveDown;
  132. } else if (uiTouchPos.x >= self.node.width - self.boundarylimitValue) {
  133. // cc.log('屏幕右边');
  134. if (self.cameraMoveType != cameraMoveType.moveRight) {
  135. self._currentDelta = event.touch.getDelta();
  136. }
  137. self.cameraMoveType = cameraMoveType.moveRight;
  138. } else if (uiTouchPos.y >= self.node.height - self.boundarylimitValue) {
  139. // cc.log('屏幕上边');
  140. if (self.cameraMoveType != cameraMoveType.moveUp) {
  141. self._currentDelta = event.touch.getDelta();
  142. }
  143. self.cameraMoveType = cameraMoveType.moveUp;
  144. } else {
  145. if (self.cameraMoveType != cameraMoveType.none) {
  146. self._currentDelta = 0;
  147. }
  148. self.cameraMoveType = cameraMoveType.none;
  149. }
  150. return
  151. };
  152. // cc.log(self.isUICameraMove);
  153. var touches = event.getTouches();
  154. // cc.log(touches);
  155. if (touches.length >= 2) {
  156. var touch1 = touches[0], touch2 = touches[1];
  157. var delta1 = touch1.getDelta(), delta2 = touch2.getDelta();
  158. var touchPoint1 = parent.convertToNodeSpaceAR(touch1.getLocation());
  159. var touchPoint2 = parent.convertToNodeSpaceAR(touch2.getLocation());
  160. //缩放
  161. var distance = touchPoint1.sub(touchPoint2);
  162. var delta = delta1.sub(delta2);
  163. var scale = 1;
  164. if (Math.abs(distance.x) > Math.abs(distance.y)) {
  165. scale = (distance.x + delta.x) / distance.x * self.target.scale;
  166. }
  167. else {
  168. scale = (distance.y + delta.y) / distance.y * self.target.scale;
  169. }
  170. // self.target.scale = scale < 0.1 ? 0.1 : scale;
  171. self.target.scale = scale = scale <= 0.4 ? 0.4 : scale;
  172. self.target.scale = scale = scale >= 1.2 ? 1.2 : scale;
  173. // self.Show.string = "(" + scale + ")";
  174. self.MainCamera.zoomRatio = scale;
  175. this.myx = 432 / self.MainCamera.zoomRatio;
  176. this.myy = 768 / self.MainCamera.zoomRatio;
  177. self.isUICameraMove = true;
  178. self._deteItem();
  179. } else {
  180. // 640 360
  181. //533.33 300
  182. // self.MainCamera.zoomRatio = 0.8;
  183. this.myx = 432 / self.MainCamera.zoomRatio;
  184. this.myy = 768 / self.MainCamera.zoomRatio;
  185. var delta = event.touch.getDelta();
  186. self.MainCamera.node.x -= delta.x / self.MainCamera.zoomRatio;
  187. self.MainCamera.node.y -= delta.y / self.MainCamera.zoomRatio;
  188. if (Math.abs(delta.x) > 2 || Math.abs(delta.y) > 2)
  189. self.isUICameraMove = true;
  190. // cc.log(Math.abs(delta.x),'Math.abs(delta.y)',Math.abs(delta.y));
  191. // 控制节点移不出屏幕;
  192. //Constants.MoveX 2700
  193. var _value = (1 - self.MainCamera.zoomRatio) * 400;
  194. var minX = -(Constants.MoveX + 1000 * self.MainCamera.zoomRatio);//;//最小X坐标;
  195. var maxX = Constants.MoveX + 1000 * self.MainCamera.zoomRatio;//Constants.MoveMaxX;
  196. var minY = -((Constants.MoveY + 1550 * self.MainCamera.zoomRatio) + _value); //最小Y坐标;
  197. var maxY = (Constants.MoveY + 1550 * self.MainCamera.zoomRatio) + _value;
  198. //console.log('maxY:', maxY, 'self.MainCamera.zoomRatio:', self.MainCamera.zoomRatio);
  199. var nPos = self.MainCamera.node.position; //节点实时坐标;
  200. if (nPos.x < minX) {
  201. nPos.x = minX;
  202. };
  203. if (nPos.x > maxX) {
  204. nPos.x = maxX;
  205. };
  206. if (nPos.y < minY) {
  207. nPos.y = minY;
  208. };
  209. if (nPos.y > maxY) {
  210. nPos.y = maxY;
  211. };
  212. self.MainCamera.node.setPosition(nPos);
  213. // self.Show.string = "(" + self.MainCamera.node.position + ")";
  214. self._deteItem();
  215. }
  216. }.bind(this), self.node);
  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. });