FarmCountDown.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import { BitmapFontC } from "../../game/component/BitmapFontC";
  2. import { GameProp, 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. console.log('onRiped start');
  51. this.riped();
  52. }
  53. }
  54. }
  55. public cleanRiped(): void {
  56. this.isRipeInit = false;
  57. this.isDataed = false;
  58. this.ripedIcon.active = false;
  59. this.sickIcon.active = false;
  60. }
  61. public async onSpeed() {
  62. //通信 暂时注释
  63. // if (g.userData.diamond >= 25) {
  64. // let result = await this.http.send("/api/product/quickProduct", { buildID: this.buildID });
  65. // if (result.code == 0) {
  66. // platform.reportThinking("diamond_decrease", JSON.stringify({ previous_number: g.userData.diamond, decrease_number: result.data, current_number: g.userData.diamond - result.data, reasons: "speed" }));
  67. // g.userData.diamond -= result.data;
  68. // this.riped();
  69. // this.selectGroup.active = false;
  70. // this.onSpeedButton && this.onSpeedButton.emit(null);
  71. // return;
  72. // }
  73. // }
  74. // WindowManager.showTips("钻石不足");
  75. }
  76. /** 设置显示
  77. * @param state 0 生虫 1 成熟
  78. */
  79. async setState(state) {
  80. this.normalProgress.fillRange = 0;
  81. this.normalCount.string = '';
  82. this.normalGroup.active = false;
  83. if (state == 0) {
  84. this.sickIcon.active = true;
  85. this.ripedIcon.active = false;
  86. }
  87. else if (state == 1) {
  88. this.sickIcon.active = false;
  89. this.ripedIcon.active = true;
  90. }
  91. }
  92. public riped(sendToServer = true): void {
  93. this.counting = false;
  94. this.normalGroup.active = false;
  95. let sick = false;
  96. let cashTimes = gData.gameData.getProp(GameProp.cashTimes);
  97. if (gData.gameData.playerProp.orderData && gData.gameData.playerProp.orderData.overTimes >= 1 && cashTimes >= gData.gameData.insectAutoShowLimit && Math.random() < parseFloat(gData.gameData.configs.ServerConfig.StopProduction)) {
  98. if (this.productConfig.tab == ProductType.nzw) {
  99. if (gData.gameData.RawInsectCurArr[0] < parseInt(gData.gameData.RawInsectArr[0])) {
  100. sick = true;
  101. gData.gameData.RawInsectCurArr[0]++;
  102. }
  103. }
  104. else if (this.productConfig.tab == ProductType.dw) {
  105. if (gData.gameData.RawInsectCurArr[1] < parseInt(gData.gameData.RawInsectArr[1])) {
  106. sick = true;
  107. gData.gameData.RawInsectCurArr[1]++;
  108. }
  109. }
  110. else if (gData.gameData.RawInsectCurArr[2] < parseInt(gData.gameData.RawInsectArr[2])) {
  111. sick = true;
  112. gData.gameData.RawInsectCurArr[2]++;
  113. }
  114. }
  115. if (sick) {
  116. console.log('onRiped ' + 1111);
  117. this.sickIcon.active = true;
  118. this.onSick && this.onSick.emit([sendToServer]);
  119. }
  120. else {
  121. console.log('onRiped ' + 2222);
  122. this.ripedIcon.active = true;
  123. this.onRiped && this.onRiped.emit([sendToServer]);
  124. }
  125. }
  126. }