some fixes
This commit is contained in:
parent
6df6956133
commit
c1831994a2
@ -19,7 +19,7 @@ import { ChunkStorageTypes } from "./WorldGeneration/WorldChunk/WorldGenChunk";
|
|||||||
import { WorldChunksVisibilityUpdater } from "./WorldGeneration/WorldChunksVisibilityUpdater/WorldChunksVisibilityUpdater";
|
import { WorldChunksVisibilityUpdater } from "./WorldGeneration/WorldChunksVisibilityUpdater/WorldChunksVisibilityUpdater";
|
||||||
import { NPCProto } from "./NPC/NPCProto/NPCProto";
|
import { NPCProto } from "./NPC/NPCProto/NPCProto";
|
||||||
import { NPCController } from "./NPC/NPCController/NPCController";
|
import { NPCController } from "./NPC/NPCController/NPCController";
|
||||||
import { PathFinder } from "./Utils/PathFinding.util";
|
import { PathFinder } from "./Utils/PathFinding.utils";
|
||||||
|
|
||||||
export function generateWorld() {
|
export function generateWorld() {
|
||||||
|
|
||||||
@ -65,7 +65,7 @@ function setupInGameSelector() {
|
|||||||
let t = screenToWorldCoordinates(e.data.global.x, e.data.global.y);
|
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()));
|
// 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);
|
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()),
|
npccc.moveTo(new PointInt2D(tile.worldPosition.getX(), tile.worldPosition.getY()),
|
||||||
(cb)=>{
|
(cb)=>{
|
||||||
console.log(cb);
|
console.log(cb);
|
||||||
@ -154,9 +154,4 @@ function startGame() {
|
|||||||
testNPC.drawObject.zIndex = 4;
|
testNPC.drawObject.zIndex = 4;
|
||||||
testNPC.drawObject.scale.set(BC_SPRITES_SETTINGS.scale, BC_SPRITES_SETTINGS.scale);
|
testNPC.drawObject.scale.set(BC_SPRITES_SETTINGS.scale, BC_SPRITES_SETTINGS.scale);
|
||||||
npccc = testNPCController;
|
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()));
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { GameObject } from "../../GameObject/GameObject";
|
import { GameObject } from "../../GameObject/GameObject";
|
||||||
import { PointInt2D } from "../../Utils/Math.utils";
|
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";
|
import { NPCProto } from "../NPCProto/NPCProto";
|
||||||
/**
|
/**
|
||||||
* NPCController defines NPC behavior. Many NPC can have same NPCController for the same behavior.
|
* 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 pf = new PathFinder();
|
||||||
let nPath = pf.search(new PointInt2D(this.controlledNPC.worldPosition.getX(), this.controlledNPC.worldPosition.getY()), position);
|
let nPath = pf.search(new PointInt2D(this.controlledNPC.worldPosition.getX(), this.controlledNPC.worldPosition.getY()), position);
|
||||||
|
console.log(nPath);
|
||||||
if(nPath.error)
|
if(nPath.error)
|
||||||
{
|
{
|
||||||
console.log("failed");
|
|
||||||
callback("failed");
|
callback("failed");
|
||||||
console.log(nPath);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
else if (nPath.result.path.length < 2)
|
else if (nPath.result.path.length < 2)
|
||||||
{
|
{
|
||||||
console.log("success");
|
|
||||||
callback("success");
|
callback("success");
|
||||||
console.log(nPath);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (let i = nPath.result.path.length-1; i > 0; i--) {
|
for (let i = nPath.result.path.length-1; i > 0; i--) {
|
||||||
@ -68,7 +65,6 @@ export class NPCController extends GameObject
|
|||||||
|
|
||||||
tick(ticker)
|
tick(ticker)
|
||||||
{
|
{
|
||||||
// console.log("object");
|
|
||||||
if(this.navigationInProgress)
|
if(this.navigationInProgress)
|
||||||
{
|
{
|
||||||
if(!this.navigationFollowMidPoint)
|
if(!this.navigationFollowMidPoint)
|
||||||
@ -78,7 +74,6 @@ export class NPCController extends GameObject
|
|||||||
{
|
{
|
||||||
this.navigationInProgress = false;
|
this.navigationInProgress = false;
|
||||||
this.navigationCallback("success");
|
this.navigationCallback("success");
|
||||||
console.log("success");
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -62,7 +62,14 @@ export class PointInt2D
|
|||||||
};
|
};
|
||||||
|
|
||||||
setX(x) {
|
setX(x) {
|
||||||
|
if(x >= 0)
|
||||||
|
{
|
||||||
this.#x = Math.floor(x);
|
this.#x = Math.floor(x);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
this.#x = Math.ceil(x);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
getY() {
|
getY() {
|
||||||
@ -70,7 +77,14 @@ export class PointInt2D
|
|||||||
};
|
};
|
||||||
|
|
||||||
setY(y) {
|
setY(y) {
|
||||||
|
if(y >= 0)
|
||||||
|
{
|
||||||
this.#y = Math.floor(y);
|
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.#x = Math.floor(this.#x * x);
|
||||||
this.#y = Math.floor(this.#y * x);
|
this.#y = Math.floor(this.#y * x);
|
||||||
}
|
}
|
||||||
else if (x < 0)
|
else
|
||||||
{
|
{
|
||||||
this.#x = Math.ceil(this.#x * x);
|
this.#x = Math.ceil(this.#x * x);
|
||||||
this.#y = Math.ceil(this.#y * x);
|
this.#y = Math.ceil(this.#y * x);
|
||||||
|
@ -3,6 +3,7 @@ import { ChunkStorageTypes } from "../WorldGeneration/WorldChunk/WorldGenChunk";
|
|||||||
import { getTileAt } from "../WorldGeneration/WorldGen";
|
import { getTileAt } from "../WorldGeneration/WorldGen";
|
||||||
import { PointInt2D } from "./Math.utils";
|
import { PointInt2D } from "./Math.utils";
|
||||||
import { SceneObject } from "../SceneObjects/SceneObject";
|
import { SceneObject } from "../SceneObjects/SceneObject";
|
||||||
|
import { getNavigationGridTile } from "../World/NavigationGrid/NavigationGrid";
|
||||||
|
|
||||||
class PathFinderNode {
|
class PathFinderNode {
|
||||||
position;
|
position;
|
||||||
@ -32,14 +33,12 @@ export class NavigationPath {
|
|||||||
/**
|
/**
|
||||||
* @param {Array<PointInt2D>} path
|
* @param {Array<PointInt2D>} path
|
||||||
*/
|
*/
|
||||||
constructor(path = [])
|
constructor(path = []) {
|
||||||
{
|
|
||||||
this.path = path;
|
this.path = path;
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
export class NavigationResult
|
export class NavigationResult {
|
||||||
{
|
|
||||||
/**
|
/**
|
||||||
* @type Boolean
|
* @type Boolean
|
||||||
*/
|
*/
|
||||||
@ -52,14 +51,21 @@ export class NavigationResult
|
|||||||
* @type NavigationPath | undefined
|
* @type NavigationPath | undefined
|
||||||
*/
|
*/
|
||||||
result;
|
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.error = error;
|
||||||
this.errorText = errorText;
|
this.errorText = errorText;
|
||||||
this.result = result;
|
this.result = result;
|
||||||
|
this.state = state;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
export class PathFinder {
|
export class PathFinder {
|
||||||
/**
|
/**
|
||||||
@ -169,10 +175,11 @@ export class PathFinder {
|
|||||||
* @param {PathFinderNode} current
|
* @param {PathFinderNode} current
|
||||||
*/
|
*/
|
||||||
_reconstructPath(cameFrom, current) {
|
_reconstructPath(cameFrom, current) {
|
||||||
// console.log(cameFrom);
|
|
||||||
let totalPath = [current.position];
|
let totalPath = [current.position];
|
||||||
|
// console.log(cameFrom);
|
||||||
let keys = [...cameFrom.keys()];
|
let keys = [...cameFrom.keys()];
|
||||||
while (keys.includes(current.id)) {
|
while (keys.includes(current.id)) {
|
||||||
|
if (current.id === cameFrom.get(current.id).id) break;
|
||||||
current = cameFrom.get(current.id);
|
current = cameFrom.get(current.id);
|
||||||
totalPath.push(current.position);
|
totalPath.push(current.position);
|
||||||
}
|
}
|
||||||
@ -188,51 +195,43 @@ export class PathFinder {
|
|||||||
* @type Array<PathFinderNode>
|
* @type Array<PathFinderNode>
|
||||||
*/
|
*/
|
||||||
let neighbors = new Array();
|
let neighbors = new Array();
|
||||||
/**
|
|
||||||
* @type SceneObject
|
|
||||||
*/
|
|
||||||
//north
|
//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;
|
let node;
|
||||||
if(currentTile)
|
if (currentTile && !currentTile.isObstacle) {
|
||||||
{
|
currentTile.optionalTerrainTileRef.drawObject.tint = 0xff0000;
|
||||||
// currentTile.drawObject.tint = 0xff0000;
|
node = new PathFinderNode(currentTile.position, 1e16, 1e16, currentTile.movementCost);
|
||||||
node = new PathFinderNode(new PointInt2D(currentTile.worldPosition.getX(), currentTile.worldPosition.getY()), 1e16, 1e16, currentTile.props.navigationCost);
|
|
||||||
if (!this._existsInClosedSet(node)) {
|
if (!this._existsInClosedSet(node)) {
|
||||||
neighbors.push(node);
|
neighbors.push(node);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//south
|
//south
|
||||||
currentTile = getTileAt(root.getX(), root.getY() - BC_TERRAIN_SETTINGS.totalSize, ChunkStorageTypes.TYPE_TERRAIN);
|
currentTile = getNavigationGridTile(root.getX(), root.getY() - BC_TERRAIN_SETTINGS.totalSize);
|
||||||
if(currentTile)
|
if (currentTile && !currentTile.isObstacle) {
|
||||||
{
|
currentTile.optionalTerrainTileRef.drawObject.tint = 0xff0000;
|
||||||
// currentTile.drawObject.tint = 0xff0000;
|
node = new PathFinderNode(currentTile.position, 1e16, 1e16, currentTile.movementCost);
|
||||||
node = new PathFinderNode(new PointInt2D(currentTile.worldPosition.getX(), currentTile.worldPosition.getY()), 1e16, 1e16, currentTile.props.navigationCost);
|
|
||||||
if (!this._existsInClosedSet(node)) {
|
if (!this._existsInClosedSet(node)) {
|
||||||
neighbors.push(node);
|
neighbors.push(node);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//east
|
//east
|
||||||
currentTile = getTileAt(root.getX() + BC_TERRAIN_SETTINGS.totalSize, root.getY(), ChunkStorageTypes.TYPE_TERRAIN);
|
currentTile = getNavigationGridTile(root.getX() + BC_TERRAIN_SETTINGS.totalSize, root.getY());
|
||||||
if(currentTile)
|
if (currentTile && !currentTile.isObstacle) {
|
||||||
{
|
currentTile.optionalTerrainTileRef.drawObject.tint = 0xff0000;
|
||||||
// currentTile.drawObject.tint = 0xff0000;
|
node = new PathFinderNode(currentTile.position, 1e16, 1e16, currentTile.movementCost);
|
||||||
node = new PathFinderNode(new PointInt2D(currentTile.worldPosition.getX(), currentTile.worldPosition.getY()), 1e16, 1e16, currentTile.props.navigationCost);
|
|
||||||
if (!this._existsInClosedSet(node)) {
|
if (!this._existsInClosedSet(node)) {
|
||||||
neighbors.push(node);
|
neighbors.push(node);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//west
|
//west
|
||||||
currentTile = getTileAt(root.getX() - BC_TERRAIN_SETTINGS.totalSize, root.getY(), ChunkStorageTypes.TYPE_TERRAIN);
|
currentTile = getNavigationGridTile(root.getX() - BC_TERRAIN_SETTINGS.totalSize, root.getY());
|
||||||
if(currentTile)
|
if (currentTile && !currentTile.isObstacle) {
|
||||||
{
|
currentTile.optionalTerrainTileRef.drawObject.tint = 0xff0000;
|
||||||
// currentTile.drawObject.tint = 0xff0000;
|
node = new PathFinderNode(currentTile.position, 1e16, 1e16, currentTile.movementCost);
|
||||||
node = new PathFinderNode(new PointInt2D(currentTile.worldPosition.getX(), currentTile.worldPosition.getY()), 1e16, 1e16, currentTile.props.navigationCost);
|
|
||||||
if (!this._existsInClosedSet(node)) {
|
if (!this._existsInClosedSet(node)) {
|
||||||
neighbors.push(node);
|
neighbors.push(node);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return neighbors;
|
return neighbors;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -270,4 +269,15 @@ export class PathFinder {
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {PathFinderNode} node
|
||||||
|
*/
|
||||||
|
_existsInOpenSet(node) {
|
||||||
|
for (const i of this._openSet) {
|
||||||
|
if (i.id === node.id) return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
110
src/Game/World/NavigationGrid/NavigationGrid.js
Normal file
110
src/Game/World/NavigationGrid/NavigationGrid.js
Normal 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;
|
||||||
|
}
|
@ -3,6 +3,7 @@ import { SceneObject } from "../../SceneObjects/SceneObject";
|
|||||||
import { worldCoordinatesToChunkIndexesCoordinates, worldCoordinatesToChunkLocalCoordinates } from "../WorldGen";
|
import { worldCoordinatesToChunkIndexesCoordinates, worldCoordinatesToChunkLocalCoordinates } from "../WorldGen";
|
||||||
import { TerrainTile } from "../WorldObjects/TerrainTile/TerrainTile";
|
import { TerrainTile } from "../WorldObjects/TerrainTile/TerrainTile";
|
||||||
import { VegetationTile } from "../WorldObjects/VegetationTile/VegetationTile";
|
import { VegetationTile } from "../WorldObjects/VegetationTile/VegetationTile";
|
||||||
|
import { NavigationGridChunk } from "../../World/NavigationGrid/NavigationGrid";
|
||||||
|
|
||||||
export class ChunkStorageTypes
|
export class ChunkStorageTypes
|
||||||
{
|
{
|
||||||
@ -31,6 +32,11 @@ export class WorldChunk extends SceneObject
|
|||||||
*/
|
*/
|
||||||
npcStorage = new Map();
|
npcStorage = new Map();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type NavigationGridChunk
|
||||||
|
*/
|
||||||
|
navigationGridChunk = new NavigationGridChunk();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* property to handle chunk visibility
|
* property to handle chunk visibility
|
||||||
*/
|
*/
|
||||||
|
@ -4,17 +4,17 @@ import { BC_CAMERA, BC_CHUNKS_SETTINGS, BC_CURRENT_SCENE, BC_SPRITES_SETTINGS, B
|
|||||||
import { NumberCue, RGBColor } from "../Utils/DataTypes.utils";
|
import { NumberCue, RGBColor } from "../Utils/DataTypes.utils";
|
||||||
import { ChunkStorageTypes, WorldChunk } from "./WorldChunk/WorldGenChunk";
|
import { ChunkStorageTypes, WorldChunk } from "./WorldChunk/WorldGenChunk";
|
||||||
import { TerrainTile, TerrainTileProps } from "./WorldObjects/TerrainTile/TerrainTile";
|
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 { addGameObjectToGameState } from "../GameState/GameState";
|
||||||
import { VegetationTile, VegetationTileProps } from "./WorldObjects/VegetationTile/VegetationTile";
|
import { VegetationTile, VegetationTileProps } from "./WorldObjects/VegetationTile/VegetationTile";
|
||||||
import { WorldChunksVisibilityUpdater } from "./WorldChunksVisibilityUpdater/WorldChunksVisibilityUpdater";
|
import { WorldChunksVisibilityUpdater } from "./WorldChunksVisibilityUpdater/WorldChunksVisibilityUpdater";
|
||||||
|
import { NavigationGrid, NavigationGridChunk, NavigationGridTile } from "../World/NavigationGrid/NavigationGrid";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @type Map<String, WorldChunk>
|
* @type Map<String, WorldChunk>
|
||||||
*/
|
*/
|
||||||
const WorldChunksStorage = new Map();
|
const WorldChunksStorage = new Map();
|
||||||
|
|
||||||
|
|
||||||
/* #### REWRITE PART START ####*/
|
/* #### REWRITE PART START ####*/
|
||||||
const terrainSpriteList = {
|
const terrainSpriteList = {
|
||||||
0: { x: 21, y: 21 }, //water
|
0: { x: 21, y: 21 }, //water
|
||||||
@ -29,10 +29,10 @@ const terrainTypeList = {
|
|||||||
3: "ter_stone", //stone
|
3: "ter_stone", //stone
|
||||||
};
|
};
|
||||||
const terrainNavigationCostList = {
|
const terrainNavigationCostList = {
|
||||||
0: 1000, //water
|
0: 100000000000, //water
|
||||||
1: 5, //sand
|
1: 1, //sand
|
||||||
2: 3, //grass
|
2: 1, //grass
|
||||||
3: 2, //stone
|
3: 1, //stone
|
||||||
};
|
};
|
||||||
const grassVegetationSpriteList = {
|
const grassVegetationSpriteList = {
|
||||||
0: { x: 10, y: 11 },
|
0: { x: 10, y: 11 },
|
||||||
@ -89,14 +89,12 @@ export function worldCoordinatesToChunkIndexesCoordinates(x, y) {
|
|||||||
export function worldCoordinatesToChunkLocalCoordinates(x, y) {
|
export function worldCoordinatesToChunkLocalCoordinates(x, y) {
|
||||||
let ws = BC_TERRAIN_SETTINGS.tileSize * BC_TERRAIN_SETTINGS.scale;
|
let ws = BC_TERRAIN_SETTINGS.tileSize * BC_TERRAIN_SETTINGS.scale;
|
||||||
let hs = 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 {
|
return {
|
||||||
x: (BC_CHUNKS_SETTINGS.width - (Math.ceil(Math.abs(x) / ws) % BC_CHUNKS_SETTINGS.width)) % BC_CHUNKS_SETTINGS.width,
|
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,
|
y: Math.floor(Math.abs(y) / hs) % BC_CHUNKS_SETTINGS.height,
|
||||||
};
|
};
|
||||||
}
|
} else if (x < 0 && y < 0)
|
||||||
else if (x < 0 && y < 0)
|
|
||||||
return {
|
return {
|
||||||
x: (BC_CHUNKS_SETTINGS.width - (Math.ceil(Math.abs(x) / ws) % BC_CHUNKS_SETTINGS.width)) % BC_CHUNKS_SETTINGS.width,
|
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,
|
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,
|
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,
|
y: (BC_CHUNKS_SETTINGS.height - (Math.ceil(Math.abs(y) / hs) % BC_CHUNKS_SETTINGS.height)) % BC_CHUNKS_SETTINGS.height,
|
||||||
};
|
};
|
||||||
else
|
else {
|
||||||
{
|
|
||||||
return {
|
return {
|
||||||
x: Math.floor(Math.abs(x) / ws) % BC_CHUNKS_SETTINGS.width,
|
x: Math.floor(Math.abs(x) / ws) % BC_CHUNKS_SETTINGS.width,
|
||||||
y: Math.floor(Math.abs(y) / hs) % BC_CHUNKS_SETTINGS.height,
|
y: Math.floor(Math.abs(y) / hs) % BC_CHUNKS_SETTINGS.height,
|
||||||
@ -132,8 +129,7 @@ export function getWorldChunkById(id) {
|
|||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
export function getTileAt(xWorld, yWorld, storageType) {
|
export function getTileAt(xWorld, yWorld, storageType) {
|
||||||
let t = worldCoordinatesToChunkIndex(xWorld, yWorld);
|
let c = getWorldChunkAt(xWorld, yWorld);
|
||||||
let c = WorldChunksStorage.get(t.x+"_"+t.y);
|
|
||||||
if (!c) return undefined;
|
if (!c) return undefined;
|
||||||
return c.getFromChunk(xWorld, yWorld, storageType);
|
return c.getFromChunk(xWorld, yWorld, storageType);
|
||||||
}
|
}
|
||||||
@ -182,7 +178,10 @@ export function fillWorldGenChunk(chunk, x, y) {
|
|||||||
terrainTile.spriteSheetPath = "assets/images/world/world_terrain_atlas.png";
|
terrainTile.spriteSheetPath = "assets/images/world/world_terrain_atlas.png";
|
||||||
terrainTile.frame = new Rectangle(terrainSpriteList[res].x, terrainSpriteList[res].y, 16, 16);
|
terrainTile.frame = new Rectangle(terrainSpriteList[res].x, terrainSpriteList[res].y, 16, 16);
|
||||||
terrainTile.props = new TerrainTileProps(terrainTypeList[res], res * 5, terrainNavigationCostList[res]);
|
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.worldPosition = new Point2D(
|
||||||
|
i * BC_TERRAIN_SETTINGS.scale * BC_TERRAIN_SETTINGS.tileSize,
|
||||||
|
j * BC_TERRAIN_SETTINGS.scale * BC_TERRAIN_SETTINGS.tileSize
|
||||||
|
);
|
||||||
|
|
||||||
addGameObjectToGameState(terrainTile);
|
addGameObjectToGameState(terrainTile);
|
||||||
|
|
||||||
@ -192,6 +191,16 @@ export function fillWorldGenChunk(chunk, x, y) {
|
|||||||
terrainTile.drawObject.scale.set(BC_SPRITES_SETTINGS.scale, BC_SPRITES_SETTINGS.scale);
|
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) {
|
if (res === 2 && PRNG() > 0.9) {
|
||||||
let rv = Math.floor(PRNG() * 18);
|
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.spriteSheetPath = "assets/images/world/vegetation_ts.png";
|
||||||
vegetationTile.frame = new Rectangle(16 * grassVegetationSpriteList[rv].x, 16 * grassVegetationSpriteList[rv].y, 16, 16);
|
vegetationTile.frame = new Rectangle(16 * grassVegetationSpriteList[rv].x, 16 * grassVegetationSpriteList[rv].y, 16, 16);
|
||||||
vegetationTile.props = new VegetationTileProps("vegetation", grassVegResourcesList[rv]);
|
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);
|
addGameObjectToGameState(vegetationTile);
|
||||||
|
|
||||||
@ -210,6 +222,9 @@ export function fillWorldGenChunk(chunk, x, y) {
|
|||||||
vegetationTile.drawObject.scale.set(BC_SPRITES_SETTINGS.scale, BC_SPRITES_SETTINGS.scale);
|
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++;
|
jj++;
|
||||||
continue;
|
continue;
|
||||||
@ -230,6 +245,7 @@ export function createFirstWorldChunks() {
|
|||||||
let chunkId = chunkXCeiled + "_" + chunkYCeiled;
|
let chunkId = chunkXCeiled + "_" + chunkYCeiled;
|
||||||
|
|
||||||
let chunk = new WorldChunk(false);
|
let chunk = new WorldChunk(false);
|
||||||
|
// chunk.navigationGridChunk = new NavigationGridChunk();
|
||||||
chunk.drawObject.position.set(w * chunkXCeiled, h * chunkYCeiled);
|
chunk.drawObject.position.set(w * chunkXCeiled, h * chunkYCeiled);
|
||||||
chunk.props = { id: chunkId };
|
chunk.props = { id: chunkId };
|
||||||
BC_CURRENT_SCENE.addObjectToSceneWithInitialization(chunk);
|
BC_CURRENT_SCENE.addObjectToSceneWithInitialization(chunk);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user