LeaseFarmlandInfo.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. import date from "../Unit/date.js"
  2. import gameToast from "../Network/gameToast"
  3. /**
  4. * 操作租赁土地的相关业务
  5. */
  6. cc.Class({
  7. extends: cc.Component,
  8. properties: {
  9. leasePanelPrefabs: {
  10. default: null,
  11. type: cc.Prefab,
  12. serializable: true,
  13. },
  14. /**
  15. * 显示天数部分
  16. */
  17. upSliderNode: {
  18. default: null,
  19. type: cc.Node,
  20. serializable: true,
  21. },
  22. multipleLabel: {
  23. default: null,
  24. type: cc.Label,
  25. serializable: true,
  26. toolTip: "显示的倍数"
  27. },
  28. leaseDate: {
  29. default: null,
  30. type: cc.Label,
  31. serializable: true,
  32. toolTip: "剩余天数/总天数"
  33. },
  34. /**
  35. * 成熟期部分
  36. */
  37. midSliderNode: {
  38. default: null,
  39. type: cc.Node,
  40. serializable: true,
  41. },
  42. midDate: {
  43. default: null,
  44. type: cc.Label,
  45. serializable: true,
  46. toolTip: "剩余天数/总天数"
  47. },
  48. midSliderProgressNode: {
  49. default: null,
  50. type: cc.Node,
  51. serializable: true,
  52. toolTip: "进度条"
  53. },
  54. notLeased: {
  55. default: null,
  56. type: cc.Node,
  57. serializable: true,
  58. },
  59. // configLandId: 1
  60. // createTime: "2022-01-10 21:25:30"
  61. // id: 1
  62. // isLease: 1
  63. // isPlant: 0
  64. // landDescribe: "土地1xxx"
  65. // leaseMultiple: 1
  66. // leaseTime: "2022-01-10 21:25:17"
  67. // name: "土地1"
  68. // plantMature: 0
  69. // plantStart: "2022-01-10 21:25:22"
  70. // updateTime: "2022-01-10 21:25:33"
  71. // userId: "4"
  72. // 已经租赁的土地信息
  73. leaseLandInfo: {
  74. default: null,
  75. visible: false
  76. },
  77. // createTime: "2022-01-10 21:25:30"
  78. // id: 4
  79. // initMultiple: 1
  80. // isInit: 1
  81. // isLease: 0
  82. // isPlant: 0
  83. // landDescribe: "一倍土地"
  84. // leaseMultiple: 1
  85. // leaseTime: "2022-01-10 21:25:17"
  86. // name: "土地4"
  87. // plantMature: 0
  88. // plantStart: "2022-01-10 21:25:22"
  89. // posX: 19
  90. // posY: 10
  91. // sizeX: 2
  92. // sizeY: 2
  93. // updateTime: "2022-01-10 21:25:33"
  94. // 未初始化时候的土地信息
  95. configLandInfo: {
  96. default: null,
  97. visible: false
  98. },
  99. showLandInfo: {
  100. default: null,
  101. type: cc.Node,
  102. serializable: true,
  103. toolTip: "显示土地信息"
  104. },
  105. landInfoPrefabs: {
  106. default: null,
  107. type: cc.Prefab,
  108. serializable: true,
  109. },
  110. // 已经种植的作物信息
  111. plantInfo: {
  112. default: null,
  113. visible: false
  114. },
  115. showPlantInfo: {
  116. default: null,
  117. type: cc.Node,
  118. serializable: true,
  119. toolTip: "显示种植信息"
  120. },
  121. plantInfoPrefabs: {
  122. default: null,
  123. type: cc.Prefab,
  124. serializable: true,
  125. },
  126. //收获阶段
  127. harvestNode: {
  128. default: null,
  129. type: cc.Node,
  130. serializable: true,
  131. },
  132. isHarvest: false
  133. },
  134. // LIFE-CYCLE CALLBACKS:
  135. // onLoad () {},
  136. start() {
  137. },
  138. onSpawnLeasePanel() {
  139. let leasePanel = cc.instantiate(this.leasePanelPrefabs);
  140. let parent = cc.find('Canvas/UICamera/DAPPContainer');
  141. leasePanel.parent = parent;
  142. leasePanel.setPosition(0, 0);
  143. // leasePanel.zIndex = 999;
  144. //把操作对象传入
  145. let leasePanelScript = leasePanel.getComponent("LeaseInfo");
  146. leasePanelScript.leaseFarmlandInfoNode = this;
  147. /**
  148. * 初始化两个操作。一个是续约,续约的话,不给操作,只有续约按钮可点,相当于原来配置再购买一次
  149. *
  150. * this.leaseLandInfo 如果购买了土地,这个参数不为空
  151. */
  152. if (this.leaseLandInfo) {
  153. //续约时候拦截输入,固定倍数
  154. leasePanelScript.blockInput.active = true;
  155. //传入当前计算倍数
  156. leasePanelScript.onInitLeaseInfo(this.leaseLandInfo.leaseMultiple);
  157. } else if (0 === this.configLandInfo.initMultiple) {
  158. // 固定土地开启,限制输入,设置默认值是1
  159. leasePanelScript.blockInput.active = true;
  160. leasePanelScript.onInitLeaseInfo(1);
  161. } else {
  162. //多倍土地,可以自己输入
  163. leasePanelScript.onInitLeaseInfo(this.configLandInfo.initMultiple);
  164. }
  165. },
  166. /**
  167. * 解锁土地操作
  168. */
  169. onUnlockLand() {
  170. //直接删除此节点
  171. if (this.notLeased) {
  172. this.notLeased.active = false;
  173. // this.notLeased.destroy();
  174. // this.notLeased = null;
  175. }
  176. },
  177. /**
  178. * 重新锁定
  179. */
  180. onLockLand() {
  181. //显示按钮
  182. if (this.notLeased) {
  183. this.notLeased.active = true;
  184. }
  185. //去除提示框
  186. this.upSliderNode.parent.active = false;
  187. this.midSliderNode.active = false;
  188. this.harvestNode.active = false;
  189. //重置土地图片
  190. let _workingBuilding = this.node.getComponent("WorkingBuilding");
  191. _workingBuilding.onSetGrow(0, null);
  192. },
  193. //设置config的土地信息
  194. setConfigLandInfo(value) {
  195. this.configLandInfo = value;
  196. },
  197. //设置已租赁参数
  198. setLeaseLandInfo(value) {
  199. this.leaseLandInfo = value;
  200. //更新一下土地信息
  201. this.updateLandState();
  202. },
  203. updateLandState() {
  204. if (!this.leaseLandInfo) return;
  205. // console.log("this.leaseLandInfo.1", this.leaseLandInfo)
  206. if (1 === this.leaseLandInfo.isLease) {
  207. // console.log("已租赁土地状态:", this.leaseLandInfo);
  208. //已解锁
  209. //显示剩余天数
  210. this.upSliderNode.parent.active = true;
  211. this.multipleLabel.string = this.leaseLandInfo.leaseMultiple;
  212. //计算天数
  213. // let allDay = date.datedifference(this.leaseLandInfo.createTime, this.leaseLandInfo.leaseTime);
  214. //租赁土地的剩余天数
  215. // let [_remainingLandDay, _remainingLandHour] = date.remainDayAndHour(this.leaseLandInfo.leaseTime, date.formatTime(new Date()));
  216. //todo 处理了
  217. this.leaseDate.string = "剩余" + this.leaseLandInfo.leaseDaysRemaining + "天数,共" + this.leaseLandInfo.leaseDays + "天";
  218. let sliderProgressScript = this.upSliderNode.getComponent("slider_progress");
  219. let _proccess = this.leaseLandInfo.leaseDays === 0 ? 0 : this.leaseLandInfo.leaseDaysRemaining / this.leaseLandInfo.leaseDays;
  220. sliderProgressScript.onSetProcgress(_proccess);
  221. //删除解锁图标
  222. this.onUnlockLand();
  223. /**
  224. * 绑定生成显示信息面板,现在土地信息
  225. */
  226. this.showLandInfo.on(cc.Node.EventType.TOUCH_START, () => {
  227. let leasePanel = cc.instantiate(this.landInfoPrefabs);
  228. let parent = cc.find('Canvas/UICamera/DAPPContainer');
  229. leasePanel.parent = parent;
  230. leasePanel.setPosition(0, 0);
  231. // leasePanel.zIndex = 999;
  232. let leasePanelScript = leasePanel.getComponent("LandInfo");
  233. //price,date,start,end,multiple,describe
  234. let _date = date.datedifference(this.leaseLandInfo.createTime, this.leaseLandInfo.leaseTime);
  235. let { rentalExpenses, createTime, leaseTime, leaseMultiple, landDescribe } = this.leaseLandInfo;
  236. leasePanelScript.setInfo(rentalExpenses, _date + "天", createTime, leaseTime, leaseMultiple + "倍", landDescribe);
  237. })
  238. //如果是种植状态并且有返回种植信息
  239. if (1 === this.leaseLandInfo.isPlant && this.leaseLandInfo.seedInfo) {
  240. //种植信息
  241. this.midSliderNode.active = true;
  242. // let _currentDay = date.dateAddDate(this.leaseLandInfo.plantStart, this.leaseLandInfo.seedInfo.maturity)
  243. // let [_remainingDay, _remainingHour] = date.remainDayAndHour(_currentDay, date.formatTime(new Date()));
  244. // let _hour = _remainingHour - parseInt(_remainingHour / 24) * 24;
  245. this.midDate.string = "剩余" + this.leaseLandInfo.plantDaysRemaining + "天" + this.leaseLandInfo.plantHoursRemaining + "小时";
  246. let _maturityAllHour = this.leaseLandInfo.seedInfo.maturity * 24
  247. let _remainAllHour = this.leaseLandInfo.plantDaysRemaining * 24 + this.leaseLandInfo.plantHoursRemaining;
  248. //计算剩余时间显示(进度条)
  249. let _midProccess = _maturityAllHour === 0 ? 0 : _remainAllHour / _maturityAllHour;
  250. this.midSliderProgressNode.getComponent(cc.ProgressBar).progress = _midProccess;
  251. /**
  252. * 绑定生成显示信息面板,种植信息
  253. */
  254. this.showPlantInfo.on(cc.Node.EventType.TOUCH_START, () => {
  255. let leasePanel = cc.instantiate(this.plantInfoPrefabs);
  256. let parent = cc.find('Canvas/UICamera/DAPPContainer');
  257. leasePanel.parent = parent;
  258. leasePanel.setPosition(0, 0);
  259. // leasePanel.zIndex = 999;
  260. let leasePanelScript = leasePanel.getComponent("PlantInfo");
  261. let _harvest = 0;
  262. if (this.leaseLandInfo.leaseDate === 1) {
  263. _harvest = this.leaseLandInfo.seedInfo.harvest1;
  264. } else if (this.leaseLandInfo.leaseDate === 2) {
  265. _harvest = this.leaseLandInfo.seedInfo.harvest2;
  266. } else if (this.leaseLandInfo.leaseDate === 3) {
  267. _harvest = this.leaseLandInfo.seedInfo.harvest3;
  268. }
  269. //price,maturity,plantStart,harvestQuantity,describe
  270. let { priceCnt, maturity, seedDescribe } = this.leaseLandInfo.seedInfo;
  271. leasePanelScript.setInfo(priceCnt, maturity + "天", this.leaseLandInfo.plantStart, _harvest, seedDescribe);
  272. })
  273. }
  274. }
  275. },
  276. /**
  277. * 场景有多个收获预制时候,用GameData限制
  278. * 点击之后,重置土地信息, 并且收入进仓库果实列表中去
  279. * 需要防多次触发
  280. */
  281. onShowHarvest() {
  282. this.harvestNode.active = true;
  283. this.harvestNode.on(cc.Node.EventType.TOUCH_START, () => {
  284. //限制点击多次
  285. // if (this.isHarvest) return;
  286. // this.isHarvest = true;
  287. if (GlobalD.GameData.isOnAddFruit) return;
  288. console.log("点击收获");
  289. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "收获中..", 5);
  290. let data = { landId: this.leaseLandInfo.configLandId };
  291. //果实收入仓库,重置土地种植信息
  292. GlobalD.GameData.onAddFruit(data, (res, value) => {
  293. // console.log(value);
  294. if (0 === value.code) {
  295. //收成后处理相关状态
  296. this.harvestNode.active = false;
  297. this.midSliderNode.active = false
  298. let _workingBuilding = this.node.getComponent("WorkingBuilding");
  299. _workingBuilding.onSetGrow(0, null);
  300. //更新仓库列表
  301. let BuildingView = cc.find("Canvas/UICamera/BuildingContainer/BuildingView").getComponent("BuildingView");
  302. BuildingView.onUpdateList();
  303. // "收获成功!"
  304. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "收获成功!", 1);
  305. } else if (706 === value.code) {
  306. //土地到期,没有更新刷新的处理刷新
  307. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), value.msg, 1);
  308. //重置锁定状态显示
  309. this.onLockLand();
  310. } else {
  311. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), value.msg, 1);
  312. }
  313. //处理信息后开放
  314. // this.isHarvest = false;
  315. });
  316. })
  317. },
  318. onPayCnt() {
  319. }
  320. });