| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- import { FarmState, ProductType } from '../../game/data/GameData';
- import { Data } from '../../mk/data/Data';
- import { MoveToCenter } from '../map/MoveToCenter';
- import { FarmCountDown } from './FarmCountDown';
- import { FarmGrowProcess } from './FarmGrowProcess';
- import { FarmSystem } from './FarmSystem';
- import { IDHelper } from './IDHelper';
- const { ccclass, property } = cc._decorator;
- @ccclass
- export class FarmIcon extends cc.Component {
- @property({ type: cc.Node, tooltip: "未解锁时显示节点" }) lockNode: cc.Node = null;
- @property({ type: cc.Node, tooltip: "可解锁节点" }) canUnlockNode: cc.Node = null;
- @property({ type: cc.Node, tooltip: "选中闪烁节点" }) selectNode: cc.Node = null;
- @property({ type: cc.Sprite, tooltip: "种植动画图标" }) plantIcon: cc.Sprite = null;
- @property({ type: FarmCountDown, tooltip: "倒计时组件" }) countDown: FarmCountDown = null;
- @property({ type: FarmGrowProcess, tooltip: "成长控制组件" }) process: FarmGrowProcess = null;
- public configID: number = 0;
- public sortID: number = 0;
- private plantID: number = 0;
- private _state: FarmState = -1;
- public get state(): FarmState { return this._state; }
- private data = null;
- onLoad() {
- this.configID = this.getComponent(IDHelper).buildID;
- this.sortID = this.getComponent(IDHelper).sortID;
- gData.farmSystem.addFram(this);
- }
- start() {
- this.freshState();
- }
- update(dt) {
- // if (this.state == FarmState.Lock && gData.gameData.hasBuildData(this.configID)) {
- // this.activeFram();
- // }
- // let producting = gData.gameData.getProductingList(this.configID);
- // if (producting.length > 0) {
- // if (producting[0].ripeDate <= Date.now()) {
- // this.setState(FarmState.Ripe);
- // } else {
- // this.setState(FarmState.Growing);
- // }
- // this.countDown.setData(this.configID, false);
- // this.process.setData(this.configID);
- // } else {
- // this.setState(FarmState.Empty);
- // this.countDown.cleanRiped();
- // this.process.cleanProcess();
- // }
- let index = gData.gameData.needFreshArr.indexOf(this.configID);
- if (index != -1) {
- this.freshState();
- gData.gameData.needFreshArr.splice(index, 1);
- }
- }
- freshState() {
- this.data = gData.gameData.getFarmDataMap(this.configID);
- this.setState(this.data.state);
- }
- private setState(value: FarmState): void {
- if (this.state != value) {
- this._state = value;
- this.lockNode.active = false;
- this.canUnlockNode.active = false;
- this.plantIcon.node.active = false;
- this.countDown.node.active = false;
- switch (this.state) {
- case FarmState.Lock:
- this.lockNode.active = true;
- break;
- case FarmState.CanUnlock:
- this.lockNode.active = true;
- this.canUnlockNode.active = true;
- break;
- case FarmState.Empty:
- break;
- case FarmState.Growing:
- this.plantIcon.node.active = true;
- this.countDown.node.active = true;
- break;
- case FarmState.Ripe:
- this.plantIcon.node.active = true;
- this.countDown.node.active = true;
- break;
- }
- }
- }
- public activeFram(): void {
- this.lockNode.active = false;
- }
- public selectFarm(isSelect = false): void {
- this.selectNode.active = isSelect;
- if (isSelect) {
- this.getComponent(MoveToCenter).move();
- }
- }
- public async onTouch() {
- switch (this.data.state) {
- case FarmState.Lock:
- mk.tip.pop(`请先解锁扩建农田`);
- break;
- case FarmState.CanUnlock:
- gData.adUnlockData.openPanel(this.configID);
- break;
- case FarmState.Empty:
- gData.farmSystem.currSelectFarm = this;
- gData.plantData.openPanel(ProductType.nzw, this.configID);
- break;
- case FarmState.Growing:
- break;
- case FarmState.Ripe:
- gData.farmSystem.currSelectFarm = this;
- gData.harvestData.openPanel(this.configID, this.plantID, this.onHarvest.bind(this));
- break;
- }
- }
- public onHarvest(): void {
- this.countDown.cleanRiped();
- this.process.cleanProcess();
- gData.farmSystem.currSelectFarm = null;
- this.data.state = FarmState.Empty;
- this.data.productID = 0;
- this.data.growSpan = 0;
- gData.gameData.setFarmDataMap(this.configID, this.data);
- }
- public async plant(id: number) {
- if (this.state == FarmState.Empty && !gData.farmSystem.plantIsRequesting) {
- this.plantID = id;
- gData.farmSystem.plantIsRequesting = true;
- let config = gData.gameData.getProductMap(this.plantID);
- let growSpan = Date.now() + config.time * 1000;
- this.data.state = FarmState.Growing;
- this.data.productID = this.plantID;
- this.data.growSpan = growSpan;
- gData.gameData.setFarmDataMap(this.configID, this.data);
- this.plantIcon.node.active = true;
- this.plantIcon.spriteFrame = await mk.loader.load("game/coregame/texture/plant_icons/plantIcon_" + this.plantID, cc.SpriteFrame);
- this.plantIcon.getComponent(cc.Animation).play();
- // this.plantIcon.getComponent(Sound).play();
- this.plantIcon.getComponent(cc.Animation).on(cc.Animation.EventType.FINISHED, this.onPlantAnimationFinish, this);
- gData.farmSystem.selectNextFarm();
- gData.farmSystem.plantIsRequesting = false;
- }
- }
- public onRiped(): void {
- this._state = FarmState.Ripe;
- this.data.state = FarmState.Ripe;
- gData.gameData.setFarmDataMap(this.configID, this.data);
- gData.gameData.addProductMakeTimesById(this.plantID);
- }
- private async onPlantAnimationFinish() {
- this.plantIcon.getComponent(cc.Animation).off(cc.Animation.EventType.FINISHED, this.onPlantAnimationFinish, this);
- this.plantIcon.node.active = false;
- this.countDown.setData(this.data);
- this.process.setData(this.data);
- }
- public novice_plant(): void {
- this.process.setData(this.data);
- }
- public novice_clean(): void {
- this.process.cleanProcess();
- }
- }
|