PastureSystem.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { _decorator, Component, Node, SystemEventType, find, EventTouch, UITransform, Vec2 } from 'cc';
  2. import { GeometryUtils } from '../../core/utils/GeometryUtils';
  3. import { PastureIcon, PastureState } from '../PastureIcon';
  4. import { TouchHelper } from '../TouchHelper';
  5. const { ccclass, property } = _decorator;
  6. @ccclass('PastureSystem')
  7. export class PastureSystem extends Component {
  8. @property({ type: [PastureIcon], tooltip: "牧场集合" }) pastures: Array<PastureIcon> = [];
  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.pastures.length; i++) {
  20. const pasture = this.pastures[i];
  21. const touchHelper = pasture.getComponent(TouchHelper);
  22. if (!this.isMoved && touchHelper.points.length >= 3) {
  23. this.isMoved = false;
  24. let touchPoint = e.getUILocation();
  25. let localPoint = pasture.node.getComponent(UITransform).convertToNodeSpaceAR(GeometryUtils.V2ToV3(touchPoint));
  26. isInPoly = GeometryUtils.pointInPoly(localPoint, touchHelper.points);
  27. if (isInPoly) {//农场被点击
  28. pasture.inPolygon.call(pasture);
  29. return;
  30. }
  31. }
  32. }
  33. if (!isInPoly) {
  34. for (let i = 0; i < this.pastures.length; i++) {
  35. this.pastures[i].canShowFeedGroup = false;
  36. this.pastures[i].canShowCountDownGroup = false;
  37. }
  38. }
  39. this.isMoved = false;
  40. }
  41. public clean(): void {
  42. for (let i = 0; i < this.pastures.length; i++) {
  43. this.pastures[i].clean();
  44. }
  45. }
  46. private onTouchMove(e: EventTouch) {
  47. for (let i = 0; i < this.pastures.length; i++) {
  48. this.pastures[i].canShowFeedGroup = false;
  49. this.pastures[i].canShowCountDownGroup = false;
  50. }
  51. if (e.getAllTouches().length > 1) { this.isMoved = true; return }
  52. this.isMoved = Vec2.distance(e.getStartLocation(), e.getLocation()) > 20;
  53. }
  54. private onTouchCancel(e: EventTouch) {
  55. this.isMoved = false;
  56. }
  57. onDestroy() {
  58. this.canvas.off(SystemEventType.TOUCH_MOVE, this.onTouchMove, this);
  59. this.canvas.off(SystemEventType.TOUCH_CANCEL, this.onTouchCancel, this);
  60. this.canvas.off(SystemEventType.TOUCH_END, this.onTouchEnd, this);
  61. }
  62. }