FarmCountDown.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import { BitmapFontC } from "../../game/component/BitmapFontC";
  2. import { ProductType } from "../../game/data/GameData";
  3. import Util from "../util/Util";
  4. const { ccclass, property } = cc._decorator;
  5. @ccclass
  6. export class FarmCountDown extends cc.Component {
  7. @property({ type: cc.Component.EventHandler, tooltip: "成熟回调" }) onRiped: cc.Component.EventHandler = null;
  8. @property({ type: cc.Component.EventHandler, tooltip: "生虫回调" }) onSick: cc.Component.EventHandler = null;
  9. @property({ type: cc.Component.EventHandler, tooltip: "加速按钮回调" }) onSpeedButton: cc.Component.EventHandler = null;
  10. @property({ type: cc.Node, tooltip: "常驻计时器组" }) normalGroup: cc.Node = null;
  11. @property({ type: cc.Sprite, tooltip: "常驻进度条" }) normalProgress: cc.Sprite = null;
  12. @property({ type: BitmapFontC, tooltip: "常驻计时" }) normalCount: BitmapFontC = null;
  13. @property({ type: cc.Node, tooltip: "可收获图标" }) ripedIcon: cc.Node = null;
  14. @property({ type: cc.Node, tooltip: "生病图标" }) sickIcon: cc.Node = null;
  15. @property({ type: cc.Sprite, tooltip: "生病图标" }) clockIcon: cc.Sprite = null;
  16. private counting = false;
  17. /**成熟时间戳 */
  18. private ripeTime = 0;
  19. /**总时长 */
  20. private totleTime = 0;
  21. private isDataed = false;
  22. private isRipeInit = false;
  23. private productConfig = null;
  24. public async setData(data) {
  25. if (!this.isDataed) {
  26. this.isDataed = true;
  27. this.productConfig = gData.gameData.getProductMap(data.productID);
  28. this.ripeTime = data.growSpan;
  29. this.totleTime = this.productConfig.time;
  30. this.normalGroup.active = true;
  31. this.sickIcon.active = false;
  32. this.counting = true;
  33. this.normalCount.string = '';
  34. this.normalProgress.fillRange = 0;
  35. if (this.clockIcon) {
  36. let plantPath = 'game/coregame/texture/factory_icons/factory_';
  37. this.clockIcon.spriteFrame = await mk.loader.load(plantPath + this.productConfig.picture, cc.SpriteFrame);
  38. }
  39. }
  40. }
  41. update() {
  42. let now = Date.now();
  43. if (this.counting && now < this.ripeTime) {
  44. let timeString = Util.ParseTime2Format(Math.floor((this.ripeTime - now) * 0.001), 'm:s');
  45. this.normalCount.string = timeString;
  46. this.normalProgress.fillRange = 1 - ((this.ripeTime - now) / (this.totleTime * 1000));
  47. } else if (this.counting && now > this.ripeTime) {
  48. if (!this.isRipeInit) {
  49. this.isRipeInit = true;
  50. this.riped();
  51. }
  52. }
  53. }
  54. public cleanRiped(): void {
  55. this.isRipeInit = false;
  56. this.isDataed = false;
  57. this.ripedIcon.active = false;
  58. this.sickIcon.active = false;
  59. }
  60. public async onSpeed() {
  61. //通信 暂时注释
  62. // if (g.userData.diamond >= 25) {
  63. // let result = await this.http.send("/api/product/quickProduct", { buildID: this.buildID });
  64. // if (result.code == 0) {
  65. // platform.reportThinking("diamond_decrease", JSON.stringify({ previous_number: g.userData.diamond, decrease_number: result.data, current_number: g.userData.diamond - result.data, reasons: "speed" }));
  66. // g.userData.diamond -= result.data;
  67. // this.riped();
  68. // this.selectGroup.active = false;
  69. // this.onSpeedButton && this.onSpeedButton.emit(null);
  70. // return;
  71. // }
  72. // }
  73. // WindowManager.showTips("钻石不足");
  74. }
  75. /** 设置显示
  76. * @param state 0 生虫 1 成熟
  77. */
  78. async setState(state) {
  79. this.normalProgress.fillRange = 0;
  80. this.normalCount.string = '';
  81. this.normalGroup.active = false;
  82. if (state == 0) {
  83. this.sickIcon.active = true;
  84. this.ripedIcon.active = false;
  85. }
  86. else if (state == 1) {
  87. this.sickIcon.active = false;
  88. this.ripedIcon.active = true;
  89. }
  90. }
  91. public riped(sendToServer = true): void {
  92. this.counting = false;
  93. this.normalGroup.active = false;
  94. let sick = false;
  95. if (gData.gameData.playerProp.completeFarmTaskTimes >= 1 && Math.random() < parseFloat(gData.gameData.configs.ServerConfig.StopProduction)) {
  96. if (this.productConfig.tab == ProductType.nzw) {
  97. if (gData.gameData.RawInsectCurArr[0] < parseInt(gData.gameData.RawInsectArr[0])) {
  98. sick = true;
  99. gData.gameData.RawInsectCurArr[0]++;
  100. }
  101. }
  102. else if (this.productConfig.tab == ProductType.dw) {
  103. if (gData.gameData.RawInsectCurArr[1] < parseInt(gData.gameData.RawInsectArr[1])) {
  104. sick = true;
  105. gData.gameData.RawInsectCurArr[1]++;
  106. }
  107. }
  108. else if (gData.gameData.RawInsectCurArr[2] < parseInt(gData.gameData.RawInsectArr[2])) {
  109. sick = true;
  110. gData.gameData.RawInsectCurArr[2]++;
  111. }
  112. }
  113. if (sick) {
  114. this.sickIcon.active = true;
  115. this.onSick && this.onSick.emit([sendToServer]);
  116. }
  117. else {
  118. this.ripedIcon.active = true;
  119. this.onRiped && this.onRiped.emit([sendToServer]);
  120. }
  121. }
  122. }