buildingsTouch.js 15 KB

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