| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- import { Vec3 } from "cc";
- import { ecs } from "../../../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
- import { smc } from "../../SingletonModuleComp";
- import { MapInfo } from "../../../checkpoint/model/CheckpointModel";
- import { ECSEntity } from "../../../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECSEntity";
- import { ArrayUtil } from "../../../../../../extensions/oops-plugin-framework/assets/core/utils/ArrayUtil";
- @ecs.register('PathFind')
- export class PathFindComp extends ecs.Comp {
- private _path: Vec3[]
- private _canReach: boolean = false
- private _x: number
- private _y: number
- // private _mute: boolean = false
- // get mute(): boolean {
- // return this._mute
- // }
- // set mute(value: boolean) {
- // this._mute = value
- // }
- get y(): number {
- return this._y
- }
- set y(value: number) {
- this._y = value
- }
- get path(): Vec3[] {
- return this._path
- }
- set path(value: Vec3[]) {
- this._path = value
- }
- get canReach(): boolean {
- return this._canReach
- }
- set canReach(value: boolean) {
- this._canReach = value
- }
- get x(): number {
- return this._x
- }
- set x(value: number) {
- this._x = value
- }
- reset() {
- this.canReach = false
- this.path = null
- // this.mute = false
- }
- }
- @ecs.register('PathFindTrigger')
- export class PathFindTriggerComponent extends ecs.Comp {
- reset() {
- }
- }
- /** 路径查找 */
- export class PathFindSystem extends ecs.ComblockSystem<ecs.Entity> implements ecs.IEntityEnterSystem {
- filter(): ecs.IMatcher {
- return ecs.allOf(PathFindComp,PathFindTriggerComponent);
- }
- entityEnter(e: ecs.Entity): void {
- // const pathComp = e.get(PathFindComp)
- this.update(e)
- e.remove(PathFindTriggerComponent)
- }
- // entityRemove(e: ecs.Entity): void {
- // e.remove(PathFindComp)
- // }
- update(e: ECSEntity): void {
- const pathComp = e.get(PathFindComp)
- if (smc.initialize.account.checkpoint && smc.initialize.account.checkpoint.CheckpointModel) {
- const pathGrid = smc.initialize.account.checkpoint.CheckpointModel.path_grid
-
- const pathRes = this.findPath(ArrayUtil.copy2DArray(pathGrid), pathComp.x, pathComp.y)
- pathComp.path = pathRes
- if (pathRes && pathRes.length > 0) {
- pathComp.canReach = true
- } else {
- pathComp.canReach = false
- }
- }
- }
- findPath(grid: MapInfo[][], startX: number, startY: number): Vec3[] {
- // const cell = grid[startX][startY]
- // if (cell.fill == null) {
- // return null; // 起始格子是空的,无法移动
- // }
- let rows = grid.length;
- let cols = grid[0].length;
- let visited = Array.from({ length: rows }, () => Array(cols).fill(false));
- type pathQue = [number, number, Vec3[]]
- let queue: pathQue[] = [[startX, startY, null]]; // 初始位置和路径
- // 四个可能的移动方向(上、下、左、右)
- let directions = [[-1, 0], [1, 0], [0, -1], [0, 1]];
- while (queue.length > 0) {
- let [x, y, path] = queue.shift();
- visited[x][y] = true;
- // 检查是否到达顶部行
- if (x === 0) {
- return path ? path : [grid[x][y].pos];
- }
- for (let [dx, dy] of directions) {
- let newX = x + dx;
- let newY = y + dy;
- // 检查新位置是否有效
- if (newX >= 0 && newX < rows && newY >= 0 && newY < cols && !visited[newX][newY] && grid[newX][newY]?.fill == null) {
- queue.push([newX, newY, (path ? path.concat([grid[newX][newY]?.pos]) : [grid[x][y]?.pos, grid[newX][newY]?.pos])]);
- }
- }
- }
- return null; // 没有找到路径
- }
- }
|