added some types in WorldGeneration.js and new map alias - Vault class

This commit is contained in:
TorgaW 2024-04-09 18:22:29 +03:00
parent fe2299f864
commit aab9708ba8
5 changed files with 218 additions and 112 deletions

View File

@ -145,6 +145,7 @@ function setupInGameSelector() {
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)

View File

@ -0,0 +1,72 @@
/**
* map representation
*/
export class Vault {
constructor(name = "") {
this.name = name;
this.storage = {};
/**
* set key-value pair in storage
* @param {String} key
* @param {*} value
*/
this.set = (key, value) => {
this.storage[key] = value;
};
/**
* return element from storage
* @param {String} key
* @returns element (ref) or undefined
*/
this.get = (key) => {
return this.storage[key];
};
/**
*
* @param {String} key
* @returns true/false
*/
this.existsKey = (key) => {
return Object.keys(this.storage).includes(key);
};
/**
* remove key from storage
* @param {String} key
* @returns true/false
*/
this.del = (key) => {
return delete this.storage[key];
}
/**
* clears storage
*/
this.clear = () => {this.storage = {}}
/**
*
* @param {*} value
* @returns found value (ref) or false
*/
this.find = (value) => {
let t = Object.keys(this.storage);
for (const i of t) {
if (this.storage[i] === value) return this.storage[i];
}
return false;
};
/**
* run 'for' loop through all storage and call 'fn(storage_element)'
* @param {Function} fn
*/
this.forAll = (fn) => {
let keys = Object.keys(this.storage);
for (const i of keys) {
fn(this.storage[i]);
}
};
}
}

View File

