FarmGrowProcess.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { _decorator, Component, Node, Sprite, SpriteFrame, JsonAsset } from 'cc';
  2. import { ResourcesUtils } from '../core/resourceManager/ResourcesUtils';
  3. import { ConfigData } from '../Data/ConfigData';
  4. import { g } from '../Data/g';
  5. const { ccclass, property } = _decorator;
  6. @ccclass('FarmGrowProcess')
  7. export class FarmGrowProcess extends Component {
  8. @property({ type: [Node], tooltip: "阶段集合" }) states: Array<Node> = [];
  9. @property({ type: Sprite, tooltip: "种子图标" }) seedIcon: Sprite;
  10. @property({ type: [Sprite], tooltip: "发芽精灵集合" }) fayas: Array<Sprite> = [];
  11. @property({ type: [Sprite], tooltip: "成长精灵集合" }) grows: Array<Sprite> = [];
  12. @property({ type: [Sprite], tooltip: "成熟精灵集合" }) ripes: Array<Sprite> = [];
  13. /**成熟时间戳 */
  14. private ripeTime = 0;
  15. /**总时长 */
  16. private totleTime = 0;
  17. /**当前成长阶段 */
  18. private nowState = -1;
  19. private isDataed = false;
  20. public async setData(configID: number) {
  21. if (!this.isDataed) {
  22. this.isDataed = true;
  23. let producting = g.gameData.getProductingList(configID)[0];
  24. let config = ConfigData.configMap.get("product");
  25. let result = config[producting.productID];
  26. this.ripeTime = producting.ripeDate;
  27. this.totleTime = result["time"];
  28. this.seedIcon.node.active = true;
  29. this.seedIcon.spriteFrame = await ResourcesUtils.load("plant_icons/seed/seedIcon_" + producting.productID + "/spriteFrame", SpriteFrame, this.node);
  30. for (let i = 0; i < this.fayas.length; i++) {
  31. this.fayas[i].spriteFrame = await ResourcesUtils.load("plant_icons/faya/faya_" + producting.productID + "/spriteFrame", SpriteFrame, this.node);
  32. this.grows[i].spriteFrame = await ResourcesUtils.load("plant_icons/chengzhang/cz_" + producting.productID + "/spriteFrame", SpriteFrame, this.node);
  33. this.ripes[i].spriteFrame = await ResourcesUtils.load("plant_icons/ripe/ripe_" + producting.productID + "/spriteFrame", SpriteFrame, this.node);
  34. }
  35. this.changeState(0);
  36. }
  37. }
  38. update() {
  39. if (this.nowState >= 0 && this.nowState < 3) {
  40. let now = Date.now();
  41. let process = 1 - ((this.ripeTime - now) / (this.totleTime * 1000));
  42. if (process < 0.3) {
  43. this.changeState(0);
  44. } else if (process < 0.65) {
  45. this.changeState(1);
  46. } else if (process < 0.99) {
  47. this.changeState(2);
  48. } else {
  49. this.changeState(3);
  50. }
  51. }
  52. }
  53. private changeState(value: number): void {
  54. if (this.nowState != value) {
  55. for (let i = 0; i < this.states.length; i++) {
  56. this.states[i].active = false;
  57. }
  58. this.states[value] && (this.states[value].active = true);
  59. this.nowState = value;
  60. }
  61. }
  62. public onSpeed(): void {
  63. this.changeState(3);
  64. }
  65. public cleanProcess(): void {
  66. this.isDataed = false;
  67. this.changeState(-1);
  68. }
  69. }