FarmCountDown.ts 4.3 KB

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