LeaseFarmlandInfo.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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. * 成熟时候显示的图片
  135. */
  136. harvestSprite: {
  137. default: null,
  138. type: cc.SpriteFrame,
  139. },
  140. _workingBuilding: null,
  141. //时间计算对象
  142. timeInterval: null,
  143. //是否显示showHarvest
  144. isShowHarvest: false,
  145. isShowIndex: 0,
  146. //检查土地是否到期
  147. isCheckLandState: false,
  148. _buildingView: null
  149. },
  150. // LIFE-CYCLE CALLBACKS:
  151. onLoad() {
  152. this._workingBuilding = this.node.getComponent("WorkingBuilding");
  153. this._buildingView = cc.find("Canvas/UICamera/BuildingContainer/BuildingView").getComponent("BuildingView");
  154. },
  155. // start() {
  156. // },
  157. onSpawnLeasePanel() {
  158. let leasePanel = cc.instantiate(this.leasePanelPrefabs);
  159. let parent = cc.find('Canvas/UICamera/DAPPContainer');
  160. leasePanel.parent = parent;
  161. leasePanel.setPosition(0, 0);
  162. // leasePanel.zIndex = 999;
  163. //把操作对象传入
  164. let leasePanelScript = leasePanel.getComponent("LeaseInfo");
  165. leasePanelScript.leaseFarmlandInfoNode = this;
  166. /**
  167. * 初始化两个操作。一个是续约,续约的话,不给操作,只有续约按钮可点,相当于原来配置再购买一次
  168. *
  169. * this.leaseLandInfo 如果购买了土地,这个参数不为空
  170. */
  171. if (this.leaseLandInfo) {
  172. //续约时候拦截输入,固定倍数
  173. leasePanelScript.blockInput.active = true;
  174. //传入当前计算倍数
  175. leasePanelScript.onInitLeaseInfo(this.leaseLandInfo.leaseMultiple);
  176. } else if (0 === this.configLandInfo.initMultiple) {
  177. // 固定土地开启,限制输入,设置默认值是1
  178. leasePanelScript.blockInput.active = true;
  179. leasePanelScript.onInitLeaseInfo(1);
  180. } else {
  181. //多倍土地,可以自己输入
  182. leasePanelScript.onInitLeaseInfo(this.configLandInfo.initMultiple);
  183. }
  184. },
  185. /**
  186. * 解锁土地操作
  187. */
  188. onUnlockLand() {
  189. //直接删除此节点
  190. if (this.notLeased) {
  191. this.notLeased.active = false;
  192. // this.notLeased.destroy();
  193. // this.notLeased = null;
  194. }
  195. },
  196. /**
  197. * 重新锁定
  198. */
  199. onLockLand() {
  200. //显示按钮
  201. if (this.notLeased) {
  202. this.notLeased.active = true;
  203. }
  204. //清除当前租赁数据
  205. this.leaseLandInfo = null;
  206. this.isShowHarvest = false;
  207. this.isShowIndex = 0;
  208. if (this.timeInterval) {
  209. clearInterval(this.timeInterval);
  210. this.timeInterval = null;
  211. }
  212. //去除提示框
  213. this.upSliderNode.parent.active = false;
  214. this.midSliderNode.active = false;
  215. this.harvestNode.active = false;
  216. //重置土地图片
  217. this._workingBuilding.onSetGrow(0, null);
  218. },
  219. //设置config的土地信息
  220. setConfigLandInfo(value) {
  221. this.configLandInfo = value;
  222. },
  223. //设置已租赁参数
  224. setLeaseLandInfo(value) {
  225. if (!value) {
  226. console.log("已租赁土地状态为空:", value);
  227. return;
  228. }
  229. //设置意思显示相关
  230. this.isShowHarvest = false;
  231. this.isShowIndex = 0;
  232. //重新设置租赁检测状态
  233. this.isCheckLandState = false;
  234. this.leaseLandInfo = value;
  235. //先解绑一下信息
  236. this.showLandInfo.off(cc.Node.EventType.TOUCH_START, this._showLandInfo, this);
  237. this.showPlantInfo.off(cc.Node.EventType.TOUCH_START, this._showPlantInfo, this);
  238. //更新一下土地信息
  239. this.updateLandState();
  240. },
  241. updateLandState() {
  242. //已解锁
  243. //显示剩余天数
  244. this.upSliderNode.parent.active = true;
  245. this.multipleLabel.string = this.leaseLandInfo.leaseMultiple;
  246. //租赁土地的剩余天数
  247. //获得服务器租赁日期毫秒
  248. // let [_lDay, _lHour, _lMinutes, _lSeconds] = date.remainFromMillisecond(this.leaseLandInfo.leaseDaysMill);
  249. // //todo 处理了 this.leaseLandInfo.leaseDaysRemaining
  250. // if (0 !== _lDay) {
  251. // this.leaseDate.string = "剩余" + _lDay + "天数,共" + this.leaseLandInfo.leaseDays + "天";
  252. // } else if (0 !== _lHour) {
  253. // this.leaseDate.string = "剩余" + _lHour + "小时,共" + this.leaseLandInfo.leaseDays + "天";
  254. // } else if (0 !== _lMinutes) {
  255. // this.leaseDate.string = "剩余" + _lMinutes + "分钟,共" + this.leaseLandInfo.leaseDays + "天";
  256. // } else if (0 !== _lSeconds) {
  257. // this.leaseDate.string = "剩余" + _lMinutes + "秒,共" + this.leaseLandInfo.leaseDays + "天";
  258. // }
  259. // 进去条按天算
  260. let sliderProgressScript = this.upSliderNode.getComponent("slider_progress");
  261. let _proccess = this.leaseLandInfo.leaseDays === 0 ? 0 : this.leaseLandInfo.leaseDaysRemaining / this.leaseLandInfo.leaseDays;
  262. sliderProgressScript.onSetProcgress(_proccess);
  263. //删除解锁图标
  264. this.onUnlockLand();
  265. /**
  266. * 绑定生成显示信息面板,现在土地信息
  267. */
  268. this.showLandInfo.on(cc.Node.EventType.TOUCH_START, this._showLandInfo, this);
  269. //如果是种植状态并且有返回种植信息
  270. if (1 === this.leaseLandInfo.isPlant && this.leaseLandInfo.seedInfo) {
  271. //种植信息
  272. //如果土地是租赁转态并且同时种植了 ,这里处理土地状态,显示种植信息
  273. let _spriteFrame;
  274. switch (this.leaseLandInfo.seedInfo.picture) {
  275. case "Cabbage":
  276. _spriteFrame = this._buildingView.fruitSpriteFrame[0];
  277. break;
  278. case "Potato":
  279. _spriteFrame = this._buildingView.fruitSpriteFrame[1];
  280. break;
  281. case "Carrot":
  282. _spriteFrame = this._buildingView.fruitSpriteFrame[2];
  283. break;
  284. case "Broccoli":
  285. _spriteFrame = this._buildingView.fruitSpriteFrame[3];
  286. break;
  287. case "Tomato":
  288. _spriteFrame = this._buildingView.fruitSpriteFrame[4];
  289. break;
  290. case "Squash":
  291. _spriteFrame = this._buildingView.fruitSpriteFrame[5];
  292. break;
  293. case "Eggplant":
  294. _spriteFrame = this._buildingView.fruitSpriteFrame[6];
  295. break;
  296. case "Pepper":
  297. _spriteFrame = this._buildingView.fruitSpriteFrame[7];
  298. break;
  299. case "Lentil":
  300. _spriteFrame = this._buildingView.fruitSpriteFrame[8];
  301. break;
  302. default:
  303. break;
  304. }
  305. //设置一个收获时候的图片
  306. this.harvestSprite = _spriteFrame;
  307. this.midSliderNode.active = true;
  308. // //获得服务器种植日期毫秒
  309. // let [_pDay, _pHour, _pMinutes, _pSeconds] = date.remainFromMillisecond(this.leaseLandInfo.plantDaysMill);
  310. // //todo 处理了 this.leaseLandInfo.plantDaysRemaining
  311. // if (0 === _pDay && 0 === _pHour) {
  312. // //如果种植天数和种植小时为0,显示分钟和秒
  313. // this.midDate.string = "剩余" + _pMinutes + "分钟" + _pSeconds + "秒";
  314. // } else {
  315. // //否则显示天和小时
  316. // this.midDate.string = "剩余" + _pDay + "天" + _pHour + "小时";
  317. // }
  318. // 进度条按小时计算
  319. let _maturityAllHour = this.leaseLandInfo.seedInfo.maturity * 24
  320. let _remainAllHour = this.leaseLandInfo.plantDaysRemaining * 24 + this.leaseLandInfo.plantHoursRemaining;
  321. //计算剩余时间显示(进度条)
  322. let _midProccess = _maturityAllHour === 0 ? 0 : _remainAllHour / _maturityAllHour;
  323. this.midSliderProgressNode.getComponent(cc.ProgressBar).progress = _midProccess;
  324. /**
  325. * 绑定生成显示信息面板,种植信息
  326. */
  327. this.showPlantInfo.on(cc.Node.EventType.TOUCH_START, this._showPlantInfo, this);
  328. }
  329. //每个作物直接实时计算时间
  330. if (this.timeInterval) {
  331. clearInterval(this.timeInterval);
  332. this.timeInterval = null;
  333. }
  334. let lastUpdateTime = Date.now();
  335. let elapsedTime = 0;
  336. let updateFrequency = 1; //ms
  337. this.timeInterval = setInterval(() => {
  338. let currentTime = Date.now();
  339. let deltaTime = currentTime - lastUpdateTime;
  340. elapsedTime += deltaTime;
  341. if (elapsedTime >= updateFrequency) {
  342. elapsedTime -= updateFrequency;
  343. let _leaseDaysMill = this.leaseLandInfo.leaseDaysMill - elapsedTime;
  344. let _plantDaysMill = this.leaseLandInfo.plantDaysMill - elapsedTime;
  345. lastUpdateTime = currentTime;
  346. // 更新计时显示
  347. let [_lDay, _lHour, _lMinutes, _lSeconds] = date.remainFromMillisecond(_leaseDaysMill);
  348. //todo 处理了 this.leaseLandInfo.leaseDaysRemaining
  349. if (0 !== _lDay) {
  350. this.leaseDate.string = "剩余" + _lDay + "天数,共" + this.leaseLandInfo.leaseDays + "天";
  351. } else if (0 !== _lHour) {
  352. this.leaseDate.string = "剩余" + _lHour + "小时,共" + this.leaseLandInfo.leaseDays + "天";
  353. } else if (0 !== _lMinutes) {
  354. this.leaseDate.string = "剩余" + _lMinutes + "分钟,共" + this.leaseLandInfo.leaseDays + "天";
  355. } else {
  356. this.leaseDate.string = "剩余" + _lSeconds + "秒,共" + this.leaseLandInfo.leaseDays + "天";
  357. }
  358. //todo 如果租赁时间到了,重新锁定
  359. //重置锁定状态显示
  360. if (!this.isCheckLandState) {
  361. if (_leaseDaysMill <= 0) {
  362. this.isCheckLandState = true;
  363. //检查一次土地
  364. this.onCheckLand(this.leaseLandInfo.configLandId);
  365. }
  366. }
  367. if (1 === this.leaseLandInfo.isPlant && this.leaseLandInfo.seedInfo) {
  368. //获得服务器种植日期毫秒
  369. let [_pDay, _pHour, _pMinutes, _pSeconds] = date.remainFromMillisecond(_plantDaysMill);
  370. //todo 处理了 this.leaseLandInfo.plantDaysRemaining
  371. if (0 !== _pDay) {
  372. //否则显示天和小时
  373. this.midDate.string = "剩余" + _pDay + "天" + _pHour + "小时";
  374. } else if (0 !== _pHour) {
  375. this.midDate.string = "剩余" + _pHour + "小时" + _pMinutes + "分钟";
  376. } else {
  377. this.midDate.string = "剩余" + _pMinutes + "分钟" + _pSeconds + "秒";
  378. }
  379. if (!this.isShowHarvest) {
  380. if (_plantDaysMill <= 0) {
  381. this.isShowHarvest = true;
  382. this.isShowIndex = 3;
  383. //如果传回的毫秒是小于零,说明成熟了
  384. //成熟阶段
  385. this._workingBuilding.onSetGrow(3, this.harvestSprite);
  386. //todo,如果时间小的,走收获,收获成果实
  387. // console.log("土地:", _userLeaseLand[j].configLandId, "已经成熟,可以操作收取果实了!!");
  388. //todo 土地可收获状态
  389. this.onShowHarvest();
  390. } else {
  391. let _maturityHour = this.leaseLandInfo.seedInfo.maturity * 24
  392. let _ratio = (_pDay * 24 + _pHour) / _maturityHour;
  393. if (_ratio > 0.5 && 1 !== this.isShowIndex) {
  394. this.isShowIndex = 1;
  395. this._workingBuilding.onSetGrow(1, this.harvestSprite);
  396. console.log("_ratio:" + _ratio + "=" + this.leaseLandInfo.configLandId + ",状态:" + this.isShowIndex);
  397. } else if (_ratio <= 0.5 && _ratio >= 0 && 2 !== this.isShowIndex) {
  398. this.isShowIndex = 2;
  399. this._workingBuilding.onSetGrow(2, this.harvestSprite);
  400. console.log("_ratio:" + _ratio + "=" + this.leaseLandInfo.configLandId + ",状态:" + this.isShowIndex);
  401. }
  402. }
  403. }
  404. }
  405. }
  406. });
  407. },
  408. _showLandInfo() {
  409. let leasePanel = cc.instantiate(this.landInfoPrefabs);
  410. let parent = cc.find('Canvas/UICamera/DAPPContainer');
  411. leasePanel.parent = parent;
  412. leasePanel.setPosition(0, 0);
  413. let leasePanelScript = leasePanel.getComponent("LandInfo");
  414. let _date = date.datedifference(this.leaseLandInfo.createTime, this.leaseLandInfo.leaseTime);
  415. let { configLandId, rentalExpenses, createTime, leaseTime, leaseMultiple, landDescribe } = this.leaseLandInfo;
  416. leasePanelScript.setInfo(configLandId, rentalExpenses, _date + "天", createTime, leaseTime, leaseMultiple + "倍", landDescribe);
  417. },
  418. _showPlantInfo() {
  419. let leasePanel = cc.instantiate(this.plantInfoPrefabs);
  420. let parent = cc.find('Canvas/UICamera/DAPPContainer');
  421. leasePanel.parent = parent;
  422. leasePanel.setPosition(0, 0);
  423. // leasePanel.zIndex = 999;
  424. let leasePanelScript = leasePanel.getComponent("PlantInfo");
  425. let _harvest = 0;
  426. if (this.leaseLandInfo.leaseDate === 1) {
  427. _harvest = this.leaseLandInfo.seedInfo.harvest1;
  428. } else if (this.leaseLandInfo.leaseDate === 2) {
  429. _harvest = this.leaseLandInfo.seedInfo.harvest2;
  430. } else if (this.leaseLandInfo.leaseDate === 3) {
  431. _harvest = this.leaseLandInfo.seedInfo.harvest3;
  432. }
  433. //price,maturity,plantStart,harvestQuantity,describe
  434. let { name, priceCnt, maturity, seedDescribe } = this.leaseLandInfo.seedInfo;
  435. leasePanelScript.setInfo(name, priceCnt, maturity + "天", this.leaseLandInfo.plantStart, _harvest, seedDescribe);
  436. },
  437. //更新收获的状态
  438. updateHarvest() {
  439. //todo 后续看看如何刷新,现在走的是开始刷新一次
  440. // let _workingBuilding = housingTemp_farmland.getComponent("WorkingBuilding");
  441. // //获得服务器种植日期毫秒
  442. // let [_day, _hour, _minutes, _seconds] = date.remainFromMillisecond(_leaseLandInfo.plantDaysMill);
  443. // let _maturityHour = _leaseLandInfo.seedInfo.maturity * 24
  444. // let _ratio = _hour / _maturityHour;
  445. // // console.log("生成的:" + _ratio);
  446. // if (_ratio > 0.5) {
  447. // _workingBuilding.onSetGrow(1, _spriteFrame);
  448. // } else if (_ratio >= 0) {
  449. // _workingBuilding.onSetGrow(2, _spriteFrame);
  450. // }else if (_leaseLandInfo.plantDaysMill <= 0) {
  451. // //如果传回的毫秒是小于零,说明成熟了
  452. // //成熟阶段
  453. // _workingBuilding.onSetGrow(3, _spriteFrame);
  454. // //todo,如果时间小的,走收获,收获成果实
  455. // // console.log("土地:", _userLeaseLand[j].configLandId, "已经成熟,可以操作收取果实了!!");
  456. // //todo 土地可收获状态
  457. // leaseFarmlandInfoScript.onShowHarvest();
  458. // }
  459. },
  460. //检查土地状态
  461. onCheckLand(configLandId) {
  462. GlobalD.GameData.getLandState({
  463. landId: configLandId,
  464. callback: (res, value) => {
  465. console.log("检查土地数据:" + JSON.stringify(value.msg));
  466. //这里只判断土地是否过期
  467. if (706 === value.code) {
  468. //土地到期,没有更新刷新的处理刷新
  469. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), configLandId + "号," + value.msg, 2);
  470. //重置锁定状态显示
  471. this.onLockLand();
  472. }
  473. }
  474. });
  475. },
  476. /**
  477. * 场景有多个收获预制时候,用GameData限制
  478. * 点击之后,重置土地信息, 并且收入进仓库果实列表中去
  479. * 需要防多次触发
  480. */
  481. onShowHarvest() {
  482. this.harvestNode.active = true;
  483. this.harvestNode.on(cc.Node.EventType.TOUCH_START, () => {
  484. //限制点击多次
  485. // if (this.isHarvest) return;
  486. // this.isHarvest = true;
  487. if (GlobalD.GameData.isOnAddFruit) return;
  488. console.log("点击收获");
  489. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "收获中..", 5);
  490. let data = { landId: this.leaseLandInfo.configLandId };
  491. //果实收入仓库,重置土地种植信息
  492. GlobalD.GameData.onAddFruit(data, (res, value) => {
  493. // console.log(value);
  494. if (0 === value.code) {
  495. //收成后处理相关状态
  496. this.harvestNode.active = false;
  497. this.midSliderNode.active = false
  498. this._workingBuilding.onSetGrow(0, null);
  499. //更新仓库列表
  500. this._buildingView.onUpdateList();
  501. // "收获成功!"
  502. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "收获成功!", 1);
  503. this.isShowHarvest = false;
  504. this.isShowIndex = 0;
  505. if (this.timeInterval) {
  506. clearInterval(this.timeInterval);
  507. this.timeInterval = null;
  508. }
  509. } else if (706 === value.code) {
  510. //土地到期,没有更新刷新的处理刷新
  511. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), value.msg, 1);
  512. //重置锁定状态显示
  513. this.onLockLand();
  514. } else {
  515. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), value.msg, 1);
  516. }
  517. //处理信息后开放
  518. // this.isHarvest = false;
  519. });
  520. })
  521. },
  522. onDestroy() {
  523. if (this.timeInterval) {
  524. clearInterval(this.timeInterval);
  525. this.timeInterval = null;
  526. }
  527. }
  528. });