some fixes

This commit is contained in:
TorgaW 2024-04-19 16:05:51 +03:00
parent 6df6956133
commit c1831994a2
7 changed files with 229 additions and 83 deletions

View File

@ -19,7 +19,7 @@ import { ChunkStorageTypes } from "./WorldGeneration/WorldChunk/WorldGenChunk";
import { WorldChunksVisibilityUpdater } from "./WorldGeneration/WorldChunksVisibilityUpdater/WorldChunksVisibilityUpdater";
import { NPCProto } from "./NPC/NPCProto/NPCProto";
import { NPCController } from "./NPC/NPCController/NPCController";
import { PathFinder } from "./Utils/PathFinding.util";
import { PathFinder } from "./Utils/PathFinding.utils";
export function generateWorld() {
@ -65,7 +65,7 @@ function setupInGameSelector() {
let t = screenToWorldCoordinates(e.data.global.x, e.data.global.y);
// npccc.moveTo(new Point2D(getTileAt(t.x, t.y, ChunkStorageTypes.TYPE_TERRAIN).worldPosition.getX(), getTileAt(t.x, t.y, ChunkStorageTypes.TYPE_TERRAIN).worldPosition.getY()));
let tile = getTileAt(t.x, t.y, ChunkStorageTypes.TYPE_TERRAIN);
if(tile.props.navigationCost > 10) return;
if(tile.props.navigationCost > 100) return;
npccc.moveTo(new PointInt2D(tile.worldPosition.getX(), tile.worldPosition.getY()),
(cb)=>{
console.log(cb);
@ -154,9 +154,4 @@ function startGame() {
testNPC.drawObject.zIndex = 4;
testNPC.drawObject.scale.set(BC_SPRITES_SETTINGS.scale, BC_SPRITES_SETTINGS.scale);
npccc = testNPCController;
// let pf = new PathFinder();
// setTimeout(()=>{
// console.log(pf.search(new PointInt2D(), new PointInt2D(0, 0)));
// }, 1);
// testNPCController.moveTo(new Point2D(dest.worldPosition.getX(), dest.worldPosition.getY()));
}

View File

@ -1,6 +1,6 @@
import { GameObject } from "../../GameObject/GameObject";
import { PointInt2D } from "../../Utils/Math.utils";
import { NavigationPath, PathFinder } from "../../Utils/PathFinding.util";
import { NavigationPath, PathFinder } from "../../Utils/PathFinding.utils";
import { NPCProto } from "../NPCProto/NPCProto";
/**
* NPCController defines NPC behavior. Many NPC can have same NPCController for the same behavior.
@ -35,18 +35,15 @@ export class NPCController extends GameObject
{
let pf = new PathFinder();
let nPath = pf.search(new PointInt2D(this.controlledNPC.worldPosition.getX(), this.controlledNPC.worldPosition.getY()), position);
console.log(nPath);
if(nPath.error)
{
console.log("failed");
callback("failed");
console.log(nPath);
return;
}
else if (nPath.result.path.length < 2)
{
console.log("success");
callback("success");
console.log(nPath);
return;
}
for (let i = nPath.result.path.length-1; i > 0; i--) {
@ -68,7 +65,6 @@ export class NPCController extends GameObject
tick(ticker)
{
// console.log("object");
if(this.navigationInProgress)
{
if(!this.navigationFollowMidPoint)
@ -78,7 +74,6 @@ export class NPCController extends GameObject
{
this.navigationInProgress = false;
this.navigationCallback("success");
console.log("success");
}
else
{

View File

@ -62,7 +62,14 @@ export class PointInt2D
};
setX(x) {
this.#x = Math.floor(x);
if(x >= 0)
{
this.#x = Math.floor(x);
}
else
{
this.#x = Math.ceil(x);
}
};
getY() {
@ -70,7 +77,14 @@ export class PointInt2D
};
setY(y) {
this.#y = Math.floor(y);
if(y >= 0)
{
this.#y = Math.floor(y);
}
else
{
this.#y = Math.ceil(y);
}
};
/**
@ -101,7 +115,7 @@ export class PointInt2D
this.#x = Math.floor(this.#x * x);
this.#y = Math.floor(this.#y * x);
}
else if (x < 0)
else
{
this.#x = Math.ceil(this.#x * x);
this.#y = Math.ceil(this.#y * x);

View File

@ -3,6 +3,7 @@ import { ChunkStorageTypes } from "../WorldGeneration/WorldChunk/WorldGenChunk";
import { getTileAt } from "../WorldGeneration/WorldGen";
import { PointInt2D } from "./Math.utils";
import { SceneObject } from "../SceneObjects/SceneObject";
import { getNavigationGridTile } from "../World/NavigationGrid/NavigationGrid";
class PathFinderNode {
position;
@ -30,16 +31,14 @@ export class NavigationPath {
path;
/**
* @param {Array<PointInt2D>} path
* @param {Array<PointInt2D>} path
*/
constructor(path = [])
{
constructor(path = []) {
this.path = path;
}
};
}
export class NavigationResult
{
export class NavigationResult {
/**
* @type Boolean
*/
@ -52,14 +51,21 @@ export class NavigationResult
* @type NavigationPath | undefined
*/
result;
/**
* 0 - point is unreachable
* 1 - path found
* 2 - error
* @type Int
*/
state;
constructor(error, errorText, result)
{
constructor(error, errorText, result, state) {
this.error = error;
this.errorText = errorText;
this.result = result;
this.state = state;
}
};
}
export class PathFinder {
/**
@ -169,10 +175,11 @@ export class PathFinder {
* @param {PathFinderNode} current
*/
_reconstructPath(cameFrom, current) {
// console.log(cameFrom);
let totalPath = [current.position];
// console.log(cameFrom);
let keys = [...cameFrom.keys()];
while (keys.includes(current.id)) {
if (current.id === cameFrom.get(current.id).id) break;
current = cameFrom.get(current.id);
totalPath.push(current.position);
}
@ -188,51 +195,43 @@ export class PathFinder {
* @type Array<PathFinderNode>
*/
let neighbors = new Array();
/**
* @type SceneObject
*/
//north
let currentTile = getTileAt(root.getX(), root.getY() + BC_TERRAIN_SETTINGS.totalSize, ChunkStorageTypes.TYPE_TERRAIN);
let currentTile = getNavigationGridTile(root.getX(), root.getY() + BC_TERRAIN_SETTINGS.totalSize);
let node;
if(currentTile)
{
// currentTile.drawObject.tint = 0xff0000;
node = new PathFinderNode(new PointInt2D(currentTile.worldPosition.getX(), currentTile.worldPosition.getY()), 1e16, 1e16, currentTile.props.navigationCost);
if (currentTile && !currentTile.isObstacle) {
currentTile.optionalTerrainTileRef.drawObject.tint = 0xff0000;
node = new PathFinderNode(currentTile.position, 1e16, 1e16, currentTile.movementCost);
if (!this._existsInClosedSet(node)) {
neighbors.push(node);
}
}
//south
currentTile = getTileAt(root.getX(), root.getY() - BC_TERRAIN_SETTINGS.totalSize, ChunkStorageTypes.TYPE_TERRAIN);
if(currentTile)
{
// currentTile.drawObject.tint = 0xff0000;
node = new PathFinderNode(new PointInt2D(currentTile.worldPosition.getX(), currentTile.worldPosition.getY()), 1e16, 1e16, currentTile.props.navigationCost);
currentTile = getNavigationGridTile(root.getX(), root.getY() - BC_TERRAIN_SETTINGS.totalSize);
if (currentTile && !currentTile.isObstacle) {
currentTile.optionalTerrainTileRef.drawObject.tint = 0xff0000;
node = new PathFinderNode(currentTile.position, 1e16, 1e16, currentTile.movementCost);
if (!this._existsInClosedSet(node)) {
neighbors.push(node);
}
}
//east
currentTile = getTileAt(root.getX() + BC_TERRAIN_SETTINGS.totalSize, root.getY(), ChunkStorageTypes.TYPE_TERRAIN);
if(currentTile)
{
// currentTile.drawObject.tint = 0xff0000;
node = new PathFinderNode(new PointInt2D(currentTile.worldPosition.getX(), currentTile.worldPosition.getY()), 1e16, 1e16, currentTile.props.navigationCost);
currentTile = getNavigationGridTile(root.getX() + BC_TERRAIN_SETTINGS.totalSize, root.getY());
if (currentTile && !currentTile.isObstacle) {
currentTile.optionalTerrainTileRef.drawObject.tint = 0xff0000;
node = new PathFinderNode(currentTile.position, 1e16, 1e16, currentTile.movementCost);
if (!this._existsInClosedSet(node)) {
neighbors.push(node);
}
}
//west
currentTile = getTileAt(root.getX() - BC_TERRAIN_SETTINGS.totalSize, root.getY(), ChunkStorageTypes.TYPE_TERRAIN);
if(currentTile)
{
// currentTile.drawObject.tint = 0xff0000;
node = new PathFinderNode(new PointInt2D(currentTile.worldPosition.getX(), currentTile.worldPosition.getY()), 1e16, 1e16, currentTile.props.navigationCost);
currentTile = getNavigationGridTile(root.getX() - BC_TERRAIN_SETTINGS.totalSize, root.getY());
if (currentTile && !currentTile.isObstacle) {
currentTile.optionalTerrainTileRef.drawObject.tint = 0xff0000;
node = new PathFinderNode(currentTile.position, 1e16, 1e16, currentTile.movementCost);
if (!this._existsInClosedSet(node)) {
neighbors.push(node);
}
}
return neighbors;
}
@ -256,8 +255,8 @@ export class PathFinder {
let y1 = current.getY() - this._goal.getY();
let x2 = this._start.getX() - this._goal.getX();
let y2 = this._start.getY() - this._goal.getY();
let cross = Math.abs(x1*y2 - x2*y1);
return this._manhattanDistance(current, this._goal) * d + cross*0.001;
let cross = Math.abs(x1 * y2 - x2 * y1);
return this._manhattanDistance(current, this._goal) * d + cross * 0.001;
}
/**
@ -270,4 +269,15 @@ export class PathFinder {
}
return false;
}
/**
*
* @param {PathFinderNode} node
*/
_existsInOpenSet(node) {
for (const i of this._openSet) {
if (i.id === node.id) return true;
}
return false;
}
}

View File

@ -0,0 +1,110 @@
import { PointInt2D } from "../../Utils/Math.utils";
import { getWorldChunkAt, worldCoordinatesToChunkLocalCoordinates } from "../../WorldGeneration/WorldGen";
import { TerrainTile } from "../../WorldGeneration/WorldObjects/TerrainTile/TerrainTile";
export class NavigationGridTile
{
position = new PointInt2D();
movementCost = 0;
isObstacle = false;
/**
* @type {TerrainTile}
*/
optionalTerrainTileRef = null;
/**
*
* @param {PointInt2D} pos
* @param {Number} cost
* @param {Boolean} isObstacle
*/
constructor(pos, cost, isObstacle, ref = null)
{
this.position = pos;
this.movementCost = cost;
this.isObstacle = isObstacle;
this.optionalTerrainTileRef = ref;
}
}
/**
* @param {NavigationGridTile} tile
*/
function NavigationGridTileSettingsChangeFunctor(tile) {
}
export class NavigationGridChunk
{
/**
* @type {Map<String, NavigationGridTile>}
*/
_gridTiles = new Map();
/**
*
* @param {NavigationGridTile} element
* @param {String} id
*/
addToChunk(element, id)
{
this._gridTiles.set(id, element);
}
getFromChunkById(id)
{
return this._gridTiles.get(id);
}
getFromChunk(xWorld, yWorld)
{
let coords = worldCoordinatesToChunkLocalCoordinates(xWorld, yWorld);
return this._gridTiles.get(coords.x+"_"+coords.y);
}
/**
*
* @param {String} id
* @param {NavigationGridTileSettingsChangeFunctor} func
*/
changeNavigationGridTileSettings(id, func)
{
let t = this._gridTiles.get(id);
if(t)
{
func(t);
return true;
}
return false;
}
}
export class NavigationGrid
{
/**
*
* @param {Number} xWorld
* @param {Number} yWorld
*/
static getNavigationGridTile(xWorld, yWorld)
{
let chunk = getWorldChunkAt(xWorld, yWorld);
if(chunk)
{
return chunk.navigationGridChunk.getFromChunk(xWorld, yWorld);
}
return undefined;
}
}
export function getNavigationGridTile(xWorld, yWorld)
{
let chunk = getWorldChunkAt(xWorld, yWorld);
if(chunk)
{
return chunk.navigationGridChunk.getFromChunk(xWorld, yWorld);
}
return undefined;
}

View File

@ -3,6 +3,7 @@ import { SceneObject } from "../../SceneObjects/SceneObject";
import { worldCoordinatesToChunkIndexesCoordinates, worldCoordinatesToChunkLocalCoordinates } from "../WorldGen";
import { TerrainTile } from "../WorldObjects/TerrainTile/TerrainTile";
import { VegetationTile } from "../WorldObjects/VegetationTile/VegetationTile";
import { NavigationGridChunk } from "../../World/NavigationGrid/NavigationGrid";
export class ChunkStorageTypes
{
@ -31,6 +32,11 @@ export class WorldChunk extends SceneObject
*/
npcStorage = new Map();
/**
* @type NavigationGridChunk
*/
navigationGridChunk = new NavigationGridChunk();
/**
* property to handle chunk visibility
*/

View File

@ -1,20 +1,20 @@
import {Noise} from "noisejs";
import { Noise } from "noisejs";
import { Rectangle } from "../../pixi/pixi.mjs";
import { BC_CAMERA, BC_CHUNKS_SETTINGS, BC_CURRENT_SCENE, BC_SPRITES_SETTINGS, BC_TERRAIN_SETTINGS, PRNG } from "../GlobalVariables/GlobalVariables";
import { NumberCue, RGBColor } from "../Utils/DataTypes.utils";
import { ChunkStorageTypes, WorldChunk } from "./WorldChunk/WorldGenChunk";
import { TerrainTile, TerrainTileProps } from "./WorldObjects/TerrainTile/TerrainTile";
import { Point2D, clampNumber } from "../Utils/Math.utils";
import { Point2D, PointInt2D, clampNumber } from "../Utils/Math.utils";
import { addGameObjectToGameState } from "../GameState/GameState";
import { VegetationTile, VegetationTileProps } from "./WorldObjects/VegetationTile/VegetationTile";
import { WorldChunksVisibilityUpdater } from "./WorldChunksVisibilityUpdater/WorldChunksVisibilityUpdater";
import { NavigationGrid, NavigationGridChunk, NavigationGridTile } from "../World/NavigationGrid/NavigationGrid";
/**
* @type Map<String, WorldChunk>
*/
const WorldChunksStorage = new Map();
/* #### REWRITE PART START ####*/
const terrainSpriteList = {
0: { x: 21, y: 21 }, //water
@ -29,10 +29,10 @@ const terrainTypeList = {
3: "ter_stone", //stone
};
const terrainNavigationCostList = {
0: 1000, //water
1: 5, //sand
2: 3, //grass
3: 2, //stone
0: 100000000000, //water
1: 1, //sand
2: 1, //grass
3: 1, //stone
};
const grassVegetationSpriteList = {
0: { x: 10, y: 11 },
@ -89,14 +89,12 @@ export function worldCoordinatesToChunkIndexesCoordinates(x, y) {
export function worldCoordinatesToChunkLocalCoordinates(x, y) {
let ws = BC_TERRAIN_SETTINGS.tileSize * BC_TERRAIN_SETTINGS.scale;
let hs = BC_TERRAIN_SETTINGS.tileSize * BC_TERRAIN_SETTINGS.scale;
if (x < 0 && y >= 0)
{
if (x < 0 && y >= 0) {
return {
x: (BC_CHUNKS_SETTINGS.width - (Math.ceil(Math.abs(x) / ws) % BC_CHUNKS_SETTINGS.width)) % BC_CHUNKS_SETTINGS.width,
y: Math.floor(Math.abs(y) / hs) % BC_CHUNKS_SETTINGS.height,
};
}
else if (x < 0 && y < 0)
} else if (x < 0 && y < 0)
return {
x: (BC_CHUNKS_SETTINGS.width - (Math.ceil(Math.abs(x) / ws) % BC_CHUNKS_SETTINGS.width)) % BC_CHUNKS_SETTINGS.width,
y: (BC_CHUNKS_SETTINGS.height - (Math.ceil(Math.abs(y) / hs) % BC_CHUNKS_SETTINGS.height)) % BC_CHUNKS_SETTINGS.height,
@ -106,8 +104,7 @@ export function worldCoordinatesToChunkLocalCoordinates(x, y) {
x: Math.floor(Math.abs(x) / ws) % BC_CHUNKS_SETTINGS.width,
y: (BC_CHUNKS_SETTINGS.height - (Math.ceil(Math.abs(y) / hs) % BC_CHUNKS_SETTINGS.height)) % BC_CHUNKS_SETTINGS.height,
};
else
{
else {
return {
x: Math.floor(Math.abs(x) / ws) % BC_CHUNKS_SETTINGS.width,
y: Math.floor(Math.abs(y) / hs) % BC_CHUNKS_SETTINGS.height,
@ -117,7 +114,7 @@ export function worldCoordinatesToChunkLocalCoordinates(x, y) {
export function getWorldChunkAt(xWorld, yWorld) {
let t = worldCoordinatesToChunkIndex(xWorld, yWorld);
return WorldChunksStorage.get(t.x+"_"+t.y);
return WorldChunksStorage.get(t.x + "_" + t.y);
}
export function getWorldChunkById(id) {
@ -125,16 +122,15 @@ export function getWorldChunkById(id) {
}
/**
*
* @param {Number} xWorld
* @param {Number} yWorld
* @param {ChunkStorageTypes} storageType
* @returns
*
* @param {Number} xWorld
* @param {Number} yWorld
* @param {ChunkStorageTypes} storageType
* @returns
*/
export function getTileAt(xWorld, yWorld, storageType) {
let t = worldCoordinatesToChunkIndex(xWorld, yWorld);
let c = WorldChunksStorage.get(t.x+"_"+t.y);
if(!c) return undefined;
let c = getWorldChunkAt(xWorld, yWorld);
if (!c) return undefined;
return c.getFromChunk(xWorld, yWorld, storageType);
}
@ -154,7 +150,7 @@ let terrainCue = new NumberCue([0, 1, 2, 3, 3], [0.0, 0.2, 0.25, 0.9, 1.0]);
let terrainTintCue = new NumberCue([0.9, 1, 1, 0.95, 0.9, 1, 0.93, 1], [0.0, 0.45, 0.45, 0.5, 0.5, 0.9, 0.9, 1.0]);
/**
*
*
* @param {WorldChunk} chunk chunk to fill
* @param {Number} x ceiled coordinates of left-upper corner
* @param {Number} y ceiled coordinates of left-upper corner
@ -181,8 +177,11 @@ export function fillWorldGenChunk(chunk, x, y) {
let terrainTile = new TerrainTile(false);
terrainTile.spriteSheetPath = "assets/images/world/world_terrain_atlas.png";
terrainTile.frame = new Rectangle(terrainSpriteList[res].x, terrainSpriteList[res].y, 16, 16);
terrainTile.props = new TerrainTileProps(terrainTypeList[res], res*5, terrainNavigationCostList[res]);
terrainTile.worldPosition = new Point2D(i * BC_TERRAIN_SETTINGS.scale * BC_TERRAIN_SETTINGS.tileSize, j * BC_TERRAIN_SETTINGS.scale * BC_TERRAIN_SETTINGS.tileSize);
terrainTile.props = new TerrainTileProps(terrainTypeList[res], res * 5, terrainNavigationCostList[res]);
terrainTile.worldPosition = new Point2D(
i * BC_TERRAIN_SETTINGS.scale * BC_TERRAIN_SETTINGS.tileSize,
j * BC_TERRAIN_SETTINGS.scale * BC_TERRAIN_SETTINGS.tileSize
);
addGameObjectToGameState(terrainTile);
@ -191,7 +190,17 @@ export function fillWorldGenChunk(chunk, x, y) {
terrainTile.drawObject.position.set(16 * BC_SPRITES_SETTINGS.scale * ii, 16 * BC_SPRITES_SETTINGS.scale * jj);
terrainTile.drawObject.scale.set(BC_SPRITES_SETTINGS.scale, BC_SPRITES_SETTINGS.scale);
chunk.addToChunk(terrainTile, ChunkStorageTypes.TYPE_TERRAIN, ii+"_"+jj);
chunk.addToChunk(terrainTile, ChunkStorageTypes.TYPE_TERRAIN, ii + "_" + jj);
chunk.navigationGridChunk.addToChunk(
new NavigationGridTile(
new PointInt2D(i * BC_TERRAIN_SETTINGS.scale * BC_TERRAIN_SETTINGS.tileSize, j * BC_TERRAIN_SETTINGS.scale * BC_TERRAIN_SETTINGS.tileSize),
terrainNavigationCostList[res],
terrainNavigationCostList[res] > 100,
terrainTile
),
ii + "_" + jj
);
// NavigationGrid.addToNavigationGrid(new PointInt2D(ii, jj), terrainNavigationCostList[res], terrainNavigationCostList[res] > 100);
if (res === 2 && PRNG() > 0.9) {
let rv = Math.floor(PRNG() * 18);
@ -200,7 +209,10 @@ export function fillWorldGenChunk(chunk, x, y) {
vegetationTile.spriteSheetPath = "assets/images/world/vegetation_ts.png";
vegetationTile.frame = new Rectangle(16 * grassVegetationSpriteList[rv].x, 16 * grassVegetationSpriteList[rv].y, 16, 16);
vegetationTile.props = new VegetationTileProps("vegetation", grassVegResourcesList[rv]);
vegetationTile.worldPosition = new Point2D(i * BC_TERRAIN_SETTINGS.scale * BC_TERRAIN_SETTINGS.tileSize, j * BC_TERRAIN_SETTINGS.scale * BC_TERRAIN_SETTINGS.tileSize);
vegetationTile.worldPosition = new Point2D(
i * BC_TERRAIN_SETTINGS.scale * BC_TERRAIN_SETTINGS.tileSize,
j * BC_TERRAIN_SETTINGS.scale * BC_TERRAIN_SETTINGS.tileSize
);
addGameObjectToGameState(vegetationTile);
@ -209,7 +221,10 @@ export function fillWorldGenChunk(chunk, x, y) {
vegetationTile.drawObject.position.set(16 * BC_SPRITES_SETTINGS.scale * ii, 16 * BC_SPRITES_SETTINGS.scale * jj);
vegetationTile.drawObject.scale.set(BC_SPRITES_SETTINGS.scale, BC_SPRITES_SETTINGS.scale);
chunk.addToChunk(vegetationTile, ChunkStorageTypes.TYPE_VEGETATION, ii+"_"+jj);
chunk.addToChunk(vegetationTile, ChunkStorageTypes.TYPE_VEGETATION, ii + "_" + jj);
chunk.navigationGridChunk.changeNavigationGridTileSettings(ii + "_" + jj, (tile)=>{
tile.movementCost *= 1.5;
});
jj++;
continue;
@ -230,8 +245,9 @@ export function createFirstWorldChunks() {
let chunkId = chunkXCeiled + "_" + chunkYCeiled;
let chunk = new WorldChunk(false);
// chunk.navigationGridChunk = new NavigationGridChunk();
chunk.drawObject.position.set(w * chunkXCeiled, h * chunkYCeiled);
chunk.props = {id: chunkId};
chunk.props = { id: chunkId };
BC_CURRENT_SCENE.addObjectToSceneWithInitialization(chunk);
fillWorldGenChunk(chunk, chunkXCeiled, chunkYCeiled);
@ -241,4 +257,4 @@ export function createFirstWorldChunks() {
WorldChunksVisibilityUpdater.enableAutoWorldChunksGeneration = true;
}
/* #### REWRITE PART END ####*/
/* #### REWRITE PART END ####*/