FarmGrowProcess.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. private data = null;
  17. public async setData(data) {
  18. if (!this.isDataed) {
  19. this.data = data;
  20. this.isDataed = true;
  21. let producting = gData.gameData.getProductMap(data.productID);
  22. this.ripeTime = data.growSpan;
  23. this.totleTime = producting.time;
  24. this.seedIcon.node.active = true;
  25. this.seedIcon.spriteFrame = await mk.loader.load("game/coregame/texture/plant_icons/seed/seedIcon_" + data.productID, 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_" + data.productID, cc.SpriteFrame);
  28. this.grows[i].spriteFrame = await mk.loader.load("game/coregame/texture/plant_icons/chengzhang/cz_" + data.productID, cc.SpriteFrame);
  29. this.ripes[i].spriteFrame = await mk.loader.load("game/coregame/texture/plant_icons/ripe/ripe_" + data.productID, 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. }