created PathfindingManager. WIP

This commit is contained in:
Hanusiak Piotr 2022-01-12 17:13:37 +01:00
parent 3f3f488924
commit d772fd27f8
6 changed files with 9630 additions and 12 deletions

9550
front/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -48,6 +48,7 @@
"axios": "^0.21.2",
"cross-env": "^7.0.3",
"deep-copy-ts": "^0.5.0",
"easystarjs": "^0.4.4",
"generic-type-guard": "^3.2.0",
"google-protobuf": "^3.13.0",
"phaser": "^3.54.0",

View File

@ -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);
}

View File

@ -48,9 +48,10 @@ import { PropertyUtils } from "../Map/PropertyUtils";
import { GameMapPropertiesListener } from "./GameMapPropertiesListener";
import { analyticsClient } from "../../Administration/AnalyticsClient";
import { GameMapProperties } from "./GameMapProperties";
import { CameraManager } from "./CameraManager";
import { PathfindingManager } from "../../Utils/PathfindingManager";
import type {
GroupCreatedUpdatedMessageInterface,
MessageUserJoined,
MessageUserMovedInterface,
MessageUserPositionInterface,
OnConnectInterface,
@ -64,9 +65,7 @@ import type { ActionableItem } from "../Items/ActionableItem";
import type { ItemFactoryInterface } from "../Items/ItemFactoryInterface";
import type { ITiledMap, ITiledMapLayer, ITiledMapProperty, ITiledMapObject, ITiledTileSet } from "../Map/ITiledMap";
import type { AddPlayerInterface } from "./AddPlayerInterface";
import { CameraManager } 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";
@ -88,8 +87,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;
import type { WasCameraUpdatedEvent } from "../../Api/Events/WasCameraUpdatedEvent";
@ -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.startFollow(this.CurrentPlayer);

View 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;
}
}
}

View File

@ -1990,6 +1990,13 @@ dot-case@^3.0.4:
no-case "^3.0.4"
tslib "^2.0.3"
easystarjs@^0.4.4:
version "0.4.4"
resolved "https://registry.yarnpkg.com/easystarjs/-/easystarjs-0.4.4.tgz#8cec6d20d0d8660715da0301d1da440370a8f40a"
integrity sha512-ZSt0TkB8xuIXRIrKsM3jkmk1/cZUtyvf0DqOXf6wuKq9slx9UA5kkLtiaWhtmOQFJFKdabbvXwk6RO0znghArQ==
dependencies:
heap "0.2.6"
ee-first@1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/ee-first/-/ee-first-1.1.1.tgz#590c61156b0ae2f4f0255732a158b266bc56b21d"
@ -2925,6 +2932,11 @@ he@^1.2.0:
resolved "https://registry.yarnpkg.com/he/-/he-1.2.0.tgz#84ae65fa7eafb165fddb61566ae14baf05664f0f"
integrity sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw==
heap@0.2.6:
version "0.2.6"
resolved "https://registry.yarnpkg.com/heap/-/heap-0.2.6.tgz#087e1f10b046932fc8594dd9e6d378afc9d1e5ac"
integrity sha1-CH4fELBGky/IWU3Z5tN4r8nR5aw=
hmac-drbg@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/hmac-drbg/-/hmac-drbg-1.0.1.tgz#d2745701025a6c775a6c545793ed502fc0c649a1"