created PathfindingManager. WIP
This commit is contained in:
parent
82c2d21423
commit
000b2cfe73
6 changed files with 9629 additions and 11 deletions
|
@ -1,10 +1,4 @@
|
|||
import type {
|
||||
ITiledMap,
|
||||
ITiledMapLayer,
|
||||
ITiledMapObject,
|
||||
ITiledMapObjectLayer,
|
||||
ITiledMapProperty,
|
||||
} from "../Map/ITiledMap";
|
||||
import type { ITiledMap, ITiledMapLayer, ITiledMapObject, ITiledMapProperty } from "../Map/ITiledMap";
|
||||
import { flattenGroupLayersMap } from "../Map/LayersFlattener";
|
||||
import TilemapLayer = Phaser.Tilemaps.TilemapLayer;
|
||||
import { DEPTH_OVERLAY_INDEX } from "./DepthIndexes";
|
||||
|
@ -120,6 +114,22 @@ export class GameMap {
|
|||
return [];
|
||||
}
|
||||
|
||||
public getCollisionsGrid(): number[][] {
|
||||
const collisionsLayer = this.findPhaserLayer("collisions");
|
||||
if (!collisionsLayer) {
|
||||
return [];
|
||||
}
|
||||
const grid: number[][] = [];
|
||||
for (let y = 0; y < collisionsLayer.height; y += 1) {
|
||||
const row: number[] = [];
|
||||
for (let x = 0; x < collisionsLayer.width; x += 1) {
|
||||
row.push(collisionsLayer.getTileAt(x, y) ? 1 : 0);
|
||||
}
|
||||
grid.push(row);
|
||||
}
|
||||
return grid;
|
||||
}
|
||||
|
||||
private getLayersByKey(key: number): Array<ITiledMapLayer> {
|
||||
return this.flatLayers.filter((flatLayer) => flatLayer.type === "tilelayer" && flatLayer.data[key] !== 0);
|
||||
}
|
||||
|
|
|
@ -48,9 +48,9 @@ import { PropertyUtils } from "../Map/PropertyUtils";
|
|||
import { GameMapPropertiesListener } from "./GameMapPropertiesListener";
|
||||
import { analyticsClient } from "../../Administration/AnalyticsClient";
|
||||
import { GameMapProperties } from "./GameMapProperties";
|
||||
import { PathfindingManager } from "../../Utils/PathfindingManager";
|
||||
import type {
|
||||
GroupCreatedUpdatedMessageInterface,
|
||||
MessageUserJoined,
|
||||
MessageUserMovedInterface,
|
||||
MessageUserPositionInterface,
|
||||
OnConnectInterface,
|
||||
|
@ -66,7 +66,6 @@ import type { ITiledMap, ITiledMapLayer, ITiledMapProperty, ITiledMapObject, ITi
|
|||
import type { AddPlayerInterface } from "./AddPlayerInterface";
|
||||
import { CameraManager, CameraManagerEvent, CameraManagerEventCameraUpdateData } from "./CameraManager";
|
||||
import type { HasPlayerMovedEvent } from "../../Api/Events/HasPlayerMovedEvent";
|
||||
import type { Character } from "../Entity/Character";
|
||||
|
||||
import { peerStore } from "../../Stores/PeerStore";
|
||||
import { biggestAvailableAreaStore } from "../../Stores/BiggestAvailableAreaStore";
|
||||
|
@ -89,8 +88,7 @@ import SpriteSheetFile = Phaser.Loader.FileTypes.SpriteSheetFile;
|
|||
import { deepCopy } from "deep-copy-ts";
|
||||
import FILE_LOAD_ERROR = Phaser.Loader.Events.FILE_LOAD_ERROR;
|
||||
import { MapStore } from "../../Stores/Utils/MapStore";
|
||||
import { followUsersColorStore, followUsersStore } from "../../Stores/FollowStore";
|
||||
import { getColorRgbFromHue } from "../../WebRtc/ColorGenerator";
|
||||
import { followUsersColorStore } from "../../Stores/FollowStore";
|
||||
import Camera = Phaser.Cameras.Scene2D.Camera;
|
||||
|
||||
export interface GameSceneInitInterface {
|
||||
|
@ -203,6 +201,7 @@ export class GameScene extends DirtyScene {
|
|||
private mapTransitioning: boolean = false; //used to prevent transitions happening at the same time.
|
||||
private emoteManager!: EmoteManager;
|
||||
private cameraManager!: CameraManager;
|
||||
private pathfindingManager!: PathfindingManager;
|
||||
private preloading: boolean = true;
|
||||
private startPositionCalculator!: StartPositionCalculator;
|
||||
private sharedVariablesManager!: SharedVariablesManager;
|
||||
|
@ -568,6 +567,9 @@ export class GameScene extends DirtyScene {
|
|||
{ x: this.Map.widthInPixels, y: this.Map.heightInPixels },
|
||||
waScaleManager
|
||||
);
|
||||
|
||||
this.pathfindingManager = new PathfindingManager(this, this.gameMap.getCollisionsGrid());
|
||||
this.pathfindingManager.findPath({ x: 1, y: 3 }, { x: 29, y: 3 });
|
||||
biggestAvailableAreaStore.recompute();
|
||||
this.cameraManager.startFollowPlayer(this.CurrentPlayer);
|
||||
|
||||
|
|
43
front/src/Utils/PathfindingManager.ts
Normal file
43
front/src/Utils/PathfindingManager.ts
Normal file
|
@ -0,0 +1,43 @@
|
|||
import * as EasyStar from "easystarjs";
|
||||
|
||||
export class PathfindingManager {
|
||||
private scene: Phaser.Scene;
|
||||
|
||||
private easyStar;
|
||||
|
||||
constructor(scene: Phaser.Scene, collisionsGrid: number[][]) {
|
||||
this.scene = scene;
|
||||
|
||||
this.easyStar = new EasyStar.js();
|
||||
|
||||
this.setGrid(collisionsGrid);
|
||||
}
|
||||
|
||||
public findPath(start: { x: number; y: number }, end: { x: number; y: number }): void {
|
||||
console.log("TRY TO FIND PATH");
|
||||
this.easyStar.findPath(start.x, start.y, end.x, end.y, (path) => {
|
||||
if (path === null) {
|
||||
console.warn("Path was not found.");
|
||||
} else {
|
||||
console.log("path was found");
|
||||
console.log(path);
|
||||
}
|
||||
});
|
||||
this.easyStar.calculate();
|
||||
}
|
||||
|
||||
private setGrid(grid: number[][]): void {
|
||||
console.log(grid);
|
||||
this.easyStar.setGrid(grid);
|
||||
this.easyStar.setAcceptableTiles([0]); // zeroes are walkable
|
||||
this.logGridToTheConsole(grid);
|
||||
}
|
||||
|
||||
private logGridToTheConsole(grid: number[][]): void {
|
||||
let rowNumber = 0;
|
||||
for (const row of grid) {
|
||||
console.log(`${rowNumber}:\t${row}`);
|
||||
rowNumber += 1;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue