LeaseFarmlandInfo.js 21 KB

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