| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- import { _decorator, Component, Node, Sprite, SpriteFrame, JsonAsset } from 'cc';
- import { ResourcesUtils } from '../core/resourceManager/ResourcesUtils';
- import { ConfigData } from '../Data/ConfigData';
- import { g } from '../Data/g';
- const { ccclass, property } = _decorator;
- @ccclass('FarmGrowProcess')
- export class FarmGrowProcess extends Component {
- @property({ type: [Node], tooltip: "阶段集合" }) states: Array<Node> = [];
- @property({ type: Sprite, tooltip: "种子图标" }) seedIcon: Sprite;
- @property({ type: [Sprite], tooltip: "发芽精灵集合" }) fayas: Array<Sprite> = [];
- @property({ type: [Sprite], tooltip: "成长精灵集合" }) grows: Array<Sprite> = [];
- @property({ type: [Sprite], tooltip: "成熟精灵集合" }) ripes: Array<Sprite> = [];
- /**成熟时间戳 */
- private ripeTime = 0;
- /**总时长 */
- private totleTime = 0;
- /**当前成长阶段 */
- private nowState = -1;
- private isDataed = false;
- public async setData(configID: number) {
- if (!this.isDataed) {
- this.isDataed = true;
- let producting = g.gameData.getProductingList(configID)[0];
- let config = ConfigData.configMap.get("product");
- let result = config[producting.productID];
- this.ripeTime = producting.ripeDate;
- this.totleTime = result["time"];
- this.seedIcon.node.active = true;
- this.seedIcon.spriteFrame = await ResourcesUtils.load("plant_icons/seed/seedIcon_" + producting.productID + "/spriteFrame", SpriteFrame, this.node);
- for (let i = 0; i < this.fayas.length; i++) {
- this.fayas[i].spriteFrame = await ResourcesUtils.load("plant_icons/faya/faya_" + producting.productID + "/spriteFrame", SpriteFrame, this.node);
- this.grows[i].spriteFrame = await ResourcesUtils.load("plant_icons/chengzhang/cz_" + producting.productID + "/spriteFrame", SpriteFrame, this.node);
- this.ripes[i].spriteFrame = await ResourcesUtils.load("plant_icons/ripe/ripe_" + producting.productID + "/spriteFrame", SpriteFrame, this.node);
- }
- this.changeState(0);
- }
- }
- update() {
- if (this.nowState >= 0 && this.nowState < 3) {
- let now = Date.now();
- let process = 1 - ((this.ripeTime - now) / (this.totleTime * 1000));
- if (process < 0.3) {
- this.changeState(0);
- } else if (process < 0.65) {
- this.changeState(1);
- } else if (process < 0.99) {
- this.changeState(2);
- } else {
- this.changeState(3);
- }
- }
- }
- private changeState(value: number): void {
- if (this.nowState != value) {
- for (let i = 0; i < this.states.length; i++) {
- this.states[i].active = false;
- }
- this.states[value] && (this.states[value].active = true);
- this.nowState = value;
- }
- }
- public onSpeed(): void {
- this.changeState(3);
- }
- public cleanProcess(): void {
- this.isDataed = false;
- this.changeState(-1);
- }
- }
|