ManageSeedNode.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. var reGameStates = require('GameStates');
  2. import gameToast from "../Network/gameToast";
  3. // const { values } = require('../Network/runtime');
  4. cc.Class({
  5. extends: cc.Component,
  6. properties: {
  7. seedType: {
  8. default: reGameStates.SeedType.Normal,
  9. type: cc.Enum(reGameStates.SeedType),
  10. },
  11. seedName: {
  12. default: ''
  13. },
  14. _EndPosition: {
  15. default: new cc.Vec2(),
  16. serializable: false,
  17. visible: false,
  18. },
  19. labelTip: {
  20. default: null,
  21. type: cc.Label,
  22. },
  23. labelTipString: {
  24. default: '移动可拆除对应建筑',
  25. },
  26. //记录节点
  27. targetBuildingsInfo: { default: null, visible: false },
  28. //移除的提示预制
  29. removeTipPrefab: { default: null, type: cc.Prefab },
  30. //记录要删除的节点
  31. selectedTarget: { default: [], type: cc.Node, visible: false },
  32. //记录选中的index
  33. selectedTargetIndex: { default: [], type: cc.Integer, visible: false },
  34. isMoveState: { default: true, visible: false },
  35. MoveStartNode: cc.Node,
  36. MoveEndNode: cc.Node,
  37. //content_seed 传递过来
  38. goodsSeedInfo: {
  39. default: null,
  40. tooltip: '设定一个背包种子信息',
  41. },
  42. //content_seed 设置
  43. seedSprite: {
  44. default: null,
  45. type: cc.Sprite,
  46. tooltip: "当前生出工具的提示"
  47. },
  48. fruitSpriteFrame: {
  49. default: null,
  50. type: cc.SpriteFrame,
  51. tooltip: "成熟的图片展示"
  52. },
  53. upTarget: {
  54. default: null,
  55. type: cc.Node,
  56. tooltip: "把当前操作的ui按钮传进来"
  57. },
  58. mallType: {
  59. default: 0,
  60. tooltip: "类型,默认0是种子"
  61. }
  62. },
  63. // LIFE-CYCLE CALLBACKS:
  64. // onLoad () {},
  65. start() {
  66. let self = this;
  67. GlobalD.game._ManageUIScript.onBottomMenuView(false);
  68. //初始化时候设置一下位置
  69. let startTiledPos = GlobalD.TiledMap._tilePosFromLocation(self.node.getPosition());
  70. let startEndPos = GlobalD.TiledMap._getTheMiddleLocationFromtilePos(startTiledPos);
  71. self.node.setPosition(startEndPos);
  72. self._EndPosition = startTiledPos;
  73. if(0 === self.mallType){
  74. let startIndex = GlobalD.TiledMap.getIndex(startTiledPos);
  75. let _targetBuildingsInfo = GlobalD.game.onGetBuildingsTiledMapUnitFromIndex(startIndex);
  76. if (_targetBuildingsInfo) {
  77. self.labelTip.string = '播种到' + _targetBuildingsInfo.buildInfo.id + "号" + _targetBuildingsInfo.buildInfo.buildingName + '上';
  78. self.targetBuildingsInfo = _targetBuildingsInfo;
  79. }
  80. else {
  81. self.labelTip.string = "请移动到农田上!";
  82. self.targetBuildingsInfo = null;
  83. }
  84. }else{
  85. let startIndex = GlobalD.TiledMap.getIndex(startTiledPos);
  86. let _targetBuildingsInfo = GlobalD.game.onGetBuildingsTiledMapUnitFromIndex(startIndex);
  87. if (_targetBuildingsInfo) {
  88. self.labelTip.string = "养殖在" + _targetBuildingsInfo.buildInfo.buildingName + '上';
  89. self.targetBuildingsInfo = _targetBuildingsInfo;
  90. }
  91. else {
  92. self.labelTip.string = "请移动到对应的养殖场上!";
  93. self.targetBuildingsInfo = null;
  94. }
  95. }
  96. self.node.on(cc.Node.EventType.TOUCH_MOVE, function (event) {
  97. // cc.log( "isUICameraMove:", GlobalD.GameControl.isUICameraMove);
  98. GlobalD.GameControl._isBuildingMove = true;
  99. GlobalD.GameControl.isUICameraMove = false;
  100. var delta = event.touch.getDelta();
  101. self._touchPosition.x += delta.x / GlobalD.game.MainCamera.zoomRatio;
  102. self._touchPosition.y += delta.y / GlobalD.game.MainCamera.zoomRatio;
  103. let tiledPos = GlobalD.TiledMap._tilePosFromLocation(self._touchPosition);
  104. // //不相等的时候才绘制
  105. if (tiledPos.sub(self._EndPosition).mag() != 0) {
  106. self._EndPosition = tiledPos;
  107. self._SelectHighwayTiled(tiledPos);
  108. }
  109. }, self.node);
  110. self.node.on(cc.Node.EventType.TOUCH_START, function (event) {
  111. // console.log("self.TOUCH_START:", GlobalD.GameControl.isUICameraMove);
  112. self.isMoving = true;
  113. self._touchPosition = self.node.getPosition();
  114. GlobalD.GameControl._isBuildingMove = false;
  115. GlobalD.GameControl.isUICameraMove = false;
  116. }, self.node);
  117. self.node.on(cc.Node.EventType.TOUCH_END, function (event) {
  118. // console.log("self._EndPosition:", self._EndPosition);
  119. //起始触摸位置
  120. GlobalD.GameControl._isBuildingMove = false;
  121. GlobalD.GameControl.isUICameraMove = false;
  122. let buildIndex = GlobalD.TiledMap.getIndex(self._EndPosition);
  123. let _targetBuildingsInfo = GlobalD.game.onGetBuildingsTiledMapUnitFromIndex(buildIndex);
  124. if (_targetBuildingsInfo) {
  125. if(0 === self.mallType){
  126. if (_targetBuildingsInfo.buildInfo.buildType == reGameStates.BuildType.HolyFarmland) {
  127. self.labelTip.string = '播种到' + _targetBuildingsInfo.buildInfo.id + "号" + _targetBuildingsInfo.buildInfo.buildingName + '上';
  128. self.targetBuildingsInfo = _targetBuildingsInfo;
  129. } else {
  130. self.labelTip.string = '请移动到农田上!';
  131. self.targetBuildingsInfo = null;
  132. }
  133. }else{
  134. if (_targetBuildingsInfo.buildInfo.buildType == reGameStates.BuildType.HolyAnimal) {
  135. self.labelTip.string = "养殖在" + _targetBuildingsInfo.buildInfo.buildingName + '上';
  136. self.targetBuildingsInfo = _targetBuildingsInfo;
  137. } else {
  138. self.labelTip.string = '请移动到对应的养殖场上!';
  139. self.targetBuildingsInfo = null;
  140. }
  141. }
  142. }
  143. else {
  144. self.labelTip.string = self.labelTipString;
  145. self.targetBuildingsInfo = null;
  146. }
  147. }, self.node);
  148. self.node.on(cc.Node.EventType.TOUCH_CANCEL, function (event) {
  149. //起始触摸位置
  150. GlobalD.GameControl._isBuildingMove = false;
  151. GlobalD.GameControl.isUICameraMove = false;
  152. // console.log("self.TOUCH_CANCEL:");
  153. }, self.node);
  154. },
  155. onCancleSelf() {
  156. //只要是编辑状态下。设为true
  157. GlobalD.GameControl._isBuildingCanEdit = false;
  158. GlobalD.game._ManageUIScript.onBottomMenuView(true);
  159. this.node.destroy();
  160. },
  161. /**
  162. *
  163. * 处理播种逻辑,记录数据库
  164. * @returns
  165. */
  166. onSowOnTheGround() {
  167. //调用土地相关
  168. if (!this.targetBuildingsInfo) return;
  169. // 种植土地的信息
  170. let _leaselandInfoScript = null;
  171. if (0 === this.mallType) {
  172. // 种植土地的信息
  173. _leaselandInfoScript = this.targetBuildingsInfo.getComponent("LeaseFarmlandInfo");
  174. } else {
  175. //养殖场的信息
  176. _leaselandInfoScript = this.targetBuildingsInfo.getComponent("LeaseAnimalInfo");
  177. }
  178. console.log("_leaselandInfoScript:",_leaselandInfoScript);
  179. if(!_leaselandInfoScript){
  180. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "种子(幼苗)类型不对!", 2);
  181. return;
  182. }
  183. let leaseLandInfo = _leaselandInfoScript.leaseLandInfo;
  184. if (leaseLandInfo == null) {
  185. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), 0 === this.mallType ? "请先租赁土地!" : "请先租赁养殖场", 2);
  186. //隐藏种子和显示UI
  187. this.node.active = false;
  188. GlobalD.game._ManageUIScript.onBottomMenuView(true);
  189. return;
  190. }
  191. let data = { landId: leaseLandInfo.configLandId, seedId: this.goodsSeedInfo.id, amount: 1 };
  192. GlobalD.GameData.onPlant(data, (value) => {
  193. //面板种子消耗减少1
  194. if (0 === value.code) {
  195. console.log('种子减少的类型名字', this.seedName);
  196. console.log(value);
  197. var BuildingView = cc.find("Canvas/UICamera/BuildingContainer/BuildingView").getComponent("BuildingView");
  198. BuildingView.onUpdateList();
  199. //把种子的信息存储到当前的土地 plantInfo 上;
  200. _leaselandInfoScript.setLeaseLandInfo(value.data, true);
  201. //刷新当前灾难ui
  202. GlobalD.game._ManageUIScript.onShowDisater();
  203. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "已成功种植!", 1);
  204. } else if (706 === value.code) {
  205. //土地到期,没有更新刷新的处理刷新
  206. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), value.msg, 1);
  207. //重置锁定状态显示
  208. _leaselandInfoScript.onLockLand();
  209. } else {
  210. //土地已经种植了
  211. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), value.msg, 1);
  212. }
  213. });
  214. //隐藏种子和显示UI
  215. this.node.active = false;
  216. GlobalD.game._ManageUIScript.onBottomMenuView(true);
  217. },
  218. _SelectHighwayTiled(tiledPos) {
  219. let endPos = GlobalD.TiledMap._getTheMiddleLocationFromtilePos(tiledPos);
  220. this.node.setPosition(endPos);
  221. }
  222. });