buildingsTouch.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. cc.Class({
  2. extends: cc.Component,
  3. properties: {
  4. isHiddenTouch: {
  5. default: false,
  6. tooltip: "初始化时候是否绑定touch"
  7. },
  8. //是否显示房屋信息
  9. isShowBuildingInfo: { default: false },
  10. // 是否是在预制生成
  11. _isInstance: { default: false, visible: false },
  12. //如果进入编辑状态
  13. _ifCanEdit: { default: false, visible: false },
  14. //触摸多久进入编辑状态
  15. touchTimer: { default: 0.8, visible: false },
  16. // //绘制底部颜色区域
  17. // Draw: cc.Node,
  18. //触摸一开始的位置
  19. _touchPosition: {
  20. default: new cc.Vec2(),
  21. visible: false,
  22. serializable: false
  23. },
  24. _EndPosition: {
  25. default: new cc.Vec2(),
  26. visible: false,
  27. serializable: false
  28. },
  29. //如果自动移动
  30. isAutoMove: { default: false, visible: false },
  31. targetNode: cc.Node,
  32. //编辑时候,记录旧的遮挡位置
  33. _OldIndex: 0,
  34. //是否显示节点
  35. isShowInfo: { default: false },
  36. infoNode: cc.Node,
  37. //如果是在新手引导时候建造的建筑
  38. //只要拖拽到虚影对应的位置,才能成功建造
  39. },
  40. // LIFE-CYCLE CALLBACKS:
  41. onLoad() {
  42. this._buildingsInfo = this.node.getComponent('buildingsInfo');
  43. this.buildInfo = this._buildingsInfo.buildInfo;
  44. this.title = this._buildingsInfo.title;
  45. if (!this.isHiddenTouch) {
  46. this.node.on(cc.Node.EventType.TOUCH_START, this.TouchStartFunction, this);
  47. this.node.on(cc.Node.EventType.TOUCH_END, this.TouchEndFunction, this);
  48. this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.TouchCancelFunction, this);
  49. }
  50. // console.log("this.isHiddenTouch",this.isHiddenTouch);
  51. this.Draw = this._buildingsInfo._buildZone;
  52. if (!this.Draw)
  53. this.Draw = this.node.getChildByName('Draw');
  54. this.Draw.active = false;
  55. },
  56. start() {
  57. this.InitStartPos = this._buildingsInfo.InitStartPos;
  58. this._camera = GlobalD.game.MainCamera;
  59. this._canvas = GlobalD.game.Canvas;
  60. this._tileMap = GlobalD.TiledMap;
  61. },
  62. TouchStartFunction() {
  63. // console.log("TouchStartFunction");
  64. this._touchPosition = this.node.getPosition();
  65. GlobalD.GameControl._isBuildingMove = false;
  66. this._isMove = false;
  67. this.unschedule(this.callback);
  68. this.scheduleOnce(this.callback = function () {
  69. //编辑器移动的话,返回
  70. if (GlobalD.GameControl.isUICameraMove) return;
  71. //进入编辑状态
  72. // cc.log('进入编辑状态!',GlobalD.GameControl.isUICameraMove);
  73. // this.onEditorStatus(false);
  74. //按住可以编辑
  75. }, this.touchTimer);
  76. },
  77. //生成房屋时候的预制状态
  78. // 编辑状态
  79. onEditorStatus(isInstance) {
  80. // cc.log('onEditorStatus', isInstance);
  81. //如果是编辑状态下,返回
  82. if (this._ifCanEdit) return;
  83. //只要是编辑状态下。设为true
  84. GlobalD.GameControl._isBuildingCanEdit = true;
  85. this._isInstance = isInstance;
  86. this._ifCanEdit = true;
  87. this.node.on(cc.Node.EventType.TOUCH_MOVE, this.MoveFunction, this);
  88. GlobalD.game._ManageUIScript.onEditorialBuildings(this.node);
  89. GlobalD.game.onSetCurrentBuildingTarget(this.node);
  90. if (!this.Draw)
  91. this.Draw = this.node.getChildByName('Draw')
  92. this.Draw.active = true;
  93. if (isInstance) {
  94. // cc.log(11111);
  95. this._currentTiledValue = GlobalD.TiledMap._tilePosFromLocation(this.node.position);
  96. let endPos = GlobalD.TiledMap._getTheMiddleLocationFromtilePos(this._currentTiledValue);
  97. this.node.setPosition(endPos);
  98. this._canNotBuild = GlobalD.game.areTheraOverlappingAreas(this._currentTiledValue, this.buildInfo.occupyArea, 1);
  99. } else {// if (this._tileMap)
  100. // cc.log(22222);
  101. this._currentTiledValue = this._tileMap._tilePosFromLocation(this._touchPosition);
  102. this._canNotBuild = GlobalD.game.areTheraOverlappingAreas(this._currentTiledValue, this.buildInfo.occupyArea, 1);
  103. }
  104. if (task.virtualShadowPos)
  105. this._canNotBuild = this._currentTiledValue.sub(task.virtualShadowPos).mag() == 0 ? false : true;
  106. GlobalD.game.onSpawnEditorBuildingTip(this.Draw, this._currentTiledValue, this.buildInfo.occupyArea, this._canNotBuild, this.buildInfo.buildType);
  107. this._OldIndex = this.node.zIndex;
  108. //设置最高显示
  109. this.node.zIndex = 1000;
  110. },
  111. TouchCancelFunction() {
  112. // console.log("TouchCancelFunction");
  113. this.unschedule(this.callback);
  114. GlobalD.GameControl._isBuildingMove = false;
  115. },
  116. TouchEndFunction() {
  117. // console.log("TouchEndFunction");
  118. // console.log("点击建筑物 之后",this.getOnClickTags());
  119. if (this.getOnClickTags()) {
  120. return;
  121. }
  122. GlobalD.GameControl._isBuildingMove = false;
  123. this.unschedule(this.callback);
  124. // cc.warn('touch End!');
  125. if (this.isShowInfo) {
  126. //关闭其他
  127. let _landBuildingsInfo = this.node.parent.children;
  128. for (let i = _landBuildingsInfo.length - 1; i >= 0; i--) {
  129. let resetLandInfo = _landBuildingsInfo[i];
  130. if (resetLandInfo.name == "Labour_205_Holy_Farmland") {
  131. let _buildInfo = resetLandInfo.getComponent("buildingsInfo").buildInfo;
  132. let leaseFarmlandInfoScript = resetLandInfo.getComponent("LeaseFarmlandInfo");
  133. if (_buildInfo.id == this.buildInfo.id) {
  134. leaseFarmlandInfoScript.onSetInfoBoxActive(true);
  135. } else {
  136. leaseFarmlandInfoScript.onSetInfoBoxActive(false);
  137. }
  138. }
  139. }
  140. }
  141. console.log("this.isShowBuildingInfo:", this.isShowBuildingInfo);
  142. //如果没有移动就判断点击
  143. if (!this._isMove
  144. && this.isShowBuildingInfo
  145. && !this._ifCanEdit
  146. && !GlobalD.GameControl.isUICameraMove
  147. && !GlobalD.GameControl._isBuildingCanEdit)
  148. GlobalD.game._ManageUIScript.onShowBuildingInfo(this.buildInfo);
  149. },
  150. MoveFunction(event) {
  151. // cc.log("MoveFunction");
  152. var delta = event.touch.getDelta();
  153. this._touchPosition.x += delta.x / this._camera.zoomRatio;
  154. this._touchPosition.y += delta.y / this._camera.zoomRatio;
  155. // var worldPos = this.node.parent.convertToWorldSpaceAR(this._touchPosition);
  156. //取得的节点减去canvas的偏移
  157. // let tempPos = cc.v2(worldPos.x - this._canvas.x, worldPos.y - this._canvas.y);
  158. this._currentTiledValue = this._tileMap._tilePosFromLocation(this._touchPosition);
  159. //不相等的时候才绘制
  160. if (this._currentTiledValue.sub(this._EndPosition).mag() != 0) {
  161. this._EndPosition = this._currentTiledValue;
  162. let endPos = this._tileMap._getTheMiddleLocationFromtilePos(this._currentTiledValue);
  163. this.node.setPosition(endPos);
  164. let isHas = this._canNotBuild = GlobalD.game.areTheraOverlappingAreas(this._currentTiledValue, this.buildInfo.occupyArea, 1);
  165. // this.title.string = "(" + (this._currentTiledValue.x) + "," + (this._currentTiledValue.y) + ")" + isHas;
  166. if (task.virtualShadowPos)
  167. this._canNotBuild = this._EndPosition.sub(task.virtualShadowPos).mag() == 0 ? false : true;
  168. GlobalD.game.onSpawnEditorBuildingTip(this.Draw, this._currentTiledValue, this.buildInfo.occupyArea, this._canNotBuild, this.buildInfo.buildType);
  169. }
  170. this._isMove = true;
  171. GlobalD.GameControl._isBuildingMove = true;
  172. },
  173. // 自动移动,在ManagerControl 里面调用
  174. onAutoMove(delta) {
  175. this.isAutoMove = true;
  176. this._touchPosition.x += delta.x;
  177. this._touchPosition.y += delta.y;
  178. this._currentTiledValue = this._tileMap._tilePosFromLocation(this._touchPosition);
  179. //不相等的时候才绘制
  180. if (this._currentTiledValue.sub(this._EndPosition).mag() != 0) {
  181. this._EndPosition = this._currentTiledValue;
  182. let endPos = this._tileMap._getTheMiddleLocationFromtilePos(this._currentTiledValue);
  183. this.node.setPosition(endPos);
  184. let isHas = this._canNotBuild = GlobalD.game.areTheraOverlappingAreas(this._currentTiledValue, this.buildInfo.occupyArea, 1);
  185. // this.title.string = "(" + (this._currentTiledValue.x) + "," + (this._currentTiledValue.y) + ")" + isHas;
  186. if (task.virtualShadowPos)
  187. this._canNotBuild = this._EndPosition.sub(task.virtualShadowPos).mag() == 0 ? false : true;
  188. GlobalD.game.onSpawnEditorBuildingTip(this.Draw, this._currentTiledValue, this.buildInfo.occupyArea, this._canNotBuild, this.buildInfo.buildType);
  189. }
  190. },
  191. onCancleAutoMove() {
  192. this.isAutoMove = false;
  193. },
  194. /**
  195. * 编辑房子时候
  196. * 完成建造房子流程
  197. */
  198. onFinishEdit() {
  199. GlobalD.GameControl._isBuildingCanEdit = false;
  200. GlobalD.game.onClearCurrentBuildingTarget();
  201. // cc.log(this._ifCanEdit, this._canNotBuild);
  202. if (this._canNotBuild) {
  203. //如果不可建造区域,跳回起始位置
  204. this.onCancelEdit();
  205. return;
  206. }
  207. //如果完成时候,还在初始化生成的状态,重置为false
  208. if (this._isInstance) {
  209. this._isInstance = false;
  210. GlobalD.game._ManageUIScript.onBottomMenuView(true);
  211. //只有初始化建筑建造成功才移除建筑数据
  212. /**
  213. * 操作记录的数据减少1
  214. */
  215. GlobalD.game.MuinusBuilding(this.buildInfo.buildingName);
  216. }
  217. if (!this._ifCanEdit) return;
  218. //把物体定位到对应的坐标上去
  219. if (this._currentTiledValue) {
  220. //如果可以移动的话,更新一次位置
  221. this.InitStartPos = this._currentTiledValue;
  222. var endTiledPos = cc.v2(this._currentTiledValue.x, this._currentTiledValue.y);
  223. var endPos = this._tileMap._getTheMiddleLocationFromtilePos(endTiledPos);
  224. this.node.setPosition(endPos);
  225. let isHas = GlobalD.game.doesItExistArray(this.buildInfo.id);
  226. if (!isHas) {
  227. //起始坐标,占位范围,是否占位
  228. GlobalD.game.addBuildTiled(this.buildInfo.id, this._currentTiledValue, this.buildInfo.occupyArea);
  229. this.buildInfo.startTilePos = this._currentTiledValue;
  230. GlobalD.game.addBuilding(this._buildingsInfo);
  231. } else {//如果已经存在,更新新位置
  232. //可以移动
  233. let isMove = true;
  234. GlobalD.game.updateBuildOccupy(isMove, this.buildInfo.id, this._currentTiledValue, this.buildInfo.occupyArea);
  235. this.buildInfo.startTilePos = this._currentTiledValue;
  236. GlobalD.game.updateBuilding(this._buildingsInfo);
  237. }
  238. }
  239. //完成后从新设置状态
  240. this.Draw.active = false;
  241. this._ifCanEdit = false;
  242. this.node.off(cc.Node.EventType.TOUCH_MOVE, this.MoveFunction, this);
  243. // cc.log('this.Draw.active', this.Draw.active);
  244. // cc.log('this._buildingsInfo='+this._buildingsInfo.buildInfo.EnglishName);
  245. //设置影响力
  246. GlobalD.game.onFinishAddEditorBuildingsEffect(this._buildingsInfo);
  247. if (task.TaskIconCountClick >= 13)
  248. return;
  249. //引导 开始 建造 农舍 确定之后
  250. if (task.taskCursor == 0 && this.node.name == "Env_101_house_low") {
  251. task.taskShare();
  252. task.onShadowArchitectureReset();
  253. }
  254. if (this.node.name == "Labour_201_Farmland") {
  255. task.Farming();
  256. task.onShadowArchitectureReset();
  257. }
  258. // if (this.node.name == "Labour_204_Factory") {
  259. // //盖一个工厂
  260. // task.taskfactory();
  261. // }
  262. // if (this.node.name == "Shops_30101_ColdDrinkStall") {
  263. // //盖一个便利店
  264. // task.taskConvenienceStore();
  265. // }
  266. // //
  267. // if (this.node.name == "Spe_402_GreenBelt") {
  268. // task.taskGreenBeltShare();
  269. // }
  270. // if (this.node.name == "Env_101_house_low") {
  271. // task.taskFarmhouse();
  272. // }
  273. UtilsWX.getAllBuild();
  274. },
  275. onTaskBuild() {
  276. // console.log(this._canNotBuild, task.isMushBuildState);
  277. if (task.isMushBuildState && this._canNotBuild) {
  278. //如果不可建造区域
  279. if (this.node.name == "Env_101_house_low" || this.node.name == "Labour_201_Farmland") {
  280. // task.onTaskBuildAHouseMask();
  281. cc.loader.loadRes('resUI/ShowNotEnoughMoney', function (err, texture) {
  282. var prefab = cc.instantiate(texture);
  283. prefab.getComponent('ShowNotEnoughMoney').Text('请在指定位置建造!');
  284. // this.node.addChild(prefab);
  285. cc.find('Canvas').getChildByName('UICamera').addChild(prefab);
  286. }.bind(this));
  287. }
  288. return false;
  289. }
  290. return true;
  291. },
  292. onCancelEdit() {
  293. GlobalD.GameControl._isBuildingCanEdit = false;
  294. this.node.zIndex = this._OldIndex;
  295. // cc.log('onCancelEdit');
  296. GlobalD.game.onClearCurrentBuildingTarget();
  297. if (this._isInstance) {
  298. // cc.log('删除1');
  299. //清除底部的提示,放回对象池
  300. GlobalD.game.onClearSpawnEditorBuildingTip();
  301. GlobalD.game._ManageUIScript.onBottomMenuView(true);
  302. //如果是预制生成的,取消生成的时候,删除预制
  303. this.scheduleOnce(() => {
  304. this.node.destroy();
  305. }, 0)
  306. } else {
  307. // cc.log('删除2');
  308. this.Draw.active = false;
  309. this._ifCanEdit = false;
  310. // cc.log(' buildingsInfo.buildInfo.InitStartPos', this.InitStartPos);
  311. this.node.off(cc.Node.EventType.TOUCH_MOVE, this.MoveFunction, this);
  312. var endTiledPos = cc.v2(this.InitStartPos.x, this.InitStartPos.y);
  313. var endPos = this._tileMap._getTheMiddleLocationFromtilePos(endTiledPos);
  314. this.node.setPosition(endPos);
  315. }
  316. },
  317. /**
  318. * 需要添加碰撞体
  319. * 不规则多边形触摸测试
  320. * @param {触摸点} point
  321. * @param {监听} listener
  322. */
  323. polygonHitTest(point, listener) {
  324. var polygonCollider = this.getComponent(cc.PolygonCollider);
  325. if (polygonCollider) {
  326. point = this.convertToNodeSpace(point);
  327. point.x -= this.getContentSize().width / 2;
  328. point.y -= this.getContentSize().height / 2;
  329. return cc.Intersection.pointInPolygon(point, polygonCollider.points);
  330. } else {
  331. return this._oldHitTest(point, listener);
  332. }
  333. },
  334. /**
  335. * 判断是否可以被优先点击
  336. * @returns {*}
  337. */
  338. getOnClickTags: function () {
  339. if (this.getComponent("Collect") != null) {
  340. return this.getComponent("Collect").getOnClickTag();
  341. } else {
  342. return false;
  343. }
  344. }
  345. });