import { _decorator, Component, Node, SystemEventType, find, EventTouch, UITransform, Vec2 } from 'cc'; import { GeometryUtils } from '../../core/utils/GeometryUtils'; import { PastureIcon, PastureState } from '../PastureIcon'; import { TouchHelper } from '../TouchHelper'; const { ccclass, property } = _decorator; @ccclass('PastureSystem') export class PastureSystem extends Component { @property({ type: [PastureIcon], tooltip: "牧场集合" }) pastures: Array = []; private isMoved = false; private canvas: Node; start() { this.canvas = find("Canvas"); this.canvas.on(SystemEventType.TOUCH_MOVE, this.onTouchMove, this); this.canvas.on(SystemEventType.TOUCH_CANCEL, this.onTouchCancel, this); this.canvas.on(SystemEventType.TOUCH_END, this.onTouchEnd, this); } private onTouchEnd(e: EventTouch): void { let isInPoly = false; for (let i = 0; i < this.pastures.length; i++) { const pasture = this.pastures[i]; const touchHelper = pasture.getComponent(TouchHelper); if (!this.isMoved && touchHelper.points.length >= 3) { this.isMoved = false; let touchPoint = e.getUILocation(); let localPoint = pasture.node.getComponent(UITransform).convertToNodeSpaceAR(GeometryUtils.V2ToV3(touchPoint)); isInPoly = GeometryUtils.pointInPoly(localPoint, touchHelper.points); if (isInPoly) {//农场被点击 pasture.inPolygon.call(pasture); return; } } } if (!isInPoly) { for (let i = 0; i < this.pastures.length; i++) { this.pastures[i].canShowFeedGroup = false; this.pastures[i].canShowCountDownGroup = false; } } this.isMoved = false; } public clean(): void { for (let i = 0; i < this.pastures.length; i++) { this.pastures[i].clean(); } } private onTouchMove(e: EventTouch) { for (let i = 0; i < this.pastures.length; i++) { this.pastures[i].canShowFeedGroup = false; this.pastures[i].canShowCountDownGroup = false; } if (e.getAllTouches().length > 1) { this.isMoved = true; return } this.isMoved = Vec2.distance(e.getStartLocation(), e.getLocation()) > 20; } private onTouchCancel(e: EventTouch) { this.isMoved = false; } onDestroy() { this.canvas.off(SystemEventType.TOUCH_MOVE, this.onTouchMove, this); this.canvas.off(SystemEventType.TOUCH_CANCEL, this.onTouchCancel, this); this.canvas.off(SystemEventType.TOUCH_END, this.onTouchEnd, this); } }