| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- import { _decorator, Component, Node, SystemEventType, EventTouch, find, UITransform, Vec2 } from 'cc';
- import { WindowManager } from '../core/ui/window/WindowManager';
- import { GeometryUtils } from '../core/utils/GeometryUtils';
- import { FarmIcon, FarmState } from './FarmIcon';
- import { FarmManager } from './FarmManager';
- import { TouchHelper } from './TouchHelper';
- const { ccclass, property } = _decorator;
- @ccclass('FarmSystem')
- export class FarmSystem extends Component {
- public farms: Array<FarmIcon> = [];
- private _currSelectFarm: FarmIcon;
- 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: 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 isMoved = false;
- private onTouchEnd(e: EventTouch) {
- if (!FarmManager.plantWindowIsOpening) {
- let isInPoly = false;
- this.hideAllSelectProgressGroup();
- 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.getUILocation();
- let localPoint = frame.node.getComponent(UITransform).convertToNodeSpaceAR(GeometryUtils.V2ToV3(touchPoint));
- isInPoly = GeometryUtils.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: EventTouch) {
- // this.closeWindow();
- if (e.getAllTouches().length > 1) { this.isMoved = true; return }
- this.isMoved = Vec2.distance(e.getStartLocation(), e.getLocation()) > 20;
- }
- private closeWindow() {
- WindowManager.close();
- this.currSelectFarm = null;
- }
- private onTouchCancel(e: EventTouch) {
- 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;
- }
- public hideAllSelectProgressGroup(): void {
- for (let i = 0; i < this.farms.length; i++) {
- this.farms[i].countDown.setCountActive(false);
- }
- }
- /**获取可种植农田 */
- 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: EventTouch): void {
- let index = this.farms.indexOf(node);
- if (index % 3 < 2) {
- this.farms[index + 1].node.emit(SystemEventType.TOUCH_END, e);
- }
- }
- 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);
- }
- public novice_plant(): void {
- let frame = this.farms[0];
- frame.novice_plant();
- }
- public novice_clean(): void {
- let frame = this.farms[0];
- frame.novice_clean();
- }
- }
|