| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- import { DataEventId, ExpAddType, FarmState, GameProp, ProductType } from '../../game/data/GameData';
- import { MoveToCenter } from '../map/MoveToCenter';
- import { FarmCountDown } from './FarmCountDown';
- import { FarmGrowProcess } from './FarmGrowProcess';
- 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;
- @property({ type: cc.Node, tooltip: '闲置动画' }) idleAni: cc.Node = null;
- public configID: number = 0;
- public sortID: number = 0;
- public plantID: number = 0;
- private _state: FarmState = -1;
- public get state(): FarmState { return this._state; }
- public data = null;
- onLoad() {
- //this.plantIcon.node.active = false;
- let helper = this.getComponent(IDHelper);
- this.configID = helper.buildID;
- this.sortID = helper.sortID;
- gData.farmSystem.addFram(this);
- }
- start() {
- this.freshState();
- }
- update(dt) {
- 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 async setState(value: FarmState) {
- 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;
- this.process.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:
- if (this.idleAni && this.idleAni.active == false) {
- this.idleAni.active = true;
- }
- break;
- case FarmState.Growing:
- this.countDown.node.active = true;
- this.process.node.active = true;
- this.countDown.setData(this.data);
- this.process.setData(this.data);
- if (this.idleAni && this.idleAni.active == true) {
- this.idleAni.active = false;
- }
- break;
- case FarmState.Ripe:
- this.plantIcon.node.active = true;
- this.countDown.node.active = true;
- this.process.node.active = true;
- this.countDown.setState(1);
- this.process.setData(this.data);
- this.plantID = this.data.productID;
- let prefab = await mk.loader.load("game/prefab/ProductShow1", cc.Prefab);
- let item = cc.instantiate(prefab);
- item.name = 'hb';
- item.parent = this.node;
- if (this.configID == 31000) {
- mk.guide.open(15);
- }
- break;
- case FarmState.Sick:
- this.plantIcon.node.active = true;
- this.countDown.node.active = true;
- this.process.node.active = true;
- this.countDown.setState(0);
- this.process.setData(this.data);
- break;
- case FarmState.NoOpen:
- this.lockNode.active = true;
- this.lockNode.color = cc.Color.GRAY;
- break;
- }
- }
- }
- public activeFram(): void {
- this.lockNode.active = false;
- }
- public selectFarm(isSelect = false): void {
- this.selectNode.active = isSelect;
- if (isSelect) {
- this.getComponent(MoveToCenter).move();
- }
- }
- isCanTouch = true
- public async onTouch() {
- if (!this.isCanTouch) {
- return;
- }
- switch (this.data.state) {
- case FarmState.Lock:
- mk.tip.pop(`点击拓建即可解锁农田`);
- break;
- case FarmState.CanUnlock:
- mk.data.sendDataEvent(DataEventId.button_click, "拓建icon");
- gData.adUnlockData.openPanel(this.configID);
- break;
- case FarmState.Empty:
- gData.farmSystem.currSelectFarm = this;
- mk.guide.next();
- gData.plantData.openPanel(ProductType.nzw, this.configID);
- break;
- case FarmState.Growing:
- mk.ui.openPanel('module/speedUpUI/speedUp');
- break;
- case FarmState.Ripe:
- gData.farmSystem.currSelectFarm = this;
- this.onHarvest();
- // gData.harvestData.openPanel(this.configID, this.plantID, this.onHarvest.bind(this), this.node);
- this.isCanTouch = false;
- setTimeout(() => {
- this.isCanTouch = true;
- }, 1000);
- mk.guide.next();
- break;
- case FarmState.Sick:
- // mk.tip.pop(`生虫了!`);
- mk.data.sendDataEvent(DataEventId.button_click, "生虫icon");
- gData.adClearSickData.openPanel(ProductType.nzw, this.configID);
- break;
- case FarmState.NoOpen:
- mk.tip.pop(`暂未开放`);
- break;
- }
- }
- public async onHarvest() {
- this.countDown.cleanRiped();
- this.process.cleanProcess();
- gData.farmSystem.currSelectFarm = null;
- this.data.state = FarmState.Empty;
- this.data.productID = 0;
- this.data.growSpan = 0;
- let child = this.node.getChildByName('hb');
- this.node.removeChild(child);
- gData.gameData.setFarmDataMap(this.configID, this.data);
- //await gData.gameData.gameStyle.doCropFlyLogic(this.plantID, this.node, true);
- //gData.gameData.nextMake = null;
- gData.gameData.setNextProduct(false);
- gData.gameData.gameStyle.doCropFlyLogic(this.plantID);
- if (gData.harvestData.getType == 0) {
- gData.farmGradeData.addGradeExp(ExpAddType.EAT_harvest, this.node);
- }
- else {
- gData.farmGradeData.addGradeExp(ExpAddType.EAT_others, this.node);
- }
- gData.gameData.popTableScreenADLogic(7);
- mk.guide.open(16);
- }
- public async plant(id: number, parentNode = null) {
- if (this.state == FarmState.Empty) {
- this.plantID = id;
- gData.gameData.isProducting = true;
- this.plantIcon.node.active = true;
- gData.farmSystem.currSelectFarm = this;
- gData.adData.checkPopRed();
- //gData.gameData.updateOrderTaskCopyData(id);
- try {
- gData.gameData.changeLeftTimes(-1);
- gData.gameData.addProductMakeTimesById(this.plantID);
- // await gData.gameData.gameStyle.doCropFlyLogic(this.plantID, this.node, parentNode);
- } catch (error) {
- console.log(`==========framIcon`);
- }
- gData.gameData.nextMake = null;
- gData.gameData.setNextProduct(false);
- 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(cc.Animation).on(cc.Animation.EventType.FINISHED, this.onPlantAnimationFinish, this);
- //this.doMakePlantFinishLogic();
- let flyRed = await gData.gameData.updateNewTaskProgress();
- return flyRed;
- }
- else {
- gData.gameData.nextMake = null;
- gData.gameData.setNextProduct(false);
- if (gData.gameData.nextType == 0) {
- mk.tip.pop('农田已满,无法继续生产');
- return false;
- } else {
- let flyRed = await gData.gameData.makeProduct();
- return flyRed;
- }
- //mk.tip.pop('农田已满,无法继续生产');
- return false;
- }
- }
- /** 成熟 */
- public onRiped(sendToServer) {
- console.log('onRiped ' + this.configID);
- this.data.state = FarmState.Ripe;
- gData.gameData.setFarmDataMap(this.configID, this.data, sendToServer);
- }
- /** 生虫 */
- public onSick(sendToServer) {
- this.data.state = FarmState.Sick;
- gData.gameData.setFarmDataMap(this.configID, this.data, sendToServer);
- }
- private doMakePlantFinishLogic() {
- let config = gData.gameData.getProductMap(this.plantID);
- // if (mk.guide.getCurGuideIdString() == "10_2") {
- // this.data.growSpan = 0;
- // this.data.state = FarmState.Ripe;
- // } else {
- // let growSpan = Date.now() + config.time * 1000;
- // this.data.growSpan = growSpan;
- // this.data.state = FarmState.Growing;
- // }
- let growSpan = Date.now() + config.time * 1000;
- this.data.growSpan = growSpan;
- this.data.state = FarmState.Growing;
- this.data.productID = this.plantID;
- gData.gameData.setFarmDataMap(this.configID, this.data);
- gData.gameData.isProducting = false;
- }
- private async onPlantAnimationFinish() {
- this.plantIcon.getComponent(cc.Animation).off(cc.Animation.EventType.FINISHED, this.onPlantAnimationFinish, this);
- this.plantIcon.node.active = false;
- this.doMakePlantFinishLogic();
- }
- public novice_plant(): void {
- this.process.setData(this.data);
- }
- public novice_clean(): void {
- this.process.cleanProcess();
- }
- public canHarvest() {
- if (!this.isCanTouch) {
- return;
- }
- gData.farmSystem.currSelectFarm = this;
- gData.harvestData.openPanel(this.configID, this.plantID, this.onHarvest.bind(this), this.node);
- this.isCanTouch = false;
- setTimeout(() => {
- this.isCanTouch = true;
- }, 1000);
- }
- public canClearSick() {
- gData.farmSystem.currSelectFarm = this;
- mk.data.sendDataEvent(DataEventId.button_click, "生虫icon");
- gData.adClearSickData.openPanel(ProductType.nzw, this.configID);
- }
- public clickGuideBtn() {
- if (mk.guide.isGuiding()) {
- //gData.farmSystem.currSelectFarm = this;
- gData.harvestData.openPanel(this.configID, this.plantID, this.onHarvest.bind(this), this.node, 0, false, true);
- }
- }
- }
|