buildingsTouch.js 15 KB

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