import { FarmState } from '../../game/data/GameData'; import Util from '../util/Util'; import { FarmIcon } from './FarmIcon'; import { TouchHelper } from './TouchHelper'; const { ccclass } = cc._decorator; @ccclass export class FarmSystem { public farms: Array = []; private _currSelectFarm: FarmIcon = null; public plantWindowIsOpening = false; /** 种植冷却 */ public plantIsRequesting = false; public set currSelectFarm(farm: FarmIcon) { if (this._currSelectFarm) { this._currSelectFarm.selectFarm(false); } this._currSelectFarm = farm; if (this._currSelectFarm) { let icon = this._currSelectFarm; icon.selectFarm(true); } } public get currSelectFarm(): FarmIcon { return this._currSelectFarm; } private canvas: cc.Node; start() { this.canvas = cc.find("Canvas"); this.canvas.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this); this.canvas.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this); this.canvas.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this); } private isMoved = false; private onTouchEnd(e) { if (!this.plantWindowIsOpening) { let isInPoly = false; for (let i = 0; i < this.farms.length; i++) { const frame = this.farms[i]; const touchHelper = frame.getComponent(TouchHelper); if (!this.isMoved && touchHelper.points.length >= 3) { this.isMoved = false; let touchPoint = e._point; let localPoint = frame.node.convertToNodeSpaceAR(touchPoint); isInPoly = Util.pointInPoly(localPoint, touchHelper.points); if (isInPoly) {//农场被点击 frame.onTouch.call(frame); if (frame.state == FarmState.Empty) { this.currSelectFarm = frame; } else { this.closeWindow(); } return; } } } if (!isInPoly && this.currSelectFarm) { this.closeWindow(); } this.isMoved = false; } } private onTouchMove(e: cc.Event.EventTouch) { // this.closeWindow(); if (e.getTouches().length > 1) { this.isMoved = true; return } this.isMoved = cc.Vec2.distance(e.getStartLocation(), e.getLocation()) > 20; } private closeWindow() { //暂时注释 // WindowManager.close(); this.currSelectFarm = null; } private onTouchCancel(e) { this.isMoved = false; } public addFram(fram: FarmIcon) { this.farms[fram.sortID] = fram; } public plant(id: number): void { this.currSelectFarm && this.currSelectFarm.plant(id); } public selectNextFarm(): FarmIcon | null { let nextFarm = this.getCanPlantFarm(); if (nextFarm) { this.currSelectFarm = nextFarm; } else { //暂时注释 // WindowManager.close(); } return nextFarm; } /**获取可种植农田 */ private getCanPlantFarm(): FarmIcon | null { for (let i = 0; i < this.farms.length; i++) { if (this.farms[i] != this.currSelectFarm && this.farms[i].state == FarmState.Empty) { return this.farms[i]; } } return null; } /**触摸事件的同级传递 */ public next(node: FarmIcon, e): void { let index = this.farms.indexOf(node); if (index % 3 < 2) { this.farms[index + 1].node.emit(cc.Node.EventType.TOUCH_END, e); } } onDestroy() { this.canvas.off(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this); this.canvas.off(cc.Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this); this.canvas.off(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this); } public novice_plant(): void { let frame = this.farms[0]; frame.novice_plant(); } public novice_clean(): void { let frame = this.farms[0]; frame.novice_clean(); } }