implement torch block and block lightning

This commit is contained in:
LabyStudio
2022-02-13 02:54:57 +01:00
parent 77fd18af31
commit 7858e98ffa
14 changed files with 293 additions and 47 deletions
@@ -45,6 +45,60 @@ window.Chunk = class {
this.setModifiedAllSections();
}
generateBlockLightMap() {
let targetY = 32;
for (let x = 0; x < 16; x++) {
for (let z = 0; z < 16; z++) {
for (let y = 0; y < World.TOTAL_HEIGHT; y++) {
let section = this.getSection(y >> 4);
let typeId = section.getBlockAt(x, y & 15, z);
let block = Block.getById(typeId);
let blockLight = typeId === 0 ? 0 : block.getLightValue();
if (blockLight > 0) {
section.setLightAt(EnumSkyBlock.BLOCK, x, y & 15, z, blockLight);
}
}
let level = 15;
for (let y = targetY - 2; y < 128 && level > 0;) {
y++;
let section = this.getSection(y >> 4);
let typeId = section.getBlockAt(x, y & 15, z);
let block = Block.getById(typeId);
let opacity = block.getOpacity();
let blockLight = typeId === 0 ? 0 : block.getLightValue();
if (opacity === 0) {
opacity = 1;
}
level -= opacity;
if (blockLight > level) {
level = blockLight;
}
if (level < 0) {
level = 0;
}
section.setLightAt(EnumSkyBlock.BLOCK, x, y & 15, z, blockLight);
}
}
}
this.world.updateLight(EnumSkyBlock.Block,
this.x * 16, targetY - 1, this.z * 16,
this.x * 16 + 16, targetY + 1, this.z * 16 + 16
);
this.setModifiedAllSections();
}
updateBlockLight() {
this.setModifiedAllSections();
}
@@ -99,7 +99,7 @@ window.ChunkSection = class {
getTotalLightAt(x, y, z) {
let index = y << 8 | z << 4 | x;
let skyLight = this.skyLight[index];
let skyLight = this.skyLight[index] - this.world.skylightSubtracted;
let blockLight = this.blockLight[index];
if (blockLight > skyLight) {
skyLight = blockLight;
+30 -5
View File
@@ -39,6 +39,15 @@ window.World = class {
return distance2 - distance1;
});
// Update skylight subtracted (To make the night dark)
let lightLevel = this.calculateSkylightSubtracted(1.0);
if (lightLevel !== this.skylightSubtracted) {
this.skylightSubtracted = lightLevel;
// Rebuild all chunks
this.minecraft.worldRenderer.rebuildAll();
}
// Update world time
this.time++;
}
@@ -54,6 +63,7 @@ window.World = class {
// Init
chunk.generateSkylightMap();
chunk.generateBlockLightMap();
// Register
chunk.loaded = true;
@@ -171,9 +181,12 @@ window.World = class {
level = 15;
}
} else if (sourceType === EnumSkyBlock.BLOCK) {
let i1 = this.getBlockAt(x, y, z);
if (0 > level) { // TODO
level = 0;
let typeId = this.getBlockAt(x, y, z);
let block = Block.getById(typeId);
let blockLight = typeId === 0 ? 0 : block.getLightValue();
if (blockLight > level) {
level = blockLight;
}
}
if (this.getSavedLightValue(sourceType, x, y, z) !== level) {
@@ -318,7 +331,7 @@ window.World = class {
let blockId = this.getBlockAt(x, y, z);
let block = Block.getById(blockId);
if (block != null && block.canCollide()) {
if (block != null && block.canInteract()) {
let hit = block.collisionRayTrace(x, y, z, from, to);
if (hit != null) {
return hit;
@@ -412,7 +425,7 @@ window.World = class {
let blockId = this.getBlockAt(x, y, z);
let block = Block.getById(blockId);
if (block != null && block.canCollide()) {
if (block != null && block.canInteract()) {
let hit = block.collisionRayTrace(x, y, z, from, to);
if (hit != null) {
return hit;
@@ -466,4 +479,16 @@ window.World = class {
}
return MathHelper.hsbToRgb(0.6222222 - temperature * 0.05, 0.5 + temperature * 0.1, 1.0);
}
calculateSkylightSubtracted(partialTicks) {
let angle = this.getCelestialAngle(partialTicks);
let level = 1.0 - (Math.cos(angle * 3.141593 * 2.0) * 2.0 + 0.5);
if (level < 0.0) {
level = 0.0;
}
if (level > 1.0) {
level = 1.0;
}
return Math.floor(level * 11);
}
}
@@ -1,7 +1,7 @@
window.Block = class {
static blocks = new Map();
static create() {
Block.STONE = new BlockStone(1, 0);
Block.GRASS = new BlockGrass(2, 1);
@@ -10,6 +10,7 @@ window.Block = class {
Block.LEAVE = new BlockLeave(18, 6);
Block.WATER = new BlockWater(9, 7);
Block.SAND = new BlockSand(12, 8)
Block.TORCH = new BlockTorch(50, 9)
}
constructor(id, textureSlotId = id) {
@@ -26,6 +27,10 @@ window.Block = class {
return this.id;
}
getRenderType() {
return BlockRenderType.BLOCK;
}
getTextureForFace(face) {
return this.textureSlotId;
}
@@ -36,7 +41,7 @@ window.Block = class {
shouldRenderFace(world, x, y, z, face) {
let typeId = world.getBlockAtFace(x, y, z, face);
return typeId === 0 || Block.getById(typeId).isTransparent();
return typeId === 0 || !Block.getById(typeId).isSolid();
}
getLightValue() {
@@ -51,7 +56,7 @@ window.Block = class {
return 1.0;
}
canCollide() {
canInteract() {
return true;
}
@@ -0,0 +1,20 @@
window.BlockTorch = class extends Block {
constructor(id, textureSlotId) {
super(id, textureSlotId);
this.boundingBox = new BoundingBox(0.4, 0.0, 0.4, 0.6, 0.6, 0.6);
}
getLightValue() {
return 14;
}
isSolid() {
return false;
}
getRenderType() {
return BlockRenderType.TORCH;
}
}
@@ -12,7 +12,7 @@ window.BlockWater = class extends Block {
return false;
}
canCollide() {
canInteract() {
return false;
}