buildingsTouch.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. this._touchPosition = this.node.getPosition();
  64. this._isMove = false;
  65. },
  66. TouchCancelFunction() {
  67. this.unschedule(this.callback);
  68. },
  69. TouchEndFunction() {
  70. console.log("buildings touch end!");
  71. // if (this.getOnClickTags()) {
  72. // return;
  73. // }
  74. // GlobalD.GameControl._isBuildingMove = false;
  75. this.unschedule(this.callback);
  76. // console.log("this.isShowBuildingInfo:", this.isShowBuildingInfo);
  77. //如果没有移动就判断点击
  78. if (!this._isMove
  79. // && this.isShowBuildingInfo
  80. && !this._ifCanEdit
  81. && !GlobalD.GameControl.isUICameraMove
  82. && !GlobalD.GameControl._isBuildingCanEdit) {
  83. // GlobalD.game._ManageUIScript.onShowBuildingInfo(this.buildInfo);
  84. if (this.isShowInfo) {
  85. //关闭其他
  86. let _landBuildingsInfo = this.node.parent.children;
  87. for (let i = _landBuildingsInfo.length - 1; i >= 0; i--) {
  88. let resetLandInfo = _landBuildingsInfo[i];
  89. if (resetLandInfo.name == "Labour_205_Holy_Farmland") {
  90. let _buildInfo = resetLandInfo.getComponent("buildingsInfo").buildInfo;
  91. let leaseFarmlandInfoScript = resetLandInfo.getComponent("LeaseFarmlandInfo");
  92. if (_buildInfo.id == this.buildInfo.id) {
  93. //判断是否是自己的土地,和是否租赁的状态
  94. if (leaseFarmlandInfoScript && (leaseFarmlandInfoScript.leaseLandInfo == null || !leaseFarmlandInfoScript.bSelfLand)) {
  95. return;
  96. }
  97. //判断是否显示ui
  98. if (leaseFarmlandInfoScript && 1 === leaseFarmlandInfoScript.leaseLandInfo.isPlant && leaseFarmlandInfoScript.leaseLandInfo.seedInfo) {
  99. //显示工具ui
  100. GlobalD.game._ManageUIScript.onSwitchHideUI();
  101. }
  102. leaseFarmlandInfoScript.onSetInfoBoxActive(true);
  103. //记录当前选中的buildInfo
  104. GlobalD.game.onSetCurrentSelectLeaseLandInfo(leaseFarmlandInfoScript.leaseLandInfo);
  105. } else {
  106. leaseFarmlandInfoScript.onSetInfoBoxActive(false);
  107. }
  108. } else if (resetLandInfo.name == "Labour_206_Holy_Animal") {
  109. //牧场
  110. let _buildInfo = resetLandInfo.getComponent("buildingsInfo").buildInfo;
  111. let leaseAnimalInfoScript = resetLandInfo.getComponent("LeaseAnimalInfo");
  112. if (_buildInfo.id == this.buildInfo.id) {
  113. //判断是否是自己的土地,和是否租赁的状态
  114. if (leaseAnimalInfoScript && (leaseAnimalInfoScript.leaseLandInfo == null || !leaseAnimalInfoScript.bSelfLand)) {
  115. return;
  116. }
  117. leaseAnimalInfoScript.onSetInfoBoxActive(true);
  118. //记录当前选中的buildInfo
  119. GlobalD.game.onSetCurrentSelectLeaseLandInfo(leaseAnimalInfoScript.leaseLandInfo);
  120. } else {
  121. leaseAnimalInfoScript.onSetInfoBoxActive(false);
  122. }
  123. }
  124. }
  125. }
  126. }
  127. },
  128. MoveFunction(event) {
  129. // cc.log("MoveFunction");
  130. var delta = event.touch.getDelta();
  131. this._touchPosition.x += delta.x / this._camera.zoomRatio;
  132. this._touchPosition.y += delta.y / this._camera.zoomRatio;
  133. // var worldPos = this.node.parent.convertToWorldSpaceAR(this._touchPosition);
  134. //取得的节点减去canvas的偏移
  135. // let tempPos = cc.v2(worldPos.x - this._canvas.x, worldPos.y - this._canvas.y);
  136. this._currentTiledValue = this._tileMap._tilePosFromLocation(this._touchPosition);
  137. //不相等的时候才绘制
  138. if (this._currentTiledValue.sub(this._EndPosition).mag() != 0) {
  139. this._EndPosition = this._currentTiledValue;
  140. let endPos = this._tileMap._getTheMiddleLocationFromtilePos(this._currentTiledValue);
  141. this.node.setPosition(endPos);
  142. let isHas = this._canNotBuild = GlobalD.game.areTheraOverlappingAreas(this._currentTiledValue, this.buildInfo.occupyArea, 1);
  143. // this.title.string = "(" + (this._currentTiledValue.x) + "," + (this._currentTiledValue.y) + ")" + isHas;
  144. if (task.virtualShadowPos)
  145. this._canNotBuild = this._EndPosition.sub(task.virtualShadowPos).mag() == 0 ? false : true;
  146. GlobalD.game.onSpawnEditorBuildingTip(this.Draw, this._currentTiledValue, this.buildInfo.occupyArea, this._canNotBuild, this.buildInfo.buildType);
  147. }
  148. this._isMove = true;
  149. // GlobalD.GameControl._isBuildingMove = true;
  150. },
  151. // 自动移动,在ManagerControl 里面调用
  152. onAutoMove(delta) {
  153. this.isAutoMove = true;
  154. this._touchPosition.x += delta.x;
  155. this._touchPosition.y += delta.y;
  156. this._currentTiledValue = this._tileMap._tilePosFromLocation(this._touchPosition);
  157. //不相等的时候才绘制
  158. if (this._currentTiledValue.sub(this._EndPosition).mag() != 0) {
  159. this._EndPosition = this._currentTiledValue;
  160. let endPos = this._tileMap._getTheMiddleLocationFromtilePos(this._currentTiledValue);
  161. this.node.setPosition(endPos);
  162. let isHas = this._canNotBuild = GlobalD.game.areTheraOverlappingAreas(this._currentTiledValue, this.buildInfo.occupyArea, 1);
  163. // this.title.string = "(" + (this._currentTiledValue.x) + "," + (this._currentTiledValue.y) + ")" + isHas;
  164. if (task.virtualShadowPos)
  165. this._canNotBuild = this._EndPosition.sub(task.virtualShadowPos).mag() == 0 ? false : true;
  166. GlobalD.game.onSpawnEditorBuildingTip(this.Draw, this._currentTiledValue, this.buildInfo.occupyArea, this._canNotBuild, this.buildInfo.buildType);
  167. }
  168. },
  169. onCancleAutoMove() {
  170. this.isAutoMove = false;
  171. },
  172. /**
  173. * 判断是否可以被优先点击
  174. * @returns {*}
  175. */
  176. getOnClickTags: function () {
  177. if (this.getComponent("Collect") != null) {
  178. return this.getComponent("Collect").getOnClickTag();
  179. } else {
  180. return false;
  181. }
  182. }
  183. });