import { _decorator, Component, Node, SystemEventType, find, EventTouch, UITransform, Vec2 } from 'cc'; import { GeometryUtils } from '../../core/utils/GeometryUtils'; import { MapIcon } from '../../Main/MapIcon'; import { TouchHelper } from '../../Main/TouchHelper'; const { ccclass, property } = _decorator; @ccclass('FactorySystem') export class FactorySystem extends Component { @property({ type: [MapIcon], tooltip: "工厂合计" }) factorys: 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.factorys.length; i++) { const factorys = this.factorys[i]; const touchHelper = factorys.getComponent(TouchHelper); if (!this.isMoved && touchHelper.points.length >= 3) { this.isMoved = false; let touchPoint = e.getUILocation(); let localPoint = factorys.node.getComponent(UITransform).convertToNodeSpaceAR(GeometryUtils.V2ToV3(touchPoint)); isInPoly = GeometryUtils.pointInPoly(localPoint, touchHelper.points); if (isInPoly) {//农场被点击 factorys.inPolygon.call(factorys); return; } } } this.isMoved = false; } public clean(): void { for (let i = 0; i < this.factorys.length; i++) { this.factorys[i].clean(); } } private onTouchMove(e: EventTouch) { 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; } }