From 6e33c8a99c55fe04d07517d5a5db3e290beb0fb3 Mon Sep 17 00:00:00 2001 From: TorgaW Date: Wed, 17 Apr 2024 01:48:48 +0300 Subject: [PATCH] removed a lot of logs and started A* implementation --- .vscode/settings.json | 3 +- src/Game/Game.js | 42 ++++++-- src/Game/GameObject/GameObject.js | 2 +- src/Game/GameScene/GameScene.js | 1 + src/Game/GameState/GameState.js | 2 - src/Game/InputController/InputController.js | 8 -- src/Game/NPC/NPCController/NPCController.js | 29 ++++++ src/Game/NPC/NPCProto/NPCProto.js | 60 ++++++++++++ src/Game/SceneObjects/SceneObject.js | 10 ++ src/Game/Sound/Sound.js | 1 - src/Game/Utils/Math.utils.js | 68 +++++++++++-- src/Game/Utils/PathFinding.util.js | 95 +++++++++++++++++++ src/Game/World/DayNightCycle.js | 5 - .../WorldChunk/WorldGenChunk.js | 6 +- .../WorldChunksVisibilityUpdater.js | 3 - src/Game/WorldGeneration/WorldGen.js | 71 +++++++------- .../WorldObjects/TerrainTile/TerrainTile.js | 10 +- src/Test.js | 2 - 18 files changed, 345 insertions(+), 73 deletions(-) create mode 100644 src/Game/NPC/NPCController/NPCController.js create mode 100644 src/Game/NPC/NPCProto/NPCProto.js create mode 100644 src/Game/Utils/PathFinding.util.js diff --git a/.vscode/settings.json b/.vscode/settings.json index 54d7873..b8f3e36 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -2,6 +2,7 @@ "cSpell.words": [ "Alea", "amogus", - "PIXI" + "PIXI", + "spritesheet" ] } \ No newline at end of file diff --git a/src/Game/Game.js b/src/Game/Game.js index 9f9131c..fde8456 100644 --- a/src/Game/Game.js +++ b/src/Game/Game.js @@ -5,18 +5,21 @@ import { getSpriteFromAtlas } from "./Utils/Sprites.utils"; import { profileFPS } from "./Profiler/Profiler"; import { createKeyboardBinding, inputControllerTick } from "./InputController/InputController"; import { BC_APP, BC_BUILDING_PLACEHOLDERS, BC_CAMERA, BC_CURRENT_SCENE, BC_SPRITES_SETTINGS, BC_VIEWPORT, BC_WORLD, PRNG, setBC_APP, setBC_CURRENT_SCENE, setBC_NPC_LAYER, setBC_SELECTION, setBC_VIEWPORT, setBC_WORLD } from "./GlobalVariables/GlobalVariables"; -import { clampNumber, interpolate, } from "./Utils/Math.utils"; +import { Point2D, PointInt2D, clampNumber, interpolate, } from "./Utils/Math.utils"; import { calculateViewportFromCamera, moveHorizontally, moveVertically, screenToWorldCoordinates } from "./Camera/Camera"; import { handleBuildingsIncome, incBuildingCount } from "./Buildings/Buildings"; import { handleDayNightCycle } from "./World/DayNightCycle"; import { ambientDay, ambientMusic, ambientNight, handleSounds } from "./Sound/Sound"; -import { gameStateObjectsCleaner } from "./GameState/GameState"; +import { addGameObjectToGameState, gameStateObjectsCleaner } from "./GameState/GameState"; import { tickHandler } from "./TickHandler/TickHandler"; import { GameScene } from "./GameScene/GameScene"; -import { createFirstWorldChunks, getWorldChunkAt } from "./WorldGeneration/WorldGen"; +import { createFirstWorldChunks, getTileAt, getWorldChunkAt, worldCoordinatesToChunkIndex } from "./WorldGeneration/WorldGen"; 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"; export function generateWorld() { @@ -56,6 +59,13 @@ function setupInGameSelector() { t.y = BC_SPRITES_SETTINGS.defaultSize * BC_SPRITES_SETTINGS.scale * Math.floor(t.y / (BC_SPRITES_SETTINGS.defaultSize * BC_SPRITES_SETTINGS.scale)); sprite0.position.set(t.x, t.y); }; + + // BC_VIEWPORT.onpointerdown = (e) => + // { + // 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())); + // } + } export async function initGame() { @@ -97,9 +107,10 @@ export async function initGame() { app.ticker.add(inputControllerTick); app.ticker.add(calculateViewportFromCamera); - // console.log(WorldChunksVisibilityUpdater.handleWorldChunksVisibility); - app.ticker.add(()=>{WorldChunksVisibilityUpdater.handleWorldChunksVisibility();}); - app.ticker.add(()=>{WorldChunksVisibilityUpdater.handleWorldChunkFilling();}); + //static functions shitty binding + app.ticker.add(WorldChunksVisibilityUpdater.handleWorldChunksVisibility.bind(WorldChunksVisibilityUpdater)); + app.ticker.add(WorldChunksVisibilityUpdater.handleWorldChunkFilling.bind(WorldChunksVisibilityUpdater)); + //end of shit app.ticker.add(profileFPS); app.ticker.add(handleBuildingsIncome); @@ -107,8 +118,11 @@ export async function initGame() { createKeyboardBinding("KeyS", "Hold", [moveVertically]); createKeyboardBinding("KeyA", "Hold", [moveHorizontally]); createKeyboardBinding("KeyD", "Hold", [moveHorizontally]); + } +let npccc; + function startGame() { setupGlobalInput(); setupInGameSelector(); @@ -117,9 +131,23 @@ function startGame() { ambientDay.play(); ambientNight.play(); ambientMusic.play(); - // addNPCToWorld(BC_CAMERA.position.x, BC_CAMERA.position.y, {type: "slave"}); BC_APP.ticker.add(gameStateObjectsCleaner); BC_APP.ticker.add(tickHandler); createFirstWorldChunks(); + + let testNPCController = new NPCController(); + let testNPC = new NPCProto(true, testNPCController); + testNPC.spriteSheetPath = "assets/images/characters/char0.png"; + testNPC.frame = new PIXI.Rectangle(0, 0, 16, 16); + testNPC.controller = testNPCController; + testNPCController.controlledNPC = testNPC; + addGameObjectToGameState(testNPCController); + BC_CURRENT_SCENE.addObjectToSceneWithInitialization(testNPC); + testNPC.drawObject.zIndex = 4; + testNPC.drawObject.scale.set(BC_SPRITES_SETTINGS.scale, BC_SPRITES_SETTINGS.scale); + npccc = testNPCController; + let pf = new PathFinder(); + let dest = pf.search(new PointInt2D(), new PointInt2D(10, 10)); + testNPCController.moveTo(new Point2D(dest.worldPosition.getX(), dest.worldPosition.getY())); } diff --git a/src/Game/GameObject/GameObject.js b/src/Game/GameObject/GameObject.js index ae243e8..36bdb9f 100644 --- a/src/Game/GameObject/GameObject.js +++ b/src/Game/GameObject/GameObject.js @@ -15,7 +15,7 @@ export class GameObject { // markedAsInitialized = false; /** - * GameObject id + * * @param {Boolean} tickAble */ constructor(tickAble = false) diff --git a/src/Game/GameScene/GameScene.js b/src/Game/GameScene/GameScene.js index 12aa5c5..1207779 100644 --- a/src/Game/GameScene/GameScene.js +++ b/src/Game/GameScene/GameScene.js @@ -17,6 +17,7 @@ export class GameScene { * @param {SceneObject} sceneObject */ addObjectToScene(sceneObject) { + sceneObject.onSpawn(); this.#SceneObjects.set(sceneObject.minorId, sceneObject); BC_VIEWPORT.addChild(sceneObject.drawObject); }; diff --git a/src/Game/GameState/GameState.js b/src/Game/GameState/GameState.js index 0a93dc4..ce9f4af 100644 --- a/src/Game/GameState/GameState.js +++ b/src/Game/GameState/GameState.js @@ -50,8 +50,6 @@ export function gameStateObjectsCleaner() { counter++; } CleaningQueue = new Array(); - // console.log("removed: " + counter + " objects; "+"Total tick objects: " + TickGameObjects.size + "; Total objects: " + GameObjects.size); - // console.log(TickGameObjects); } diff --git a/src/Game/InputController/InputController.js b/src/Game/InputController/InputController.js index 5fa0fbb..dfa53c5 100644 --- a/src/Game/InputController/InputController.js +++ b/src/Game/InputController/InputController.js @@ -8,12 +8,10 @@ const _keyboard_current_states = {}; window.addEventListener("keydown", (event) => { _keyboard_current_states[event.code] = true; - // console.log(_keyboard_current_states); }); window.addEventListener("keyup", (event) => { _keyboard_current_states[event.code] = false; - // console.log(_keyboard_current_states); }); /** @@ -69,9 +67,3 @@ export function inputControllerTick(deltaTime) { } } } - -// export function _debugKeyboardStates() -// { -// console.log(_keyboard_current_states); -// console.log(_keyboard_controller_bindings); -// } diff --git a/src/Game/NPC/NPCController/NPCController.js b/src/Game/NPC/NPCController/NPCController.js new file mode 100644 index 0000000..8b6a63f --- /dev/null +++ b/src/Game/NPC/NPCController/NPCController.js @@ -0,0 +1,29 @@ +import { GameObject } from "../../GameObject/GameObject"; +import { Point2D } from "../../Utils/Math.utils"; +import { NPCProto } from "../NPCProto/NPCProto"; +/** + * NPCController defines NPC behavior. Many NPC can have same NPCController for the same behavior. + */ +export class NPCController extends GameObject +{ + /** + * NPC controlled by this controller + * @type NPCProto + */ + controlledNPC = null; + + constructor(tickAble = false) + { + super(tickAble); + } + + /** + * moves NPC to position + * @param {Point2D} position + */ + moveTo(position) + { + this.controlledNPC.currentPosition = position; + this.controlledNPC.currentPosition = position; + } +}; \ No newline at end of file diff --git a/src/Game/NPC/NPCProto/NPCProto.js b/src/Game/NPC/NPCProto/NPCProto.js new file mode 100644 index 0000000..7c1c933 --- /dev/null +++ b/src/Game/NPC/NPCProto/NPCProto.js @@ -0,0 +1,60 @@ +import { Rectangle } from "../../../pixi/pixi.mjs"; +import { SceneObject } from "../../SceneObjects/SceneObject"; +import { Point2D, interpolate } from "../../Utils/Math.utils"; +import { getSpriteFromAtlas } from "../../Utils/Sprites.utils"; +import { NPCController } from "../NPCController/NPCController"; + +export class NPCProto extends SceneObject +{ + /** + * NPCController ref + * @type NPCController + */ + controller = null; + + /** + * NPC position in world + */ + currentPosition = new Point2D(); + + _positionInterpolationSpeed = 10; + + /** + * path to NPC spritesheet + */ + spriteSheetPath = ""; + + /** + * frame to cut from spritesheet + */ + frame = new Rectangle(); + + /** + * creates new NPC object + * @param {Boolean} tickAble + * @param {NPCController} controller + */ + constructor(tickAble = true, controller) + { + super(tickAble); + this.controller = controller; + }; + + onInit() + { + super.onInit(); + this.drawObject = getSpriteFromAtlas(this.spriteSheetPath, this.frame); + }; + + tick(ticker) + { + this._positionInterpolation(ticker.deltaMS / 1000 * this._positionInterpolationSpeed); + }; + + _positionInterpolation(delta) + { + let {x, y} = this.drawObject.position; + this.drawObject.position.set(interpolate(x, this.currentPosition.getX(), delta), interpolate(y, this.currentPosition.getY(), delta)); + }; + +}; \ No newline at end of file diff --git a/src/Game/SceneObjects/SceneObject.js b/src/Game/SceneObjects/SceneObject.js index 021ee75..068c305 100644 --- a/src/Game/SceneObjects/SceneObject.js +++ b/src/Game/SceneObjects/SceneObject.js @@ -1,5 +1,6 @@ import { GameObject } from "../GameObject/GameObject"; import { Container } from "../../pixi/pixi.mjs"; +import { Point2D } from "../Utils/Math.utils"; export class SceneObject extends GameObject { /** @@ -13,6 +14,15 @@ export class SceneObject extends GameObject { */ props = {}; + /** + * world position for scene object + */ + worldPosition = new Point2D(); + + /** + * + * @param {Boolean} tickAble + */ constructor(tickAble) { super(tickAble); diff --git a/src/Game/Sound/Sound.js b/src/Game/Sound/Sound.js index 1c32515..4fd3459 100644 --- a/src/Game/Sound/Sound.js +++ b/src/Game/Sound/Sound.js @@ -61,7 +61,6 @@ let nightAmbientSoundCue = new NumberCue( ); export function handleSounds(tick) { - // console.log(tick); let p = getDayPhase(); ambientDay.volume(dayAmbientSoundCue.getValueAt(p) * globalSoundVolume * ambientSoundVolume * 0.6); ambientNight.volume(nightAmbientSoundCue.getValueAt(p) * globalSoundVolume * ambientSoundVolume * 0.6); diff --git a/src/Game/Utils/Math.utils.js b/src/Game/Utils/Math.utils.js index a6839b1..e4977a2 100644 --- a/src/Game/Utils/Math.utils.js +++ b/src/Game/Utils/Math.utils.js @@ -2,32 +2,86 @@ import { PRNG } from "../GlobalVariables/GlobalVariables"; export class Point2D { + #x = 0; + #y = 0; /** * Point2D object * @param {Number} x * @param {Number} y */ - constructor(x = 0.0, y = 0.0) + constructor(x = 0, y = 0) { - this._x = x; - this._y = y; + this.#x = Math.floor(x); + this.#y = Math.floor(y); } getX() { - return this._x; + return this.#x; }; setX(x) { - this._x = x; + this.#x = x; }; getY() { - return this._y; + return this.#y; }; setY(y) { - this._y = y; + this.#y = y; }; + + /** + * + * @param {Point2D} a + * @param {Point2D} b + */ + static isEqual(a, b) + { + return (a.getX() === b.getX() && a.getY() === b.getY()); + } +} + +export class PointInt2D +{ + #x = 0; + #y = 0; + /** + * Point2D object + * @param {Number} x + * @param {Number} y + */ + constructor(x = 0, y = 0) + { + this.#x = x; + this.#y = y; + } + + getX() { + return this.#x; + }; + + setX(x) { + this.#x = Math.floor(x); + }; + + getY() { + return this.#y; + }; + + setY(y) { + this.#y = Math.floor(y); + }; + + /** + * + * @param {PointInt2D} a + * @param {PointInt2D} b + */ + static isEqual(a, b) + { + return (a.getX() === b.getX() && a.getY() === b.getY()); + } } /** diff --git a/src/Game/Utils/PathFinding.util.js b/src/Game/Utils/PathFinding.util.js new file mode 100644 index 0000000..2d4d650 --- /dev/null +++ b/src/Game/Utils/PathFinding.util.js @@ -0,0 +1,95 @@ +import { BC_TERRAIN_SETTINGS } from "../GlobalVariables/GlobalVariables"; +import { ChunkStorageTypes } from "../WorldGeneration/WorldChunk/WorldGenChunk"; +import { getTileAt } from "../WorldGeneration/WorldGen"; +import { PointInt2D } from "./Math.utils"; +import { TerrainTile } from "../WorldGeneration/WorldObjects/TerrainTile/TerrainTile"; + +class PathFinderNode +{ + position; + fScore; + neighbors; + + /** + * + * @param {PointInt2D} position + * @param {Number} fScore + */ + constructor(position, fScore) + { + this.position = position; + this.fScore = fScore; + } +}; + +export class PathFinder +{ + /** + * @type Array + */ + _openSet = new Array(); + _closedSet = new Array(); + + /** + * + * @param {PointInt2D} start + * @param {PointInt2D} goal + */ + search(start, goal) + { + this._openSet.push(new PathFinderNode(start, 0)); + + //code + + let currentNode = new PathFinderNode(new PointInt2D(), 0); + /** + * @type Map + */ + let gScores = new Map(); + /** + * @type Map + */ + let fScores = new Map(); + + let minFScoreNodeIndex = 0; + //START LOOP WOOOOAAAW + while (this._openSet.length > 0) { + //find node with min f score + //better to rewrite it to priority queue + for (let i = 0; i < this._openSet.length; i++) { + if(this._openSet[i] < this._openSet[minFScoreNodeIndex]) + minFScoreNodeIndex = i; + } + //wow! node is found and set!! + currentNode = this._openSet[minFScoreNodeIndex]; + + if(PointInt2D.isEqual(currentNode.position, goal)) + { + //wow!!! we have found an end of the path!!! this is so cool!!! + return 1111; //return something weird stuff + } + + //and now we must delete this node... what a sad situation... + this._openSet.splice(minFScoreNodeIndex, 1); + //but wait! we add it to closed set! nice!!! (why we are doing this??? idk... okay) + this._closedSet.push(currentNode); + + /** + * @type TerrainTile + */ + let neighbors = this._getNeighbors(currentNode.position); + return neighbors; + } + } + + /** + * + * @param {PointInt2D} root + */ + _getNeighbors(root) + { + let neighborNorth = getTileAt(root.getX(), root.getY() - BC_TERRAIN_SETTINGS.tileSize*BC_TERRAIN_SETTINGS.scale, ChunkStorageTypes.TYPE_TERRAIN); + return neighborNorth; + //TODO + } +}; \ No newline at end of file diff --git a/src/Game/World/DayNightCycle.js b/src/Game/World/DayNightCycle.js index 225f6f9..7e6ece9 100644 --- a/src/Game/World/DayNightCycle.js +++ b/src/Game/World/DayNightCycle.js @@ -40,11 +40,6 @@ export function handleDayNightCycle(tick) { }); currentColor = dayColorsCue.getColorAt(getDayPhase()); - // currentColor = dayColor.multiplyByNumber(t); - // console.log(((Math.floor(timeElapsed) % (gameSecondsInGameMinute * gameMinutesInGameHour * gameHoursInGameDay)) / (gameSecondsInGameMinute * gameMinutesInGameHour * gameHoursInGameDay))); - // console.log(currentColor.toHex()); - // console.log(((Math.floor(timeElapsed) % (gameSecondsInGameMinute * gameMinutesInGameHour * gameHoursInGameDay)) / (gameSecondsInGameMinute * gameMinutesInGameHour * gameHoursInGameDay))); - // currentColor.multiplyByNumber(((Math.floor(timeElapsed) % (gameSecondsInGameMinute * gameMinutesInGameHour * gameHoursInGameDay)) / (gameSecondsInGameMinute * gameMinutesInGameHour * gameHoursInGameDay))); BC_VIEWPORT.tint = currentColor.toNumber(); } diff --git a/src/Game/WorldGeneration/WorldChunk/WorldGenChunk.js b/src/Game/WorldGeneration/WorldChunk/WorldGenChunk.js index a2839c3..a638570 100644 --- a/src/Game/WorldGeneration/WorldChunk/WorldGenChunk.js +++ b/src/Game/WorldGeneration/WorldChunk/WorldGenChunk.js @@ -1,6 +1,6 @@ import { Container } from "../../../pixi/pixi.mjs"; import { SceneObject } from "../../SceneObjects/SceneObject"; -import { worldCoordinatesToChunkLocalCoordinates } from "../WorldGen"; +import { worldCoordinatesToChunkIndexesCoordinates, worldCoordinatesToChunkLocalCoordinates } from "../WorldGen"; import { TerrainTile } from "../WorldObjects/TerrainTile/TerrainTile"; import { VegetationTile } from "../WorldObjects/VegetationTile/VegetationTile"; @@ -26,6 +26,10 @@ export class WorldChunk extends SceneObject * @type Map */ buildingsStorage = new Map(); + /** + * @type Map + */ + npcStorage = new Map(); /** * property to handle chunk visibility diff --git a/src/Game/WorldGeneration/WorldChunksVisibilityUpdater/WorldChunksVisibilityUpdater.js b/src/Game/WorldGeneration/WorldChunksVisibilityUpdater/WorldChunksVisibilityUpdater.js index c1bf5c4..c0bbc59 100644 --- a/src/Game/WorldGeneration/WorldChunksVisibilityUpdater/WorldChunksVisibilityUpdater.js +++ b/src/Game/WorldGeneration/WorldChunksVisibilityUpdater/WorldChunksVisibilityUpdater.js @@ -29,7 +29,6 @@ export class WorldChunksVisibilityUpdater { * Ticker function */ static handleWorldChunkFilling() { - // console.log(tick.deltaMS); if (this._currentIndex < 0) return; fillWorldGenChunk( this._chunksFillingQueue[this._currentIndex].chunk, @@ -50,7 +49,6 @@ export class WorldChunksVisibilityUpdater { let cx = Math.floor(BC_CAMERA.position.x / w); let cy = Math.floor(BC_CAMERA.position.y / h); let chunkId = cx + "_" + cy; - // console.log(this._visibleChunks); if ((this._visibleChunks.has(chunkId) && !this._visibleChunks.get(chunkId).centralChunk) || !this._visibleChunks.has(chunkId)) { for (const visChunk of this._visibleChunks) { visChunk[1].drawObject.visible = false; @@ -65,7 +63,6 @@ export class WorldChunksVisibilityUpdater { this._visibleChunks.set(t_chunkId, wChunk); wChunk.drawObject.visible = true; } else if (this.enableAutoWorldChunksGeneration) { - // console.log(t_chunkId); let newChunk = new WorldChunk(false); BC_CURRENT_SCENE.addObjectToSceneWithInitialization(newChunk); newChunk.drawObject.position.set(w * (cx + i), h * (cy + j)); diff --git a/src/Game/WorldGeneration/WorldGen.js b/src/Game/WorldGeneration/WorldGen.js index 081278a..3f11402 100644 --- a/src/Game/WorldGeneration/WorldGen.js +++ b/src/Game/WorldGeneration/WorldGen.js @@ -4,7 +4,7 @@ import { BC_CAMERA, BC_CHUNKS_SETTINGS, BC_CURRENT_SCENE, BC_SPRITES_SETTINGS, B import { NumberCue, RGBColor } from "../Utils/DataTypes.utils"; import { ChunkStorageTypes, WorldChunk } from "./WorldChunk/WorldGenChunk"; import { TerrainTile, TerrainTileProps } from "./WorldObjects/TerrainTile/TerrainTile"; -import { clampNumber } from "../Utils/Math.utils"; +import { Point2D, clampNumber } from "../Utils/Math.utils"; import { addGameObjectToGameState } from "../GameState/GameState"; import { VegetationTile, VegetationTileProps } from "./WorldObjects/VegetationTile/VegetationTile"; import { WorldChunksVisibilityUpdater } from "./WorldChunksVisibilityUpdater/WorldChunksVisibilityUpdater"; @@ -28,6 +28,12 @@ const terrainTypeList = { 2: "ter_grass", //grass 3: "ter_stone", //stone }; +const terrainNavigationCostList = { + 0: 1000, //water + 1: 5, //sand + 2: 3, //grass + 3: 2, //stone +}; const grassVegetationSpriteList = { 0: { x: 10, y: 11 }, 1: { x: 11, y: 11 }, @@ -79,29 +85,34 @@ export function worldCoordinatesToChunkIndexesCoordinates(x, y) { let hs = BC_TERRAIN_SETTINGS.tileSize * BC_TERRAIN_SETTINGS.scale; return { x: Math.floor(x / ws), y: Math.floor(y / hs) }; } + 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) + { return { - x: BC_CHUNKS_SETTINGS.width - (Math.floor(Math.abs(x) / ws) % BC_CHUNKS_SETTINGS.width) - 1, + 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) return { - x: BC_CHUNKS_SETTINGS.width - (Math.floor(Math.abs(x) / ws) % BC_CHUNKS_SETTINGS.width) - 1, - y: BC_CHUNKS_SETTINGS.height - (Math.floor(Math.abs(y) / hs) % BC_CHUNKS_SETTINGS.height) - 1, + 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, }; - else if (x > 0 && y < 0) + else if (x >= 0 && y < 0) return { x: Math.floor(Math.abs(x) / ws) % BC_CHUNKS_SETTINGS.width, - y: BC_CHUNKS_SETTINGS.height - (Math.floor(Math.abs(y) / hs) % BC_CHUNKS_SETTINGS.height) - 1, + y: (BC_CHUNKS_SETTINGS.height - (Math.ceil(Math.abs(y) / hs) % BC_CHUNKS_SETTINGS.height)) % BC_CHUNKS_SETTINGS.height, }; else + { return { x: Math.floor(Math.abs(x) / ws) % BC_CHUNKS_SETTINGS.width, y: Math.floor(Math.abs(y) / hs) % BC_CHUNKS_SETTINGS.height, }; + } } export function getWorldChunkAt(xWorld, yWorld) { @@ -113,6 +124,20 @@ export function getWorldChunkById(id) { return WorldChunksStorage.get(id); } +/** + * + * @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; + return c.getFromChunk(xWorld, yWorld, storageType); +} + export function worldChunkExists(id) { return WorldChunksStorage.has(id); } @@ -154,10 +179,10 @@ export function fillWorldGenChunk(chunk, x, y) { res = Math.floor(terrainCue.getValueAt(res)); let terrainTile = new TerrainTile(false); - // console.log(terrainTile.isTickEnabled()); 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); + 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); @@ -166,16 +191,16 @@ 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, i+"_"+j); + chunk.addToChunk(terrainTile, ChunkStorageTypes.TYPE_TERRAIN, ii+"_"+jj); if (res === 2 && PRNG() > 0.9) { let rv = Math.floor(PRNG() * 18); let vegetationTile = new VegetationTile(false); - // console.log(vegetationTile.isTickEnabled()); 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); addGameObjectToGameState(vegetationTile); @@ -184,7 +209,7 @@ 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, i+"_"+j); + chunk.addToChunk(vegetationTile, ChunkStorageTypes.TYPE_VEGETATION, ii+"_"+jj); jj++; continue; @@ -206,33 +231,11 @@ export function createFirstWorldChunks() { let chunk = new WorldChunk(false); chunk.drawObject.position.set(w * chunkXCeiled, h * chunkYCeiled); - + chunk.props = {id: chunkId}; BC_CURRENT_SCENE.addObjectToSceneWithInitialization(chunk); fillWorldGenChunk(chunk, chunkXCeiled, chunkYCeiled); WorldChunksStorage.set(chunkId, chunk); - // console.log(chunkId); - // let chunkRef = createWorldChunkContainer(); - // chunkRef.isRenderGroup = true; - // chunkRef.position.set(w * chunkXCeiled, h * chunkYCeiled); - // console.log(w * chunkXCeiled, h * chunkYCeiled); - // let chunk0 = new WorldChunk( - // chunkRef, - // chunkRef.getChildAt(0), - // chunkRef.getChildAt(1), - // chunkRef.getChildAt(2), - // // chunkRef.getChildAt(3), - // new Vault("terrain"), - // new Vault("vegetation"), - // new Vault("buildings") - // ); - - // fillChunk(chunk0, chunkXCeiled, chunkYCeiled); - // WORLD_CHUNKS.set(chunkId, chunk0); - // chunkRef.visible = false; - // addToViewport(chunkRef); - // console.log(WORLD_CHUNKS) - // console.log(chunkXCeiled, chunkYCeiled); } } WorldChunksVisibilityUpdater.enableAutoWorldChunksGeneration = true; diff --git a/src/Game/WorldGeneration/WorldObjects/TerrainTile/TerrainTile.js b/src/Game/WorldGeneration/WorldObjects/TerrainTile/TerrainTile.js index 98784f9..9cf157d 100644 --- a/src/Game/WorldGeneration/WorldObjects/TerrainTile/TerrainTile.js +++ b/src/Game/WorldGeneration/WorldObjects/TerrainTile/TerrainTile.js @@ -8,20 +8,28 @@ export class TerrainTileProps * @type String */ type; + /** * @type Number */ temperature; + /** + * @type Number + */ + navigationCost = 1000; + /** * * @param {String} type * @param {Number} temperature + * @param {Number} navigationCost */ - constructor(type = "", temperature = 0) + constructor(type = "", temperature = 0, navigationCost = 0) { this.type = type; this.temperature = temperature; + this.navigationCost = navigationCost; }; diff --git a/src/Test.js b/src/Test.js index e3a2fed..651a3b9 100644 --- a/src/Test.js +++ b/src/Test.js @@ -131,5 +131,3 @@ export function search() { //no solution by default return []; } - -// console.log(search()); \ No newline at end of file