FarmGrowProcess.ts 2.9 KB

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