FarmCountDown.ts 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { v3 } from 'cc';
  2. import { _decorator, Component, Sprite, SpriteFrame, JsonAsset, Node, ProgressBar, EventHandler, Label } from 'cc';
  3. import { Http } from '../core/net/Http';
  4. import { ResourcesUtils } from '../core/resourceManager/ResourcesUtils';
  5. import { BitmapFont } from '../core/ui/BitmapFont';
  6. import { WindowManager } from '../core/ui/window/WindowManager';
  7. import { Utils } from '../core/utils/Utils';
  8. import { ConfigData } from '../Data/ConfigData';
  9. import { g } from '../Data/g';
  10. import { platform } from '../Data/platform';
  11. const { ccclass, property } = _decorator;
  12. @ccclass('FarmCountDown')
  13. export class FarmCountDown extends Component {
  14. @property({ type: EventHandler, tooltip: "成熟回调" }) onRiped: EventHandler;
  15. @property({ type: EventHandler, tooltip: "加速按钮回调" }) onSpeedButton: EventHandler;
  16. @property({ type: Node, tooltip: "常驻计时器组" }) normalGroup: Node;
  17. @property({ type: ProgressBar, tooltip: "常驻进度条" }) normalProgress: ProgressBar;
  18. @property({ type: BitmapFont, tooltip: "常驻计时" }) normalCount: BitmapFont;
  19. @property({ type: Node, tooltip: "可收获图标" }) ripedIcon: Node;
  20. @property({ type: Node, tooltip: "点击计时器组" }) selectGroup: Node;
  21. @property({ type: ProgressBar, tooltip: "点击进度条" }) selectProgress: ProgressBar;
  22. @property({ type: BitmapFont, tooltip: "点击计时" }) selectCount: BitmapFont;
  23. @property({ type: Sprite, tooltip: "作物图标" }) plantIcon: Sprite;
  24. @property({ type: Label, tooltip: "加速花费文本" }) castLabel: Label;
  25. @property({ tooltip: "网络请求对象", type: Http }) http: Http;
  26. private counting = false;
  27. /**成熟时间戳 */
  28. private ripeTime = 0;
  29. /**总时长 */
  30. private totleTime = 0;
  31. private buildID = 0;
  32. private isDataed = false;
  33. public async setData(configID: number, isShow = true) {
  34. if (!this.isDataed) {
  35. this.isDataed = true;
  36. this.buildID = configID;
  37. let producting = g.gameData.getProductingList(configID)[0];
  38. let config = ConfigData.configMap.get("product");
  39. let result = config[producting.productID];
  40. this.ripeTime = producting.ripeDate;
  41. this.totleTime = result["time"];
  42. this.normalGroup.active = true;
  43. this.counting = true;
  44. this.selectGroup.active = isShow;
  45. this.plantIcon.spriteFrame = await ResourcesUtils.load<SpriteFrame>("plant_icons/plantIcon_" + producting.productID + "/spriteFrame", SpriteFrame, this.node);
  46. }
  47. }
  48. update() {
  49. let now = Date.now();
  50. if (this.counting && now < this.ripeTime) {
  51. let timeString = Utils.formatCountDown(this.ripeTime - now);
  52. this.selectCount.string = this.normalCount.string = timeString;
  53. this.selectProgress.progress = this.normalProgress.progress = 1 - ((this.ripeTime - now) / (this.totleTime * 1000));
  54. } else if (this.counting && now > this.ripeTime) {
  55. this.riped();
  56. }
  57. }
  58. public cleanRiped(): void {
  59. this.isDataed = false;
  60. this.ripedIcon.active = false;
  61. }
  62. public async onSpeed() {
  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. public setCountActive(value: boolean): void {
  77. this.selectGroup.active = value;
  78. }
  79. private riped(): void {
  80. this.counting = false;
  81. this.normalGroup.active = false;
  82. this.selectGroup.active = false;
  83. this.ripedIcon.active = true;
  84. this.onRiped && this.onRiped.emit(null);
  85. }
  86. }