FactorySystem.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { _decorator, Component, Node, SystemEventType, find, EventTouch, UITransform, Vec2 } from 'cc';
  2. import { GeometryUtils } from '../../core/utils/GeometryUtils';
  3. import { MapIcon } from '../../Main/MapIcon';
  4. import { TouchHelper } from '../../Main/TouchHelper';
  5. const { ccclass, property } = _decorator;
  6. @ccclass('FactorySystem')
  7. export class FactorySystem extends Component {
  8. @property({ type: [MapIcon], tooltip: "工厂合计" }) factorys: Array<MapIcon> = [];
  9. private isMoved = false;
  10. private canvas: Node;
  11. start() {
  12. this.canvas = find("Canvas");
  13. this.canvas.on(SystemEventType.TOUCH_MOVE, this.onTouchMove, this);
  14. this.canvas.on(SystemEventType.TOUCH_CANCEL, this.onTouchCancel, this);
  15. this.canvas.on(SystemEventType.TOUCH_END, this.onTouchEnd, this);
  16. }
  17. private onTouchEnd(e: EventTouch): void {
  18. let isInPoly = false;
  19. for (let i = 0; i < this.factorys.length; i++) {
  20. const factorys = this.factorys[i];
  21. const touchHelper = factorys.getComponent(TouchHelper);
  22. if (!this.isMoved && touchHelper.points.length >= 3) {
  23. this.isMoved = false;
  24. let touchPoint = e.getUILocation();
  25. let localPoint = factorys.node.getComponent(UITransform).convertToNodeSpaceAR(GeometryUtils.V2ToV3(touchPoint));
  26. isInPoly = GeometryUtils.pointInPoly(localPoint, touchHelper.points);
  27. if (isInPoly) {//农场被点击
  28. factorys.inPolygon.call(factorys);
  29. return;
  30. }
  31. }
  32. }
  33. this.isMoved = false;
  34. }
  35. public clean(): void {
  36. for (let i = 0; i < this.factorys.length; i++) {
  37. this.factorys[i].clean();
  38. }
  39. }
  40. private onTouchMove(e: EventTouch) {
  41. if (e.getAllTouches().length > 1) { this.isMoved = true; return }
  42. this.isMoved = Vec2.distance(e.getStartLocation(), e.getLocation()) > 20;
  43. }
  44. private onTouchCancel(e: EventTouch) {
  45. this.isMoved = false;
  46. }
  47. }