improve memory usage

This commit is contained in:
LabyStudio
2022-05-12 04:11:54 +02:00
parent 24c0451031
commit b055293dd4
2 changed files with 13 additions and 32 deletions
+1
View File
@@ -74,6 +74,7 @@ _Note: All textures from the original game were used for the screenshots only!_
### Licensing ### Licensing
- The main rendering library is [three.js](https://github.com/mrdoob/three.js/) - The main rendering library is [three.js](https://github.com/mrdoob/three.js/)
- 64 bits number implementation by [long.js](https://github.com/dcodeIO/long.js)
- All used sound resources are taken from [freesounds.org](https://freesound.org/people/C418/downloaded_sounds/?page=8#sound) - All used sound resources are taken from [freesounds.org](https://freesound.org/people/C418/downloaded_sounds/?page=8#sound)
<hr> <hr>
@@ -30,19 +30,7 @@ export default class ChunkSection {
this.blocksData = []; this.blocksData = [];
this.blockLight = []; this.blockLight = [];
this.skyLight = []; this.skyLight = [];
this.empty = true;
// Fill chunk with air and light
for (let tX = 0; tX < ChunkSection.SIZE; tX++) {
for (let tY = 0; tY < ChunkSection.SIZE; tY++) {
for (let tZ = 0; tZ < ChunkSection.SIZE; tZ++) {
let index = tY << 8 | tZ << 4 | tX;
this.blocks[index] = 0;
this.blocksData[index] = 0;
this.blockLight[index] = 0;
this.skyLight[index] = 14;
}
}
}
} }
render() { render() {
@@ -91,25 +79,27 @@ export default class ChunkSection {
getBlockAt(x, y, z) { getBlockAt(x, y, z) {
let index = y << 8 | z << 4 | x; let index = y << 8 | z << 4 | x;
return this.blocks[index]; return !this.empty && index in this.blocks ? this.blocks[index] : 0;
} }
getBlockDataAt(x, y, z) { getBlockDataAt(x, y, z) {
let index = y << 8 | z << 4 | x; let index = y << 8 | z << 4 | x;
return this.blocksData[index]; return !this.empty && index in this.blocksData ? this.blocksData[index] : 0;
} }
setBlockAt(x, y, z, typeId) { setBlockAt(x, y, z, typeId) {
let index = y << 8 | z << 4 | x; let index = y << 8 | z << 4 | x;
this.blocks[index] = typeId; this.blocks[index] = typeId;
this.isModified = true; this.isModified = true;
if (this.empty && typeId !== 0) {
this.empty = false;
}
} }
setBlockDataAt(x, y, z, data) { setBlockDataAt(x, y, z, data) {
let index = y << 8 | z << 4 | x; let index = y << 8 | z << 4 | x;
this.blocksData[index] = data; this.blocksData[index] = data;
this.isModified = true; this.isModified = true;
} }
@@ -128,8 +118,8 @@ export default class ChunkSection {
getTotalLightAt(x, y, z) { getTotalLightAt(x, y, z) {
let index = y << 8 | z << 4 | x; let index = y << 8 | z << 4 | x;
let skyLight = this.skyLight[index] - this.world.skylightSubtracted; let skyLight = (!this.empty && index in this.skyLight ? this.skyLight[index] : (this.empty ? 15 : 14)) - this.world.skylightSubtracted;
let blockLight = this.blockLight[index]; let blockLight = !this.empty && index in this.blockLight ? this.blockLight[index] : 0;
if (blockLight > skyLight) { if (blockLight > skyLight) {
skyLight = blockLight; skyLight = blockLight;
} }
@@ -139,25 +129,15 @@ export default class ChunkSection {
getLightAt(sourceType, x, y, z) { getLightAt(sourceType, x, y, z) {
let index = y << 8 | z << 4 | x; let index = y << 8 | z << 4 | x;
if (sourceType === EnumSkyBlock.SKY) { if (sourceType === EnumSkyBlock.SKY) {
return this.skyLight[index]; return !this.empty && index in this.skyLight ? this.skyLight[index] : (this.empty ? 15 : 14);
} }
if (sourceType === EnumSkyBlock.BLOCK) { if (sourceType === EnumSkyBlock.BLOCK) {
return this.blockLight[index]; return !this.empty && index in this.blockLight ? this.blockLight[index] : 0;
} }
return 0; return 0;
} }
isEmpty() { isEmpty() {
for (let x = 0; x < ChunkSection.SIZE; x++) { return this.empty;
for (let y = 0; y < ChunkSection.SIZE; y++) {
for (let z = 0; z < ChunkSection.SIZE; z++) {
let index = y << 8 | z << 4 | x;
if (this.blocks[index] !== 0) {
return false;
}
}
}
}
return true;
} }
} }