FactoryCountDown.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { _decorator, Component, Node, JsonAsset, EventHandler } from 'cc';
  2. import { Utils } from '../../core/utils/Utils';
  3. import { ConfigData } from '../../Data/ConfigData';
  4. import { g } from '../../Data/g';
  5. const { ccclass, property } = _decorator;
  6. @ccclass('FactoryCountDown')
  7. export class FactoryCountDown extends Component {
  8. @property({ tooltip: "配置ID" }) configID: number = 0;
  9. @property({ type: EventHandler, tooltip: "商品制作完成时的回调" }) onComplete: EventHandler;
  10. public progress: number = 0;
  11. public timerString: string = "";
  12. private config: any;
  13. async start() {
  14. this.config = ConfigData.configMap.get("product");
  15. }
  16. update() {
  17. let producting = g.gameData.getProductingList(this.configID);
  18. let now = Date.now();
  19. if (producting.length > 0 && this.config) {
  20. let totleTime = this.config[producting[0].productID]["time"];
  21. if (producting[0].ripeDate > now) {
  22. this.progress = 1 - ((producting[0].ripeDate - now) / (totleTime * 1000));
  23. this.timerString = Utils.formatCountDown(producting[0].ripeDate - now);
  24. } else {
  25. this.onComplete && this.onComplete.emit([producting[0].productID]);
  26. g.gameData.getProductingList(this.configID).shift();
  27. }
  28. }
  29. }
  30. public async ripe() {
  31. let producting = g.gameData.getProductingList(this.configID);
  32. this.onComplete && this.onComplete.emit([producting[0].productID]);
  33. let build = ConfigData.configMap.get("build");
  34. if (build[this.configID]["sequence"] == 1) {
  35. let lastTime = producting[0].ripeDate - Date.now();
  36. g.gameData.getProductingList(this.configID).shift();
  37. for (let i = 0; i < g.gameData.getProductingList(this.configID).length; i++) {
  38. g.gameData.getProductingList(this.configID)[i].ripeDate -= lastTime;
  39. }
  40. } else {
  41. g.gameData.getProductingList(this.configID).shift();
  42. }
  43. }
  44. }