diff --git a/src/Game/Game.js b/src/Game/Game.js index 5fa7e07..9f9131c 100644 --- a/src/Game/Game.js +++ b/src/Game/Game.js @@ -8,27 +8,15 @@ import { BC_APP, BC_BUILDING_PLACEHOLDERS, BC_CAMERA, BC_CURRENT_SCENE, BC_SPRIT import { clampNumber, interpolate, } from "./Utils/Math.utils"; import { calculateViewportFromCamera, moveHorizontally, moveVertically, screenToWorldCoordinates } from "./Camera/Camera"; -import { - addToBuildings, - createFirstWorldChunks, - getChunk, - getObjectFromBuildingsLayer, - getObjectFromTerrainLayer, - getObjectFromVegetationLayer, - removeFromVegetation, - updateChunksVisibility, - worldCoordinatesToChunkIndexesCoordinates, - worldCoordinatesToChunkLocalCoordinates, -} from "./WorldGeneration/WorldGeneration"; import { handleBuildingsIncome, incBuildingCount } from "./Buildings/Buildings"; import { handleDayNightCycle } from "./World/DayNightCycle"; import { ambientDay, ambientMusic, ambientNight, handleSounds } from "./Sound/Sound"; -import { handleChunkFilling } from "./WorldGeneration/ChunkFillQueue"; -import { addNPCToWorld } from "./NPC/NPC"; import { gameStateObjectsCleaner } from "./GameState/GameState"; import { tickHandler } from "./TickHandler/TickHandler"; import { GameScene } from "./GameScene/GameScene"; -import { createChunks } from "./WorldGeneration/WorldGen"; +import { createFirstWorldChunks, getWorldChunkAt } from "./WorldGeneration/WorldGen"; +import { ChunkStorageTypes } from "./WorldGeneration/WorldChunk/WorldGenChunk"; +import { WorldChunksVisibilityUpdater } from "./WorldGeneration/WorldChunksVisibilityUpdater/WorldChunksVisibilityUpdater"; export function generateWorld() { @@ -52,56 +40,6 @@ function setupGlobalInput() { function setupInGameSelector() { - let ph = getSpriteFromAtlas("assets/images/buildings/buildings.png", new PIXI.Rectangle(0, 0, 16, 16)); - ph.scale.set(BC_SPRITES_SETTINGS.scale); - ph.alpha = 0.5; - ph.visible = false; - ph.zIndex = 1; - BC_BUILDING_PLACEHOLDERS.push(ph); - BC_VIEWPORT.addChild(ph); - ph = getSpriteFromAtlas("assets/images/buildings/buildings.png", new PIXI.Rectangle(16, 0, 16, 16)); - ph.scale.set(BC_SPRITES_SETTINGS.scale); - ph.alpha = 0.5; - ph.visible = false; - ph.zIndex = 1; - BC_BUILDING_PLACEHOLDERS.push(ph); - BC_VIEWPORT.addChild(ph); - ph = getSpriteFromAtlas("assets/images/buildings/buildings.png", new PIXI.Rectangle(0, 16, 16, 16)); - ph.scale.set(BC_SPRITES_SETTINGS.scale); - ph.alpha = 0.5; - ph.visible = false; - ph.zIndex = 1; - BC_BUILDING_PLACEHOLDERS.push(ph); - BC_VIEWPORT.addChild(ph); - ph = getSpriteFromAtlas("assets/images/buildings/buildings.png", new PIXI.Rectangle(16, 16, 16, 16)); - ph.scale.set(BC_SPRITES_SETTINGS.scale); - ph.alpha = 0.5; - ph.visible = false; - ph.zIndex = 1; - BC_BUILDING_PLACEHOLDERS.push(ph); - BC_VIEWPORT.addChild(ph); - ph = getSpriteFromAtlas("assets/images/buildings/buildings.png", new PIXI.Rectangle(0, 32, 16, 16)); - ph.scale.set(BC_SPRITES_SETTINGS.scale); - ph.alpha = 0.5; - ph.visible = false; - ph.zIndex = 1; - BC_BUILDING_PLACEHOLDERS.push(ph); - BC_VIEWPORT.addChild(ph); - ph = getSpriteFromAtlas("assets/images/buildings/buildings.png", new PIXI.Rectangle(16, 32, 16, 16)); - ph.scale.set(BC_SPRITES_SETTINGS.scale); - ph.alpha = 0.5; - ph.visible = false; - ph.zIndex = 1; - BC_BUILDING_PLACEHOLDERS.push(ph); - BC_VIEWPORT.addChild(ph); - - UIMainPipe.subscribe((s)=>{ - for (const i of BC_BUILDING_PLACEHOLDERS) { - i.visible = false; - } - BC_BUILDING_PLACEHOLDERS[s.selectedBuilding].visible = true; - }); - let sprite0 = PIXI.Sprite.from("assets/images/selection.png"); sprite0.scale.set(BC_SPRITES_SETTINGS.scale, BC_SPRITES_SETTINGS.scale); BC_VIEWPORT.addChild(sprite0); @@ -111,116 +49,12 @@ function setupInGameSelector() { BC_VIEWPORT.eventMode = "static"; BC_VIEWPORT.onmousemove = (e) => { let t = screenToWorldCoordinates(e.data.global.x, e.data.global.y); - let terrainObject = getObjectFromTerrainLayer(t.x, t.y); - let vegetationObject = getObjectFromVegetationLayer(t.x, t.y); - let buildingObject = getObjectFromBuildingsLayer(t.x, t.y); - - UISelectionInfo.update((s) => { - let t_arr = []; - if (terrainObject) { - t_arr.push(terrainObject.type); - } - if (vegetationObject) { - t_arr.push(vegetationObject.type); - } - s.types = [...t_arr]; - }); + // let tChunk = getWorldChunkAt(t.x, t.y); + // let tTerrainTile = tChunk.getFromChunk(t.x, t.y, ChunkStorageTypes.TYPE_TERRAIN); t.x = BC_SPRITES_SETTINGS.defaultSize * BC_SPRITES_SETTINGS.scale * Math.floor(t.x / (BC_SPRITES_SETTINGS.defaultSize * BC_SPRITES_SETTINGS.scale)); 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); - - if(UIMainPipe.get().building && terrainObject.type !== "ter_water" && !vegetationObject && !buildingObject) - { - let q = UIMainPipe.get().selectedBuilding; - BC_BUILDING_PLACEHOLDERS[q].visible = true; - BC_BUILDING_PLACEHOLDERS[q].position.set(t.x, t.y); - } - else - { - BC_BUILDING_PLACEHOLDERS[UIMainPipe.get().selectedBuilding].visible = false; - } - }; - - BC_VIEWPORT.onpointerdown = (e)=>{ - let t = screenToWorldCoordinates(e.data.global.x, e.data.global.y); - let terrainObject = getObjectFromTerrainLayer(t.x, t.y); - let vegetationObject = getObjectFromVegetationLayer(t.x, t.y); - let buildingObject = getObjectFromBuildingsLayer(t.x, t.y); - // console.log(vegetationObject); - if(UIMainPipe.get().building && terrainObject.type !== "ter_water" && !vegetationObject && !buildingObject) - { - if(UIObtainedResourcesPipe.get().gold >= 50) - { - let chunk = getChunk(t.x, t.y); - if(chunk) - { - let ns; - switch(UIMainPipe.get().selectedBuilding) - { - case 0: - ns = getSpriteFromAtlas("assets/images/buildings/buildings.png", new PIXI.Rectangle(0, 0, 16, 16)); - break; - case 1: - ns = getSpriteFromAtlas("assets/images/buildings/buildings.png", new PIXI.Rectangle(16, 0, 16, 16)); - break; - case 2: - ns = getSpriteFromAtlas("assets/images/buildings/buildings.png", new PIXI.Rectangle(0, 16, 16, 16)); - break; - case 3: - ns = getSpriteFromAtlas("assets/images/buildings/buildings.png", new PIXI.Rectangle(16, 16, 16, 16)); - break; - case 4: - ns = getSpriteFromAtlas("assets/images/buildings/buildings.png", new PIXI.Rectangle(0, 32, 16, 16)); - break; - case 5: - ns = getSpriteFromAtlas("assets/images/buildings/buildings.png", new PIXI.Rectangle(16, 32, 16, 16)); - break; - } - ns.scale.set(BC_SPRITES_SETTINGS.scale, BC_SPRITES_SETTINGS.scale); - let r = worldCoordinatesToChunkIndexesCoordinates(t.x, t.y); - // console.log(r); - addToBuildings(chunk, ns, {type: "building"}, r.x, r.y); - r = worldCoordinatesToChunkLocalCoordinates(t.x, t.y); - // console.log(r); - ns.position.set(r.x * BC_SPRITES_SETTINGS.scale * BC_SPRITES_SETTINGS.defaultSize, r.y * BC_SPRITES_SETTINGS.scale * BC_SPRITES_SETTINGS.defaultSize); - UIObtainedResourcesPipe.update((s)=>{ - s.gold -= 50; - }); - incBuildingCount(1); - } - } - } - else if(!UIMainPipe.get().building) - { - if(vegetationObject) - { - try { - switch (vegetationObject.type) { - case "grass": - UIObtainedResourcesPipe.update((s)=>{ - s.grass += vegetationObject.num; - }); - break; - case "stone": - UIObtainedResourcesPipe.update((s)=>{ - s.stone += vegetationObject.num; - }); - break; - case "wood": - UIObtainedResourcesPipe.update((s)=>{ - s.wood += vegetationObject.num; - }); - break; - default: - break; - } - removeFromVegetation(t.x, t.y, vegetationObject.obj); - } catch (error) { - - } - } - } }; } @@ -263,8 +97,9 @@ export async function initGame() { app.ticker.add(inputControllerTick); app.ticker.add(calculateViewportFromCamera); - // app.ticker.add(updateChunksVisibility); - // app.ticker.add(handleChunkFilling); + // console.log(WorldChunksVisibilityUpdater.handleWorldChunksVisibility); + app.ticker.add(()=>{WorldChunksVisibilityUpdater.handleWorldChunksVisibility();}); + app.ticker.add(()=>{WorldChunksVisibilityUpdater.handleWorldChunkFilling();}); app.ticker.add(profileFPS); app.ticker.add(handleBuildingsIncome); @@ -286,5 +121,5 @@ function startGame() { BC_APP.ticker.add(gameStateObjectsCleaner); BC_APP.ticker.add(tickHandler); - createChunks(); + createFirstWorldChunks(); } diff --git a/src/Game/GameObject/GameObject.js b/src/Game/GameObject/GameObject.js index fc52086..ae243e8 100644 --- a/src/Game/GameObject/GameObject.js +++ b/src/Game/GameObject/GameObject.js @@ -10,17 +10,17 @@ export class GameObject { majorId = -1; //primary game object id minorId = -1; - #tickEnabled = true; - #markedPendingKill = false; + _tickEnabled = false; + _markedPendingKill = false; // markedAsInitialized = false; /** * GameObject id * @param {Boolean} tickAble */ - constructor(tickAble = true) + constructor(tickAble = false) { - this.#tickEnabled = tickAble; + this._tickEnabled = tickAble; }; /** @@ -37,9 +37,9 @@ export class GameObject { */ kill() { - if(this.#markedPendingKill) return; + if(this._markedPendingKill) return; this.preDestroy(); - this.#markedPendingKill = true; + this._markedPendingKill = true; addToCleaningQueue(this.majorId, this.minorId); }; @@ -64,6 +64,6 @@ export class GameObject { */ tick(ticker){}; - isTickEnabled(){return this.#tickEnabled;}; - isMarkedPendingKill(){return this.#markedPendingKill;}; + isTickEnabled(){return this._tickEnabled;}; + isMarkedPendingKill(){return this._markedPendingKill;}; }; \ No newline at end of file diff --git a/src/Game/GameState/GameState.js b/src/Game/GameState/GameState.js index cde0ba2..0a93dc4 100644 --- a/src/Game/GameState/GameState.js +++ b/src/Game/GameState/GameState.js @@ -28,7 +28,7 @@ function incId() { } /** - * adds game object to game state + * adds game object to game state and inits it * @param {GameObject} gameObject */ export function addGameObjectToGameState(gameObject) { @@ -43,10 +43,15 @@ export function addGameObjectToGameState(gameObject) { } export function gameStateObjectsCleaner() { + let counter = 0; for (const i of CleaningQueue) { GameObjects.delete(i.minor); TickGameObjects.delete(i.minor); + 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/NPC/NPC.js b/src/Game/NPC/NPC.js deleted file mode 100644 index bd10520..0000000 --- a/src/Game/NPC/NPC.js +++ /dev/null @@ -1,69 +0,0 @@ -import { BC_NPC_LAYER, BC_SPRITES_SETTINGS } from "../GlobalVariables/GlobalVariables"; -import { Vault } from "../Utils/DataTypes.utils"; -import { getSpriteFromAtlas } from "../Utils/Sprites.utils"; -import { WORLD_CHUNKS, worldCoordinatesToChunkIndex, worldCoordinatesToChunkIndexesCoordinates } from "../WorldGeneration/WorldGeneration"; -import * as PIXI from "../../pixi/pixi.mjs"; -import { addToViewport } from "../Utils/World.utils"; - -export const NPCVault = new Vault(); -let lastNPCId = -1; - -export class NPC -{ - /** - * - * @param {Position} param0 - * @param {Position} param1 - * @param {NPCProps} props - */ - constructor(x, y, x_c, y_c, spriteRef, id, props = {}) - { - this.position = {x, y}; - this.ceiledPosition = {x_c, y_c}; - // this.spriteRef = spriteRef; - // this.id = id; - this.props = {spriteRef, id, classRef: this, ...props}; - } - - setProps(props) - { - this.props = {...this.props, ...props}; - } - - tick(ticker) - { - - } -}; - -export class NPCProps -{ - constructor({type}) - { - this.type = type; - } -}; - -/** - * - * @param {Number} x - * @param {Number} y - * @param {NPCProps} props - */ -export function addNPCToWorld(x, y, props={}) { - let pos = worldCoordinatesToChunkIndexesCoordinates(x, y); - let sprite = getSpriteFromAtlas("assets/images/characters/char0.png", new PIXI.Rectangle(0,0,16,16)); - sprite.position.set(pos.x *BC_SPRITES_SETTINGS.defaultSize *BC_SPRITES_SETTINGS.scale, pos.y *BC_SPRITES_SETTINGS.defaultSize *BC_SPRITES_SETTINGS.scale); - sprite.scale.set(BC_SPRITES_SETTINGS.scale, BC_SPRITES_SETTINGS.scale); - sprite.zIndex = 100; - // addToViewport(sprite); - lastNPCId++; - let npc = new NPC(x, y, pos.x, pos.y, sprite, lastNPCId, props); - NPCVault.set(lastNPCId, npc); - BC_NPC_LAYER.addChild(sprite); - console.log(BC_NPC_LAYER); -} - -export function npcTickHandler(ticker) -{ -} \ No newline at end of file diff --git a/src/Game/SceneObjects/SceneObject.js b/src/Game/SceneObjects/SceneObject.js index 2d37e49..021ee75 100644 --- a/src/Game/SceneObjects/SceneObject.js +++ b/src/Game/SceneObjects/SceneObject.js @@ -13,6 +13,12 @@ export class SceneObject extends GameObject { */ props = {}; + constructor(tickAble) + { + super(tickAble); + // this._tickEnabled = tickAble; + } + /** * Instantly* kills drawObject (by PIXI) and after several ticks kills SceneObject * diff --git a/src/Game/WorldGeneration/ChunkFillQueue.js b/src/Game/WorldGeneration/ChunkFillQueue.js deleted file mode 100644 index db1ae04..0000000 --- a/src/Game/WorldGeneration/ChunkFillQueue.js +++ /dev/null @@ -1,24 +0,0 @@ -import { WorldChunk } from "../WorldChunk/WorldChunk"; -import { fillChunk } from "./WorldGeneration"; - -let chunkFillQueue = []; -let currentChunkIndex = -1; - -/** - * - * @param {WorldChunk} chunk - */ -export function addChunkToFillQueue(chunk, x, y) -{ - chunkFillQueue.push({chunk, x, y}); - currentChunkIndex++; -} - -export function handleChunkFilling(tick) { - // console.log(tick.deltaMS); - if(currentChunkIndex < 0) return; - fillChunk(chunkFillQueue[currentChunkIndex].chunk, chunkFillQueue[currentChunkIndex].x, chunkFillQueue[currentChunkIndex].y); - chunkFillQueue.pop(); - currentChunkIndex--; - -} diff --git a/src/Game/WorldGeneration/ObjectsPropsSchemes.js b/src/Game/WorldGeneration/ObjectsPropsSchemes.js deleted file mode 100644 index 4eea08d..0000000 --- a/src/Game/WorldGeneration/ObjectsPropsSchemes.js +++ /dev/null @@ -1,45 +0,0 @@ -import { WorldChunk } from "../WorldChunk/WorldChunk"; - -/** - * - * @param {PIXI.Container} objectRef - * @param {String} type - */ -export function TerrainObjectProps(objectRef, {type}) { - this.obj = objectRef; - this.type = type; -}; - -/** - * - * @param {PIXI.Container} objectRef - * @param {String} type - * @param {Number} num - */ -export function VegetationObjectProps(objectRef, {type, num}) { - this.obj = objectRef; - this.type = type; - this.num = num; -}; - -/** - * - * @param {PIXI.Container} objectRef - * @param {String} type - * @param {Number} goldSec - */ -export function BuildingsObjectProps(objectRef, {type, goldSec}) { - this.obj = objectRef; - this.type = type; - this.goldSec = goldSec; -}; - -/** - * - * @param {WorldChunk} chunkRef - * @param {Boolean} central - */ -export function VisibleChunkProps(chunkRef, central) { - this.chunkRef = chunkRef; - this.central = central; -} \ No newline at end of file diff --git a/src/Game/WorldGeneration/WorldChunk/WorldGenChunk.js b/src/Game/WorldGeneration/WorldChunk/WorldGenChunk.js index 0e555c9..a2839c3 100644 --- a/src/Game/WorldGeneration/WorldChunk/WorldGenChunk.js +++ b/src/Game/WorldGeneration/WorldChunk/WorldGenChunk.js @@ -1,6 +1,8 @@ import { Container } from "../../../pixi/pixi.mjs"; import { SceneObject } from "../../SceneObjects/SceneObject"; +import { worldCoordinatesToChunkLocalCoordinates } from "../WorldGen"; import { TerrainTile } from "../WorldObjects/TerrainTile/TerrainTile"; +import { VegetationTile } from "../WorldObjects/VegetationTile/VegetationTile"; export class ChunkStorageTypes { @@ -16,6 +18,19 @@ export class WorldChunk extends SceneObject * @type Map */ terrainStorage = new Map(); + /** + * @type Map + */ + vegetationStorage = new Map(); + /** + * @type Map + */ + buildingsStorage = new Map(); + + /** + * property to handle chunk visibility + */ + centralChunk = false; /** * @@ -26,14 +41,20 @@ export class WorldChunk extends SceneObject addToChunk(object, storageType, objectId) { switch (storageType) { - case 0: + case ChunkStorageTypes.TYPE_TERRAIN: this.terrainStorage.set(objectId, object); break; - - default: + case ChunkStorageTypes.TYPE_VEGETATION: + this.vegetationStorage.set(objectId, object); break; + case ChunkStorageTypes.TYPE_BUILDINGS: + this.buildingsStorage.set(objectId, object); + break; + default: + return false; } this.drawObject.addChild(object.drawObject); + return true; } /** @@ -45,13 +66,41 @@ export class WorldChunk extends SceneObject removeFromChunk(object, storageType, objectId) { switch (storageType) { - case 0: + case ChunkStorageTypes.TYPE_TERRAIN: this.terrainStorage.delete(objectId); break; - - default: + case ChunkStorageTypes.TYPE_VEGETATION: + this.vegetationStorage.delete(objectId); break; + case ChunkStorageTypes.TYPE_BUILDINGS: + this.buildingsStorage.delete(objectId); + break; + default: + return false; + } + this.drawObject.removeChild(object.drawObject); + return true; + }; + + /** + * + * @param {Number} xWorld + * @param {Number} yWorld + * @param {ChunkStorageTypes} storageType + * @returns SceneObject or undefined + */ + getFromChunk(xWorld, yWorld, storageType) + { + let coords = worldCoordinatesToChunkLocalCoordinates(xWorld, yWorld); + switch (storageType) { + case ChunkStorageTypes.TYPE_TERRAIN: + return this.terrainStorage.get(coords.x + "_" + coords.y); + case ChunkStorageTypes.TYPE_VEGETATION: + return this.vegetationStorage.get(coords.x + "_" + coords.y); + case ChunkStorageTypes.TYPE_BUILDINGS: + return this.buildingsStorage.get(coords.x + "_" + coords.y); + default: + return undefined; } - this.drawObject.removeChild(object); } }; \ No newline at end of file diff --git a/src/Game/WorldGeneration/WorldChunksVisibilityUpdater/WorldChunksVisibilityUpdater.js b/src/Game/WorldGeneration/WorldChunksVisibilityUpdater/WorldChunksVisibilityUpdater.js new file mode 100644 index 0000000..c1bf5c4 --- /dev/null +++ b/src/Game/WorldGeneration/WorldChunksVisibilityUpdater/WorldChunksVisibilityUpdater.js @@ -0,0 +1,82 @@ +import { BC_CAMERA, BC_CHUNKS_SETTINGS, BC_CURRENT_SCENE, BC_TERRAIN_SETTINGS } from "../../GlobalVariables/GlobalVariables"; +import { fillWorldGenChunk, getWorldChunkById, setWorldChunkById, worldChunkExists } from "../WorldGen"; +import { WorldChunk } from "../WorldChunk/WorldGenChunk"; + +export class WorldChunksVisibilityUpdater { + static _chunksFillingQueue = []; + static _currentIndex = -1; + /** + * @type Map + */ + static _visibleChunks = new Map(); + static enableAutoWorldChunksGeneration = false; + + static _chunksVisibilityRange = { + min: -2, + max: 3 + }; + + // static { + // this.visibleChunks = new Map(); + // }; + + static addChunkToFillingQueue(chunk, x, y) { + this._chunksFillingQueue.push({ chunk, x, y }); + this._currentIndex++; + } + + /** + * Ticker function + */ + static handleWorldChunkFilling() { + // console.log(tick.deltaMS); + if (this._currentIndex < 0) return; + fillWorldGenChunk( + this._chunksFillingQueue[this._currentIndex].chunk, + this._chunksFillingQueue[this._currentIndex].x, + this._chunksFillingQueue[this._currentIndex].y + ); + this._chunksFillingQueue.pop(); + this._currentIndex--; + } + + + /** + * Ticker function + */ + static handleWorldChunksVisibility() { + let w = BC_CHUNKS_SETTINGS.width * BC_TERRAIN_SETTINGS.tileSize * BC_TERRAIN_SETTINGS.scale; + let h = BC_CHUNKS_SETTINGS.height * BC_TERRAIN_SETTINGS.tileSize * BC_TERRAIN_SETTINGS.scale; + 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; + } + this._visibleChunks.clear(); + for (let i = this._chunksVisibilityRange.min; i < this._chunksVisibilityRange.max; i++) { + for (let j = this._chunksVisibilityRange.min; j < this._chunksVisibilityRange.max; j++) { + let t_chunkId = (cx + i) + "_" + (cy + j); + if (worldChunkExists(t_chunkId)) { + let wChunk = getWorldChunkById(t_chunkId); + wChunk.centralChunk = (i === 0 && j === 0); + 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)); + this.addChunkToFillingQueue(newChunk, cx + i, cy + j); + this._visibleChunks.set(t_chunkId, newChunk); + newChunk.drawObject.visible = true; + newChunk.centralChunk = (i === 0 && j === 0); + setWorldChunkById(newChunk, t_chunkId); + } + } + } + } + } +} diff --git a/src/Game/WorldGeneration/WorldGen.js b/src/Game/WorldGeneration/WorldGen.js index af2b622..081278a 100644 --- a/src/Game/WorldGeneration/WorldGen.js +++ b/src/Game/WorldGeneration/WorldGen.js @@ -6,6 +6,8 @@ import { ChunkStorageTypes, WorldChunk } from "./WorldChunk/WorldGenChunk"; import { TerrainTile, TerrainTileProps } from "./WorldObjects/TerrainTile/TerrainTile"; import { clampNumber } from "../Utils/Math.utils"; import { addGameObjectToGameState } from "../GameState/GameState"; +import { VegetationTile, VegetationTileProps } from "./WorldObjects/VegetationTile/VegetationTile"; +import { WorldChunksVisibilityUpdater } from "./WorldChunksVisibilityUpdater/WorldChunksVisibilityUpdater"; /** * @type Map @@ -46,19 +48,6 @@ const grassVegetationSpriteList = { 16: { x: 8, y: 12 }, 17: { x: 9, y: 12 }, }; -const sandVegetationSpriteList = { - 0: { x: 14, y: 10 }, - 1: { x: 7, y: 13 }, - 2: { x: 6, y: 13 }, - 3: { x: 5, y: 13 }, - 4: { x: 4, y: 13 }, - 5: { x: 3, y: 13 }, -}; -const stoneVegetationSpriteList = { - 0: { x: 5, y: 13 }, - 1: { x: 4, y: 13 }, - 2: { x: 3, y: 13 }, -}; const grassVegResourcesList = { 0: { type: "grass", num: 10 / 5 }, 1: { type: "grass", num: 9 / 5 }, @@ -80,20 +69,57 @@ const grassVegResourcesList = { 17: { type: "wood", num: 2 / 5 }, }; -const sandVegResourcesList = { - 0: { type: "grass", num: 4 / 5 }, - 1: { type: "stone", num: 1 / 5 }, - 2: { type: "stone", num: 3 / 5 }, - 3: { type: "stone", num: 4 / 5 }, - 4: { type: "stone", num: 5 / 5 }, - 5: { type: "stone", num: 5 / 5 }, -}; +export function worldCoordinatesToChunkIndex(x, y) { + let w = BC_CHUNKS_SETTINGS.width * BC_TERRAIN_SETTINGS.tileSize * BC_TERRAIN_SETTINGS.scale; + let h = BC_CHUNKS_SETTINGS.height * BC_TERRAIN_SETTINGS.tileSize * BC_TERRAIN_SETTINGS.scale; + return { x: Math.floor(x / w), y: Math.floor(y / h) }; +} +export function worldCoordinatesToChunkIndexesCoordinates(x, y) { + let ws = BC_TERRAIN_SETTINGS.tileSize * BC_TERRAIN_SETTINGS.scale; + 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, + 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, + }; + 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, + }; + else + return { + x: Math.floor(Math.abs(x) / ws) % BC_CHUNKS_SETTINGS.width, + y: Math.floor(Math.abs(y) / hs) % BC_CHUNKS_SETTINGS.height, + }; +} -const stoneVegResourcesList = { - 0: { type: "stone", num: 4 / 5 }, - 1: { type: "stone", num: 5 / 5 }, - 2: { type: "stone", num: 5 / 5 }, -}; +export function getWorldChunkAt(xWorld, yWorld) { + let t = worldCoordinatesToChunkIndex(xWorld, yWorld); + return WorldChunksStorage.get(t.x+"_"+t.y); +} + +export function getWorldChunkById(id) { + return WorldChunksStorage.get(id); +} + +export function worldChunkExists(id) { + return WorldChunksStorage.has(id); +} + +export function setWorldChunkById(chunk, id) { + WorldChunksStorage.set(id, chunk); +} let noise = new Noise(Math.floor(PRNG() * 188822321)); let noiseErosion = new Noise(Math.floor(PRNG() * 327749029)); @@ -128,6 +154,7 @@ 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); @@ -140,65 +167,35 @@ export function fillWorldGenChunk(chunk, x, y) { terrainTile.drawObject.scale.set(BC_SPRITES_SETTINGS.scale, BC_SPRITES_SETTINGS.scale); chunk.addToChunk(terrainTile, ChunkStorageTypes.TYPE_TERRAIN, i+"_"+j); - // let sprite = getSpriteFromAtlas( - // "assets/images/world/world_terrain_atlas.png", - // new PIXI.Rectangle(terrainSpriteList[res].x, terrainSpriteList[res].y, 16, 16) - // ); - // sprite.position.set(16 * BC_SPRITES_SETTINGS.scale * ii, 16 * BC_SPRITES_SETTINGS.scale * jj); - // sprite.scale.set(BC_SPRITES_SETTINGS.scale, BC_SPRITES_SETTINGS.scale); - // sprite.tint = sTint; - // addToTerrain(chunk, sprite, { type: terrainTypeList[res] }, i, j); - // console.log(ii, jj); - // addToTerrain(sprite, {x: i, y: j}, {type: terrainTypeList[res]}); - // if (res === 2 && PRNG() > 0.9) { - // let rv = Math.floor(PRNG() * 18); - // let veg = getSpriteFromAtlas( - // "assets/images/world/vegetation_ts.png", - // new PIXI.Rectangle(16 * grassVegetationSpriteList[rv].x, 16 * grassVegetationSpriteList[rv].y, 16, 16) - // ); - // veg.position.set(16 * BC_SPRITES_SETTINGS.scale * ii, 16 * BC_SPRITES_SETTINGS.scale * jj); - // veg.scale.set(BC_SPRITES_SETTINGS.scale, BC_SPRITES_SETTINGS.scale); - // veg.tint = sTint; - // addToVegetation(chunk, veg, { ...grassVegResourcesList[rv] }, i, j); - // jj++; - // continue; - // } - // if (res === 1 && PRNG() > 0.99) { - // let rv = Math.floor(PRNG() * 6); + if (res === 2 && PRNG() > 0.9) { + let rv = Math.floor(PRNG() * 18); - // let veg = getSpriteFromAtlas( - // "assets/images/world/vegetation_ts.png", - // new PIXI.Rectangle(16 * sandVegetationSpriteList[rv].x, 16 * sandVegetationSpriteList[rv].y, 16, 16) - // ); - // veg.position.set(16 * BC_SPRITES_SETTINGS.scale * ii, 16 * BC_SPRITES_SETTINGS.scale * jj); - // veg.scale.set(BC_SPRITES_SETTINGS.scale, BC_SPRITES_SETTINGS.scale); - // veg.tint = sTint; - // addToVegetation(chunk, veg, { ...sandVegResourcesList[rv] }, i, j); - // jj++; - // continue; - // } - // if (res === 3 && PRNG() > 0.9) { - // let rv = Math.floor(PRNG() * 3); + 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]); - // let veg = getSpriteFromAtlas( - // "assets/images/world/vegetation_ts.png", - // new PIXI.Rectangle(16 * stoneVegetationSpriteList[rv].x, 16 * stoneVegetationSpriteList[rv].y, 16, 16) - // ); - // veg.position.set(16 * BC_SPRITES_SETTINGS.scale * ii, 16 * BC_SPRITES_SETTINGS.scale * jj); - // veg.scale.set(BC_SPRITES_SETTINGS.scale, BC_SPRITES_SETTINGS.scale); - // veg.tint = sTint; - // addToVegetation(chunk, veg, { ...stoneVegResourcesList[rv] }, i, j); - // jj++; - // continue; - // } + addGameObjectToGameState(vegetationTile); + + vegetationTile.drawObject.tint = sTint; + vegetationTile.drawObject.zIndex = 2; + 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); + + jj++; + continue; + } jj++; } ii++; } } -export function createChunks() { +export function createFirstWorldChunks() { let w = BC_CHUNKS_SETTINGS.width * BC_TERRAIN_SETTINGS.tileSize * BC_TERRAIN_SETTINGS.scale; let h = BC_CHUNKS_SETTINGS.height * BC_TERRAIN_SETTINGS.tileSize * BC_TERRAIN_SETTINGS.scale; for (let i = -1; i < 2; i++) { @@ -238,6 +235,7 @@ export function createChunks() { // console.log(chunkXCeiled, chunkYCeiled); } } + WorldChunksVisibilityUpdater.enableAutoWorldChunksGeneration = true; } /* #### REWRITE PART END ####*/ \ No newline at end of file diff --git a/src/Game/WorldGeneration/WorldGeneration.js b/src/Game/WorldGeneration/WorldGeneration.js deleted file mode 100644 index 6c228dd..0000000 --- a/src/Game/WorldGeneration/WorldGeneration.js +++ /dev/null @@ -1,459 +0,0 @@ -import { Noise } from "noisejs"; -import { BC_CAMERA, BC_CHUNKS_SETTINGS, BC_SPRITES_SETTINGS, BC_TERRAIN_SETTINGS, PRNG } from "../GlobalVariables/GlobalVariables"; -import { clampNumber, integerDivision, mapRange } from "../Utils/Math.utils"; -import { addToViewport } from "../Utils/World.utils"; -import { WorldChunk, createWorldChunkContainer } from "../WorldChunk/WorldChunk"; -import { getSpriteFromAtlas } from "../Utils/Sprites.utils"; -import * as PIXI from "../../pixi/pixi.mjs"; -import { BuildingsObjectProps, TerrainObjectProps, VegetationObjectProps, VisibleChunkProps } from "./ObjectsPropsSchemes"; -import { NumberCue, RGBColor, Vault } from "../Utils/DataTypes.utils"; -import { addChunkToFillQueue } from "./ChunkFillQueue"; -// import {WorldChunk} from "../Types/WorldGenerationTypes"; - -const terrainSpriteList = { - 0: { x: 21, y: 21 }, //water - 1: { x: 2, y: 21 }, //sand - 2: { x: 2, y: 2 }, //grass - 3: { x: 21, y: 2 }, //stone -}; -const terrainTypeList = { - 0: "ter_water", //water - 1: "ter_sand", //sand - 2: "ter_grass", //grass - 3: "ter_stone", //stone -}; -const grassVegetationSpriteList = { - 0: { x: 10, y: 11 }, - 1: { x: 11, y: 11 }, - 2: { x: 12, y: 11 }, - 3: { x: 13, y: 11 }, - 4: { x: 14, y: 11 }, - 5: { x: 15, y: 11 }, - 6: { x: 16, y: 11 }, - 7: { x: 17, y: 11 }, - 8: { x: 18, y: 11 }, - 9: { x: 19, y: 11 }, - 10: { x: 0, y: 12 }, - 11: { x: 1, y: 12 }, - 12: { x: 2, y: 12 }, - 13: { x: 3, y: 12 }, - 14: { x: 7, y: 13 }, - 15: { x: 7, y: 12 }, - 16: { x: 8, y: 12 }, - 17: { x: 9, y: 12 }, -}; -const sandVegetationSpriteList = { - 0: { x: 14, y: 10 }, - 1: { x: 7, y: 13 }, - 2: { x: 6, y: 13 }, - 3: { x: 5, y: 13 }, - 4: { x: 4, y: 13 }, - 5: { x: 3, y: 13 }, -}; -const stoneVegetationSpriteList = { - 0: { x: 5, y: 13 }, - 1: { x: 4, y: 13 }, - 2: { x: 3, y: 13 }, -}; -const grassVegResourcesList = { - 0: { type: "grass", num: 10 / 5 }, - 1: { type: "grass", num: 9 / 5 }, - 2: { type: "grass", num: 8 / 5 }, - 3: { type: "grass", num: 7 / 5 }, - 4: { type: "grass", num: 4 / 5 }, - 5: { type: "grass", num: 4 / 5 }, - 6: { type: "grass", num: 5 / 5 }, - 7: { type: "grass", num: 3 / 5 }, - 8: { type: "grass", num: 2 / 5 }, - 9: { type: "grass", num: 1 / 5 }, - 10: { type: "grass", num: 2 / 5 }, - 11: { type: "grass", num: 2 / 5 }, - 12: { type: "grass", num: 2 / 5 }, - 13: { type: "grass", num: 2 / 5 }, - 14: { type: "stone", num: 1 / 5 }, - 15: { type: "wood", num: 2 / 5 }, - 16: { type: "wood", num: 2 / 5 }, - 17: { type: "wood", num: 2 / 5 }, -}; - -const sandVegResourcesList = { - 0: { type: "grass", num: 4 / 5 }, - 1: { type: "stone", num: 1 / 5 }, - 2: { type: "stone", num: 3 / 5 }, - 3: { type: "stone", num: 4 / 5 }, - 4: { type: "stone", num: 5 / 5 }, - 5: { type: "stone", num: 5 / 5 }, -}; - -const stoneVegResourcesList = { - 0: { type: "stone", num: 4 / 5 }, - 1: { type: "stone", num: 5 / 5 }, - 2: { type: "stone", num: 5 / 5 }, -}; - -export const WORLD_CHUNKS = new Vault(); -let VISIBLE_CHUNKS = new Vault(); - -export function worldCoordinatesToChunkIndex(x, y) { - let w = BC_CHUNKS_SETTINGS.width * BC_TERRAIN_SETTINGS.tileSize * BC_TERRAIN_SETTINGS.scale; - let h = BC_CHUNKS_SETTINGS.height * BC_TERRAIN_SETTINGS.tileSize * BC_TERRAIN_SETTINGS.scale; - return { x: Math.floor(x / w), y: Math.floor(y / h) }; -} -export function worldCoordinatesToChunkIndexesCoordinates(x, y) { - let ws = BC_TERRAIN_SETTINGS.tileSize * BC_TERRAIN_SETTINGS.scale; - 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, - 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, - }; - 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, - }; - else - return { - x: Math.floor(Math.abs(x) / ws) % BC_CHUNKS_SETTINGS.width, - y: Math.floor(Math.abs(y) / hs) % BC_CHUNKS_SETTINGS.height, - }; -} - -/** - * - * @param {Number} x world coordinates - * @param {Number} y world coordinates - */ -export function getChunk(x, y) { - let t = worldCoordinatesToChunkIndex(x, y); - if (WORLD_CHUNKS.existsKey(t.x + "_" + t.y)) return WORLD_CHUNKS.get(t.x + "_" + t.y); - else return undefined; -} - -let enableAutoGeneration = false; - -export function updateChunksVisibility() { - let w = BC_CHUNKS_SETTINGS.width * BC_TERRAIN_SETTINGS.tileSize * BC_TERRAIN_SETTINGS.scale; - let h = BC_CHUNKS_SETTINGS.height * BC_TERRAIN_SETTINGS.tileSize * BC_TERRAIN_SETTINGS.scale; - let cx = Math.floor(BC_CAMERA.position.x / w); - let cy = Math.floor(BC_CAMERA.position.y / h); - let chunkId = cx + "_" + cy; - if ((VISIBLE_CHUNKS.existsKey(chunkId) && !VISIBLE_CHUNKS.get(chunkId).central) || !VISIBLE_CHUNKS.existsKey(chunkId)) { - VISIBLE_CHUNKS.forAll((vis_chunk) => { - vis_chunk.chunkRef.chunk.visible = false; - }); - VISIBLE_CHUNKS.clear(); - for (let i = -2; i < 3; i++) { - for (let j = -2; j < 3; j++) { - let t_chunkId = cx + i + "_" + (cy + j); - if (WORLD_CHUNKS.existsKey(t_chunkId)) { - VISIBLE_CHUNKS.set(t_chunkId, new VisibleChunkProps(WORLD_CHUNKS.get(t_chunkId), i === 0 && j === 0)); - VISIBLE_CHUNKS.get(t_chunkId).chunkRef.chunk.visible = true; - } else if (enableAutoGeneration) { - // console.log(t_chunkId); - let newChunk = createWorldChunkContainer(); - // chunkRef.isRenderGroup = true; - newChunk.position.set(w * (cx + i), h * (cy + j)); - // console.log(w * chunkXCeiled, h * chunkYCeiled); - let chunk0 = new WorldChunk( - newChunk, - newChunk.getChildAt(0), - newChunk.getChildAt(1), - newChunk.getChildAt(2), - // newChunk.getChildAt(3), - new Vault("terrain"), - new Vault("vegetation"), - new Vault("buildings") - ); - // fillChunk(chunk0, cx + i, cy + j); - addChunkToFillQueue(chunk0, cx + i, cy + j); - WORLD_CHUNKS.set(t_chunkId, chunk0); - addToViewport(newChunk); - VISIBLE_CHUNKS.set(t_chunkId, new VisibleChunkProps(WORLD_CHUNKS.get(t_chunkId), i === 0 && j === 0)); - VISIBLE_CHUNKS.get(t_chunkId).chunkRef.chunk.visible = true; - } - } - } - } -} - -/** - * - * @param {WorldChunk} chunk from WORLD_CHUNKS - * @param {PIXI.Container} object - * @param {TerrainObjectProps} props - * @param {Number} ceiledX - * @param {Number} ceiledY - */ -export function addToTerrain(chunk, object, props, ceiledX, ceiledY) { - chunk.terrainLayer.addChild(object); - chunk.terrainObjectsVault.set(ceiledX + "_" + ceiledY, new TerrainObjectProps(object, { ...props })); -} - -/** - * - * @param {WorldChunk} chunk from WORLD_CHUNKS - * @param {PIXI.Container} object - * @param {VegetationObjectProps} props - * @param {Number} ceiledX - * @param {Number} ceiledY - */ -export function addToVegetation(chunk, object, props, ceiledX, ceiledY) { - chunk.vegetationLayer.addChild(object); - chunk.vegetationObjectsVault.set(ceiledX + "_" + ceiledY, new VegetationObjectProps(object, { ...props })); -} - -/** - * - * @param {WorldChunk} chunk from WORLD_CHUNKS - * @param {PIXI.Container} object - * @param {BuildingsObjectProps} props - * @param {Number} ceiledX - * @param {Number} ceiledY - */ -export function addToBuildings(chunk, object, props, ceiledX, ceiledY) { - chunk.buildingsLayer.addChild(object); - chunk.buildingsObjectsVault.set(ceiledX + "_" + ceiledY, new BuildingsObjectProps(object, { ...props })); -} - -/** - * - * @param {Number} x world coordinates - * @param {Number} y world coordinates - * @param {PIXI.Container} object object to remove - */ -export function removeFromTerrain(x, y, object) { - let chunkIndex = worldCoordinatesToChunkIndex(x, y); - let chunkId = chunkIndex.x + "_" + chunkIndex.y; - if (WORLD_CHUNKS.existsKey(chunkId)) { - let objLocalPos = worldCoordinatesToChunkIndexesCoordinates(x, y); - let objId = objLocalPos.x + "_" + objLocalPos.y; - WORLD_CHUNKS.get(chunkId).terrainLayer.removeChild(object); - WORLD_CHUNKS.get(chunkId).terrainObjectsVault.del(objId); - object.destroy(); - } -} -/** - * - * @param {Number} x world coordinates - * @param {Number} y world coordinates - * @param {PIXI.Container} object object to remove - */ -export function removeFromVegetation(x, y, object) { - let chunkIndex = worldCoordinatesToChunkIndex(x, y); - let chunkId = chunkIndex.x + "_" + chunkIndex.y; - if (WORLD_CHUNKS.existsKey(chunkId)) { - let objLocalPos = worldCoordinatesToChunkIndexesCoordinates(x, y); - let objId = objLocalPos.x + "_" + objLocalPos.y; - WORLD_CHUNKS.get(chunkId).vegetationLayer.removeChild(object); - WORLD_CHUNKS.get(chunkId).vegetationObjectsVault.del(objId); - object.destroy(); - } -} - -/** - * - * @param {Number} x world coordinates - * @param {Number} y world coordinates - * @param {PIXI.Container} object object to remove - */ -export function removeFromBuildings(x, y, object) { - let chunkIndex = worldCoordinatesToChunkIndex(x, y); - let chunkId = chunkIndex.x + "_" + chunkIndex.y; - if (WORLD_CHUNKS.existsKey(chunkId)) { - let objLocalPos = worldCoordinatesToChunkIndexesCoordinates(x, y); - let objId = objLocalPos.x + "_" + objLocalPos.y; - WORLD_CHUNKS.get(chunkId).buildingsLayer.removeChild(object); - WORLD_CHUNKS.get(chunkId).buildingsObjectsVault.del(objId); - object.destroy(); - } -} - -/** - * - * @param {Number} x world coordinates - * @param {Number} y world coordinates - */ -export function getObjectFromTerrainLayer(x, y) { - // let t_ceiled = {x: Math.floor(t.x / (BC_TERRAIN_SETTINGS.tileSize*BC_TERRAIN_SETTINGS.scale)), y: Math.floor(t.y / (BC_TERRAIN_SETTINGS.tileSize*BC_TERRAIN_SETTINGS.scale))}; - let w = BC_CHUNKS_SETTINGS.width * BC_TERRAIN_SETTINGS.tileSize * BC_TERRAIN_SETTINGS.scale; - let h = BC_CHUNKS_SETTINGS.height * 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 chunkId = Math.floor(x / w) + "_" + Math.floor(y / h); - let objectId = Math.floor(x / ws) + "_" + Math.floor(y / hs); - if (WORLD_CHUNKS.existsKey(chunkId)) return WORLD_CHUNKS.get(chunkId).terrainObjectsVault.get(objectId); -} -/** - * - * @param {Number} x world coordinates - * @param {Number} y world coordinates - */ -export function getObjectFromVegetationLayer(x, y) { - // let t_ceiled = {x: Math.floor(t.x / (BC_TERRAIN_SETTINGS.tileSize*BC_TERRAIN_SETTINGS.scale)), y: Math.floor(t.y / (BC_TERRAIN_SETTINGS.tileSize*BC_TERRAIN_SETTINGS.scale))}; - let w = BC_CHUNKS_SETTINGS.width * BC_TERRAIN_SETTINGS.tileSize * BC_TERRAIN_SETTINGS.scale; - let h = BC_CHUNKS_SETTINGS.height * 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 chunkId = Math.floor(x / w) + "_" + Math.floor(y / h); - let objectId = Math.floor(x / ws) + "_" + Math.floor(y / hs); - if (WORLD_CHUNKS.existsKey(chunkId)) return WORLD_CHUNKS.get(chunkId).vegetationObjectsVault.get(objectId); -} - -/** - * - * @param {Number} x world coordinates - * @param {Number} y world coordinates - */ -export function getObjectFromBuildingsLayer(x, y) { - // let t_ceiled = {x: Math.floor(t.x / (BC_TERRAIN_SETTINGS.tileSize*BC_TERRAIN_SETTINGS.scale)), y: Math.floor(t.y / (BC_TERRAIN_SETTINGS.tileSize*BC_TERRAIN_SETTINGS.scale))}; - let w = BC_CHUNKS_SETTINGS.width * BC_TERRAIN_SETTINGS.tileSize * BC_TERRAIN_SETTINGS.scale; - let h = BC_CHUNKS_SETTINGS.height * 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 chunkId = Math.floor(x / w) + "_" + Math.floor(y / h); - let objectId = Math.floor(x / ws) + "_" + Math.floor(y / hs); - if (WORLD_CHUNKS.existsKey(chunkId)) return WORLD_CHUNKS.get(chunkId).buildingsObjectsVault.get(objectId); -} - -export function createFirstWorldChunks() { - let w = BC_CHUNKS_SETTINGS.width * BC_TERRAIN_SETTINGS.tileSize * BC_TERRAIN_SETTINGS.scale; - let h = BC_CHUNKS_SETTINGS.height * BC_TERRAIN_SETTINGS.tileSize * BC_TERRAIN_SETTINGS.scale; - for (let i = -1; i < 2; i++) { - for (let j = -1; j < 2; j++) { - let chunkXCeiled = Math.floor((BC_CAMERA.position.x + w * i) / w); - let chunkYCeiled = Math.floor((BC_CAMERA.position.y + h * j) / h); - let chunkId = chunkXCeiled + "_" + chunkYCeiled; - // 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); - } - } - enableAutoGeneration = true; -} - -let noise = new Noise(Math.floor(PRNG() * 188822321)); -let noiseErosion = new Noise(Math.floor(PRNG() * 327749029)); -let noiseBiomes = new Noise(Math.floor(PRNG() * 927472011)); - -let terrainCue = new NumberCue([0, 1, 2, 3, 3], [0.0, 0.45, 0.5, 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]); - -export function fillChunk(chunk, x, y) { - let ii = 0; - let jj = 0; - for (let i = BC_CHUNKS_SETTINGS.width * x; i < BC_CHUNKS_SETTINGS.width * (x + 1); i++) { - jj = 0; - for (let j = BC_CHUNKS_SETTINGS.height * y; j < BC_CHUNKS_SETTINGS.height * (y + 1); j++) { - let res = (noise.simplex2(i * 0.025, j * 0.025) + 1) / 2; - let resR = (noiseErosion.simplex2(i * 0.3, j * 0.3) + 1) / 2; - let resB = (noiseBiomes.simplex2(i * 0.01, j * 0.01) + 1) / 2; - if (resB > 0.7) { - res = clampNumber(res - resR / 4, 0.0, 0.99); - } - if (resB > 0.5 && res < 0.9 && res >= 0.5) { - res = clampNumber(res + resR / 4, 0.0, 0.99); - } - // res = Math.pow(res, 2); - // console.log(resR); - // let resB = (noiseBiomes.perlin2(i * 0.01, j * 0.01) + 1) / 2; - // console.log(Math.pow(resB, 0.5)); - // console.log(res); - // res = clampNumber(res * resB, 0.0, 0.99); - // if(resB < 0.5) - // { - // res = clampNumber(res / 2 , 0.0, 0.99); - // } - // else if(resB < 0.9) - // { - // res = clampNumber(res * 2, 0.0, 0.99); - // } - // res = Math.pow(res, 0.9); - let sTint = new RGBColor(255, 255, 255).multiplyByNumber(terrainTintCue.getValueAt(res)).toNumber(); - res = Math.floor(terrainCue.getValueAt(res)); - let sprite = getSpriteFromAtlas( - "assets/images/world/world_terrain_atlas.png", - new PIXI.Rectangle(terrainSpriteList[res].x, terrainSpriteList[res].y, 16, 16) - ); - sprite.position.set(16 * BC_SPRITES_SETTINGS.scale * ii, 16 * BC_SPRITES_SETTINGS.scale * jj); - sprite.scale.set(BC_SPRITES_SETTINGS.scale, BC_SPRITES_SETTINGS.scale); - sprite.tint = sTint; - addToTerrain(chunk, sprite, { type: terrainTypeList[res] }, i, j); - // console.log(ii, jj); - // addToTerrain(sprite, {x: i, y: j}, {type: terrainTypeList[res]}); - if (res === 2 && PRNG() > 0.9) { - let rv = Math.floor(PRNG() * 18); - - let veg = getSpriteFromAtlas( - "assets/images/world/vegetation_ts.png", - new PIXI.Rectangle(16 * grassVegetationSpriteList[rv].x, 16 * grassVegetationSpriteList[rv].y, 16, 16) - ); - veg.position.set(16 * BC_SPRITES_SETTINGS.scale * ii, 16 * BC_SPRITES_SETTINGS.scale * jj); - veg.scale.set(BC_SPRITES_SETTINGS.scale, BC_SPRITES_SETTINGS.scale); - veg.tint = sTint; - addToVegetation(chunk, veg, { ...grassVegResourcesList[rv] }, i, j); - jj++; - continue; - } - if (res === 1 && PRNG() > 0.99) { - let rv = Math.floor(PRNG() * 6); - - let veg = getSpriteFromAtlas( - "assets/images/world/vegetation_ts.png", - new PIXI.Rectangle(16 * sandVegetationSpriteList[rv].x, 16 * sandVegetationSpriteList[rv].y, 16, 16) - ); - veg.position.set(16 * BC_SPRITES_SETTINGS.scale * ii, 16 * BC_SPRITES_SETTINGS.scale * jj); - veg.scale.set(BC_SPRITES_SETTINGS.scale, BC_SPRITES_SETTINGS.scale); - veg.tint = sTint; - addToVegetation(chunk, veg, { ...sandVegResourcesList[rv] }, i, j); - jj++; - continue; - } - if (res === 3 && PRNG() > 0.9) { - let rv = Math.floor(PRNG() * 3); - - let veg = getSpriteFromAtlas( - "assets/images/world/vegetation_ts.png", - new PIXI.Rectangle(16 * stoneVegetationSpriteList[rv].x, 16 * stoneVegetationSpriteList[rv].y, 16, 16) - ); - veg.position.set(16 * BC_SPRITES_SETTINGS.scale * ii, 16 * BC_SPRITES_SETTINGS.scale * jj); - veg.scale.set(BC_SPRITES_SETTINGS.scale, BC_SPRITES_SETTINGS.scale); - veg.tint = sTint; - addToVegetation(chunk, veg, { ...stoneVegResourcesList[rv] }, i, j); - jj++; - continue; - } - jj++; - } - ii++; - } -} diff --git a/src/Game/WorldGeneration/WorldObjects/BuildingTile/BuildingTile.js b/src/Game/WorldGeneration/WorldObjects/BuildingTile/BuildingTile.js new file mode 100644 index 0000000..0a931ef --- /dev/null +++ b/src/Game/WorldGeneration/WorldObjects/BuildingTile/BuildingTile.js @@ -0,0 +1,41 @@ +import { Rectangle } from "../../../../pixi/pixi.mjs"; +import { SceneObject } from "../../../SceneObjects/SceneObject"; +import { getSpriteFromAtlas } from "../../../Utils/Sprites.utils"; + +export class BuildingTileProps +{ + /** + * @type String + */ + type; + /** + * @type Number + */ + price; + + /** + * + * @param {String} type + * @param {Number} price + */ + constructor(type = "", price = 0) + { + this.type = type; + this.price = price; + }; + + +} + +export class BuildingTile extends SceneObject +{ + props = new BuildingTileProps(); + spriteSheetPath = ""; + frame = new Rectangle(); + + onInit() + { + super.onInit(); + this.drawObject = getSpriteFromAtlas(this.spriteSheetPath, this.frame); + }; +}; \ No newline at end of file diff --git a/src/Game/WorldGeneration/WorldObjects/TerrainTile/TerrainTile.js b/src/Game/WorldGeneration/WorldObjects/TerrainTile/TerrainTile.js index bfed181..98784f9 100644 --- a/src/Game/WorldGeneration/WorldObjects/TerrainTile/TerrainTile.js +++ b/src/Game/WorldGeneration/WorldObjects/TerrainTile/TerrainTile.js @@ -33,20 +33,9 @@ export class TerrainTile extends SceneObject spriteSheetPath = ""; frame = new Rectangle(); - // #printed = false; - onInit() { super.onInit(); this.drawObject = getSpriteFromAtlas(this.spriteSheetPath, this.frame); }; - - // tick(ticker) - // { - // if(!this.#printed) - // { - // console.log(this.props); - // this.#printed = true; - // } - // } }; \ No newline at end of file diff --git a/src/Game/WorldGeneration/WorldObjects/VegetationTile/VegetationTile.js b/src/Game/WorldGeneration/WorldObjects/VegetationTile/VegetationTile.js new file mode 100644 index 0000000..16070cc --- /dev/null +++ b/src/Game/WorldGeneration/WorldObjects/VegetationTile/VegetationTile.js @@ -0,0 +1,53 @@ +import { Rectangle } from "../../../../pixi/pixi.mjs"; +import { GameObject } from "../../../GameObject/GameObject"; +import { PRNG } from "../../../GlobalVariables/GlobalVariables"; +import { SceneObject } from "../../../SceneObjects/SceneObject"; +import { getSpriteFromAtlas } from "../../../Utils/Sprites.utils"; +import { ChunkStorageTypes } from "../../WorldChunk/WorldGenChunk"; + +export class VegetationTileProps +{ + /** + * @type String + */ + type; + /** + * @type any + */ + resourcesList; + + /** + * + * @param {String} type + * @param {any} resourcesList + */ + constructor(type = "", resourcesList = {}) + { + this.type = type; + this.resourcesList = resourcesList; + }; + + +} + +export class VegetationTile extends SceneObject +{ + props = new VegetationTileProps(); + spriteSheetPath = ""; + frame = new Rectangle(); + + /** + * + * @param {Boolean} tickAble + */ + constructor(tickAble = false) + { + super(tickAble); + } + + onInit() + { + super.onInit(); + this.drawObject = getSpriteFromAtlas(this.spriteSheetPath, this.frame); + }; +}; \ No newline at end of file