@ -1,6 +1,7 @@
import * as PIXI from "../../pixi/pixi.mjs";
import { Vault } from "../Utils/DataTypes.utils";
export function createWorldChunk() {
export function createWorldChunkContainer() {
let chunk = new PIXI.Container();
let terrainLayer = new PIXI.Container();
let vegetationLayer = new PIXI.Container();
@ -12,56 +13,23 @@ export function createWorldChunk() {
}
/**
*
* @param {String} type
*
* @param {PIXI.Container} chunk
* @param {PIXI.Container} terrainLayer
* @param {PIXI.Container} vegetationLayer
* @param {PIXI.Container} buildingsLayer
* @param {Vault} terrainObjectsVault
* @param {Vault} vegetationObjectsVault
* @param {Vault} buildingsObjectsVault
*/
export function terrainObjectProps(type) {
this.type = type;
};
/**
*
* @param {String} type
*/
export function buildingObjectProps(type) {
this.type = type;
};
/**
*
* @param {String} type
* @param {Number} num
*/
export function vegetationObjectProps(type, num) {
this.type = type;
this.num = num;
};
/**
*
* @param {terrainObjectProps} obj
*/
export function terrainObjectsVaultType(obj)
{
}
/**
*
* @param {PIXI.Container} chunk
* @param {PIXI.Container} terrainLayer
* @param {PIXI.Container} vegetationLayer
* @param {PIXI.Container} buildingsLayer
* @param {terrainObjectsVaultType} terrainObjectsVault
* @param {vegetationObjectProps} vegetationObjectsVault
* @param {buildingObjectProps} buildingsObjectsVault
*/
export function WorldChunk(chunk, terrainLayer, vegetationLayer, buildingsLayer, terrainObjectsVault, vegetationObjectsVault, buildingsObjectsVault) {
this.chunk = chunk;
this.terrainLayer = terrainLayer;
this.vegetationLayer = vegetationLayer;
this.buildingsLayer = buildingsLayer;
this.terrainObjectsVault = terrainObjectsVault;
this.vegetationObjectsVault = vegetationObjectsVault;
this.buildingsObjectsVault = buildingsObjectsVault;
}
export class WorldChunk {
constructor(chunk, terrainLayer, vegetationLayer, buildingsLayer, terrainObjectsVault, vegetationObjectsVault, buildingsObjectsVault) {
this.chunk = chunk;
this.terrainLayer = terrainLayer;
this.vegetationLayer = vegetationLayer;
this.buildingsLayer = buildingsLayer;
this.terrainObjectsVault = terrainObjectsVault;
this.vegetationObjectsVault = vegetationObjectsVault;
this.buildingsObjectsVault = buildingsObjectsVault;
}
}

View File

@ -0,0 +1,45 @@
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;
}

View File

@ -2,9 +2,11 @@ import { Noise } from "noisejs";
import { BC_CAMERA, BC_CHUNKS_SETTINGS, BC_SPRITES_SETTINGS, BC_TERRAIN_SETTINGS, PRNG } from "../GlobalVariables/GlobalVariables";
import { integerDivision } from "../Utils/Math.utils";
import { addToViewport } from "../Utils/World.utils";
import { WorldChunk, createWorldChunk } from "../WorldChunk/WorldChunk";
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 { Vault } from "../Utils/DataTypes.utils";
// import {WorldChunk} from "../Types/WorldGenerationTypes";
const terrainSpriteList = {
@ -88,17 +90,8 @@ const stoneVegResourcesList = {
2: { type: "stone", num: 5 / 5 },
};
/**
* {
* chunk: PIXI.Container - chunk,
terrainLayer: PIXI.Container - 0 index child,
vegetationLayer: PIXI.Container - 1st child,
terrainObjectsVault: {},
vegetationObjectsVault: {}
* }
*/
const WORLD_CHUNKS = {};
let VISIBLE_CHUNKS = {};
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;
@ -142,7 +135,7 @@ export function worldCoordinatesToChunkLocalCoordinates(x, y) {
*/
export function getChunk(x, y) {
let t = worldCoordinatesToChunkIndex(x, y);
if (WORLD_CHUNKS[t.x + "_" + t.y]) return WORLD_CHUNKS[t.x + "_" + t.y];
if (WORLD_CHUNKS.existsKey(t.x + "_" + t.y)) return WORLD_CHUNKS.get(t.x + "_" + t.y);
else return undefined;
}
@ -154,29 +147,37 @@ export function updateChunksVisibility() {
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[chunkId] && !VISIBLE_CHUNKS[chunkId].central) || !VISIBLE_CHUNKS[chunkId]) {
for (const key in VISIBLE_CHUNKS) {
VISIBLE_CHUNKS[key].chunkRef.chunk.visible = false;
}
VISIBLE_CHUNKS = {};
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 = -1; i < 2; i++) {
for (let j = -1; j < 2; j++) {
let t_chunkId = cx + i + "_" + (cy + j);
if (WORLD_CHUNKS[t_chunkId]) {
VISIBLE_CHUNKS[t_chunkId] = { chunkRef: WORLD_CHUNKS[t_chunkId], central: i === 0 && j === 0 };
VISIBLE_CHUNKS[t_chunkId].chunkRef.chunk.visible = true;
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 = createWorldChunk();
// 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), {}, {}, {});
let chunk0 = new WorldChunk(
newChunk,
newChunk.getChildAt(0),
newChunk.getChildAt(1),
newChunk.getChildAt(2),
new Vault("terrain"),
new Vault("vegetation"),
new Vault("buildings")
);
fillChunk(chunk0, cx + i, cy + j);
WORLD_CHUNKS[t_chunkId] = chunk0;
WORLD_CHUNKS.set(t_chunkId, chunk0);
addToViewport(newChunk);
VISIBLE_CHUNKS[t_chunkId] = { chunkRef: WORLD_CHUNKS[t_chunkId], central: i === 0 && j === 0 };
VISIBLE_CHUNKS[t_chunkId].chunkRef.chunk.visible = true;
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;
}
}
}
@ -185,41 +186,41 @@ export function updateChunksVisibility() {
/**
*
* @param {*} chunk from WORLD_CHUNKS
* @param {WorldChunk} chunk from WORLD_CHUNKS
* @param {PIXI.Container} object
* @param {*} props any props
* @param {TerrainObjectProps} props
* @param {Number} ceiledX
* @param {Number} ceiledY
*/
export function addToTerrain(chunk, object, props = {}, ceiledX, ceiledY) {
export function addToTerrain(chunk, object, props, ceiledX, ceiledY) {
chunk.terrainLayer.addChild(object);
chunk.terrainObjectsVault[ceiledX + "_" + ceiledY] = { obj: object, ...props };
chunk.terrainObjectsVault.set(ceiledX + "_" + ceiledY, new TerrainObjectProps(object, { ...props }));
}
/**
*
* @param {*} chunk from WORLD_CHUNKS
* @param {WorldChunk} chunk from WORLD_CHUNKS
* @param {PIXI.Container} object
* @param {*} props any props
* @param {VegetationObjectProps} props
* @param {Number} ceiledX
* @param {Number} ceiledY
*/
export function addToVegetation(chunk, object, props = {}, ceiledX, ceiledY) {
export function addToVegetation(chunk, object, props, ceiledX, ceiledY) {
chunk.vegetationLayer.addChild(object);
chunk.vegetationObjectsVault[ceiledX + "_" + ceiledY] = { obj: object, ...props };
chunk.vegetationObjectsVault.set(ceiledX + "_" + ceiledY, new VegetationObjectProps(object, { ...props }));
}
/**
*
* @param {*} chunk from WORLD_CHUNKS
* @param {WorldChunk} chunk from WORLD_CHUNKS
* @param {PIXI.Container} object
* @param {*} props any props
* @param {BuildingsObjectProps} props
* @param {Number} ceiledX
* @param {Number} ceiledY
*/
export function addToBuildings(chunk, object, props = {}, ceiledX, ceiledY) {
chunk.buildingsLayer.addChild(object);
chunk.buildingsObjectsVault[ceiledX + "_" + ceiledY] = { obj: object, ...props };
chunk.buildingsObjectsVault.set(ceiledX + "_" + ceiledY, new BuildingsObjectProps(object, { ...props }));
}
/**
@ -231,11 +232,11 @@ export function addToBuildings(chunk, object, props = {}, ceiledX, ceiledY) {
export function removeFromTerrain(x, y, object) {
let chunkIndex = worldCoordinatesToChunkIndex(x, y);
let chunkId = chunkIndex.x + "_" + chunkIndex.y;
if (WORLD_CHUNKS[chunkId]) {
if (WORLD_CHUNKS.existsKey(chunkId)) {
let objLocalPos = worldCoordinatesToChunkIndexesCoordinates(x, y);
let objId = objLocalPos.x + "_" + objLocalPos.y;
WORLD_CHUNKS[chunkId].terrainLayer.removeChild(object);
WORLD_CHUNKS[chunkId].terrainObjectsVault[objId] = undefined;
WORLD_CHUNKS.get(chunkId).terrainLayer.removeChild(object);
WORLD_CHUNKS.get(chunkId).terrainObjectsVault.del(objId);
object.destroy();
}
}
@ -248,11 +249,29 @@ export function removeFromTerrain(x, y, object) {
export function removeFromVegetation(x, y, object) {
let chunkIndex = worldCoordinatesToChunkIndex(x, y);
let chunkId = chunkIndex.x + "_" + chunkIndex.y;
if (WORLD_CHUNKS[chunkId]) {
if (WORLD_CHUNKS.existsKey(chunkId)) {
let objLocalPos = worldCoordinatesToChunkIndexesCoordinates(x, y);
let objId = objLocalPos.x + "_" + objLocalPos.y;
WORLD_CHUNKS[chunkId].vegetationLayer.removeChild(object);
WORLD_CHUNKS[chunkId].vegetationObjectsVault[objId] = undefined;
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();
}
}
@ -270,7 +289,7 @@ export function getObjectFromTerrainLayer(x, y) {
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[chunkId]) return WORLD_CHUNKS[chunkId].terrainObjectsVault[objectId];
if (WORLD_CHUNKS.existsKey(chunkId)) return WORLD_CHUNKS.get(chunkId).terrainObjectsVault.get(objectId);
}
/**
*
@ -285,7 +304,7 @@ export function getObjectFromVegetationLayer(x, y) {
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[chunkId]) return WORLD_CHUNKS[chunkId].vegetationObjectsVault[objectId];
if (WORLD_CHUNKS.existsKey(chunkId)) return WORLD_CHUNKS.get(chunkId).vegetationObjectsVault.get(objectId);
}
/**
@ -301,7 +320,7 @@ export function getObjectFromBuildingsLayer(x, y) {
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[chunkId]) return WORLD_CHUNKS[chunkId].buildingsObjectsVault[objectId];
if (WORLD_CHUNKS.existsKey(chunkId)) return WORLD_CHUNKS.get(chunkId).buildingsObjectsVault.get(objectId);
}
export function createFirstWorldChunks() {
@ -312,25 +331,26 @@ export function createFirstWorldChunks() {
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 = createWorldChunk();
// console.log(chunkId);
let chunkRef = createWorldChunkContainer();
// chunkRef.isRenderGroup = true;
chunkRef.position.set(w * chunkXCeiled, h * chunkYCeiled);
// console.log(w * chunkXCeiled, h * chunkYCeiled);
let chunk0 = {
chunk: chunkRef,
terrainLayer: chunkRef.getChildAt(0),
vegetationLayer: chunkRef.getChildAt(1),
buildingsLayer: chunkRef.getChildAt(2),
terrainObjectsVault: {},
vegetationObjectsVault: {},
buildingsObjectsVault: {},
};
let chunk0 = new WorldChunk(
chunkRef,
chunkRef.getChildAt(0),
chunkRef.getChildAt(1),
chunkRef.getChildAt(2),
new Vault("terrain"),
new Vault("vegetation"),
new Vault("buildings")
);
fillChunk(chunk0, chunkXCeiled, chunkYCeiled);
WORLD_CHUNKS[chunkId] = chunk0;
WORLD_CHUNKS.set(chunkId, chunk0);
chunkRef.visible = false;
addToViewport(chunkRef);
// console.log(WORLD_CHUNKS)
// console.log(chunkXCeiled, chunkYCeiled);
}
}