diff --git a/package.json b/package.json index b681649..4cce13b 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ }, "dependencies": { "@neutralinojs/lib": "^5.3.0", + "navmesh": "^2.3.1", "phaser": "^3.80.1", "phaser3-rex-plugins": "^1.80.6" }, diff --git a/src/assets/assets.ts b/src/assets/assets.ts index 6ffe610..826dde8 100644 --- a/src/assets/assets.ts +++ b/src/assets/assets.ts @@ -42,8 +42,11 @@ const images: Image[] = [ // Items image('items/coin', 'coin'), image('items/bath1', 'bath_1'), + image('items/bath1f', 'bath_1_front'), image('items/bath2', 'bath_2'), + image('items/bath2f', 'bath_2_front'), image('items/bath3', 'bath_3'), + image('items/bath3f', 'bath_3_front'), image('items/wax1', 'wax_1'), image('items/wax2', 'wax_2'), image('items/wax3', 'wax_3'), diff --git a/src/assets/images/items/bath1.png b/src/assets/images/items/bath1.png index 93e2548..259a762 100644 Binary files a/src/assets/images/items/bath1.png and b/src/assets/images/items/bath1.png differ diff --git a/src/assets/images/items/bath1f.png b/src/assets/images/items/bath1f.png new file mode 100644 index 0000000..c5aa927 Binary files /dev/null and b/src/assets/images/items/bath1f.png differ diff --git a/src/assets/images/items/bath2.png b/src/assets/images/items/bath2.png index 8c74450..0b366e3 100644 Binary files a/src/assets/images/items/bath2.png and b/src/assets/images/items/bath2.png differ diff --git a/src/assets/images/items/bath2f.png b/src/assets/images/items/bath2f.png new file mode 100644 index 0000000..d88283b Binary files /dev/null and b/src/assets/images/items/bath2f.png differ diff --git a/src/assets/images/items/bath3.png b/src/assets/images/items/bath3.png index b5f0a5d..6c4e0bd 100644 Binary files a/src/assets/images/items/bath3.png and b/src/assets/images/items/bath3.png differ diff --git a/src/assets/images/items/bath3f.png b/src/assets/images/items/bath3f.png new file mode 100644 index 0000000..bb4e594 Binary files /dev/null and b/src/assets/images/items/bath3f.png differ diff --git a/src/components/Board.ts b/src/components/Board.ts index 027b741..bba8dcd 100644 --- a/src/components/Board.ts +++ b/src/components/Board.ts @@ -1,8 +1,15 @@ import { GameScene } from "@/scenes/GameScene"; +export type GridPoint = { + gridX: number, + gridY: number +}; + export class Board extends Phaser.GameObjects.Container { public scene: GameScene; public size: number; + public width: number; + public height: number; private grid: Phaser.GameObjects.Grid; private things: any[]; @@ -21,6 +28,9 @@ export class Board extends Phaser.GameObjects.Container { // this.size = scene.H / (height + 2); this.size = cellSize; + this.width = width; + this.height = height + this.grid = this.scene.add.grid( 0, 0, @@ -44,6 +54,8 @@ export class Board extends Phaser.GameObjects.Container { resize(width: number, height: number, cellSize: number) { // this.size = this.scene.H / height; this.size = cellSize; + this.width = width; + this.height = height this.grid.destroy(); this.grid = this.scene.add.grid( @@ -70,7 +82,7 @@ export class Board extends Phaser.GameObjects.Container { } // Return grid cell of the coordinates - coordToGrid(x: number, y: number) { + coordToGrid(x: number, y: number): GridPoint { const gridX = Math.floor((x - this.x + this.grid.width / 2) / this.size); const gridY = Math.floor((y - this.y + this.grid.height / 2) / this.size); return { gridX, gridY }; diff --git a/src/scenes/GameScene.ts b/src/scenes/GameScene.ts index d0c2a68..eb064e2 100644 --- a/src/scenes/GameScene.ts +++ b/src/scenes/GameScene.ts @@ -1,5 +1,5 @@ import { BaseScene } from "@/scenes/BaseScene"; -import { Board } from "@/components/Board"; +import { Board, GridPoint } from "@/components/Board"; import { Employee } from "@/components/Employee"; import { Customer } from "@/components/Customer"; import { CustomerId } from "@/components/CustomerData"; @@ -20,6 +20,9 @@ import { TextEffect } from "@/components/TextEffect"; import { BasicEffect } from "@/components/BasicEffect"; import { Intermission, Mode } from "@/components/Intermission"; +import { NavMesh } from "navmesh"; +import { centerOnSubdividedCoord, GenerateNavMesh } from "@/utils/NavMeshHelper"; + enum GameState { Cutscene, Day, @@ -27,6 +30,7 @@ enum GameState { Intermission, } + export class GameScene extends BaseScene { private background: Phaser.GameObjects.Image; private board: Board; @@ -45,6 +49,7 @@ export class GameScene extends BaseScene { public activeItem: ItemButton; public effects: Effect[]; + private navmesh: NavMesh; // Game stats public state: GameState = GameState.Cutscene; @@ -342,6 +347,8 @@ export class GameScene extends BaseScene { employee.upgrade(); } }); + + this.navmesh = GenerateNavMesh(this.board, LevelData[id]) } // Start a new day @@ -610,7 +617,7 @@ export class GameScene extends BaseScene { return; } - let closestEmployee: any = null; + let closestEmployee: Employee = null as unknown as Employee; let closestDistance = Infinity; this.employees.forEach((employee) => { @@ -640,6 +647,26 @@ export class GameScene extends BaseScene { closestEmployee.setCustomer(customer); closestEmployee.walkTo(x, y); + + const [cx, cy] = centerOnSubdividedCoord(this.board, station, 7); + const posEmp = this.board.coordToGrid(closestEmployee.x, closestEmployee.y); + const posSta = this.board.coordToGrid(station.x, station.y); + + + const scale = ({gridX, gridY}: GridPoint) => { + return {x: gridX*7, y: gridY*7} + } + + //this.navmesh.findPath({}) + + console.log(scale(posEmp)) + + const path = this.navmesh.findPath(scale(posEmp), scale(posSta)); + + + + console.log("path", path) + // Wait for employee.on("walkend") } } diff --git a/src/utils/NavMeshHelper.ts b/src/utils/NavMeshHelper.ts new file mode 100644 index 0000000..6f777b1 --- /dev/null +++ b/src/utils/NavMeshHelper.ts @@ -0,0 +1,92 @@ +import type { Board } from "@/components/Board"; +import { Level, BlockType } from "@/components/Levels"; +import { StationId } from "@/components/StationData"; +import type { Station } from "@/components/Station"; +import NavMesh, { buildPolysFromGridMap } from "navmesh"; + +const subdivision = 7; + +const stationMask = [ + [true,true,true,true,true,true,true], + [true,true,false,false,false,false,true], + [true,false,false,false,false,true,true], + [true,true,false,true,true,true,true], + [true,false,false,false,false,true,true], + [true,true,false,false,false,false,true], + [true,true,true,true,true,true,true], +]; + +function stationToGrid(board: Board, station: Station) { + const {gridX, gridY} = board.coordToGrid(station.x, station.y); + return [gridX, gridY]; +} + +function Array2DFromGrid(board: Board, subdivision: number): boolean[][] { + console.log(board.height) + return new Array(board.width*subdivision).fill(true).map(() => new Array(board.height*subdivision).fill(true)); +} + +export function centerOnSubdividedCoord(board: Board, station: Station, subdivision: number) { + const [x, y] = stationToGrid(board, station); + const center = Math.floor(subdivision/2); + return [x+center, y+center]; +} + +const t = true; +const f = false; + +function testNav() { + + const mesh = [ + [f, f, f, f, f], + [f, t, t, t, f], + [f, t, f, t, f], + [f, t, t, t, f], + [f, t, t, t, f], + [f, f, f, f, f], + ]; + + const polys = new NavMesh(buildPolysFromGridMap(mesh)); + const path = polys.findPath( + {x: 1, y: 1}, + {x: 3, y: 3} + ) + console.log("new polys", ) + +} + +export function GenerateNavMesh(board: Board, level: Level) { + testNav() + const nav = Array2DFromGrid(board, subdivision); + + for (let y = 0; y < level.height; y++) { + for (let x = 0; x < level.width; x++) { + const block = level.grid[y][x]; + + switch (block) { + case BlockType.Wall: + for(let sx = 0; sx < stationMask.length; sx++) { + for(let sy = 0; sy < stationMask.length; sy++) { + nav[x*subdivision + sx][y*subdivision + sy] = false; + } + } + break; + case BlockType.HornAndNails: + case BlockType.ScalePolish: + case BlockType.GoldBath: + for(let sx = 0; sx < stationMask.length; sx++) { + for(let sy = 0; sy < stationMask.length; sy++) { + if(!stationMask[sx][sy]) { + nav[x*subdivision + sx][y*subdivision + sy] = false; + } + } + } + //case BlockType.CashRegister: + //this.addStation(x, y, StationId.CashRegister); + break; + } + } + } + console.log("nav", nav) + return new NavMesh(buildPolysFromGridMap(nav)); +} \ No newline at end of file