diff --git a/src/js/net/minecraft/client/inventory/Inventory.js b/src/js/net/minecraft/client/inventory/Inventory.js index 55c5309..0693259 100644 --- a/src/js/net/minecraft/client/inventory/Inventory.js +++ b/src/js/net/minecraft/client/inventory/Inventory.js @@ -3,6 +3,15 @@ window.Inventory = class { constructor() { this.selectedSlotIndex = 0; this.items = []; + + // Default items in inventory + this.items[0] = 1; + this.items[1] = 2; + this.items[2] = 3; + this.items[3] = 17; + this.items[4] = 18; + this.items[5] = 12; + this.items[6] = 50; } setItemInSelectedSlot(typeId) { diff --git a/src/js/net/minecraft/client/render/BlockRenderer.js b/src/js/net/minecraft/client/render/BlockRenderer.js index 16ba948..1825c34 100644 --- a/src/js/net/minecraft/client/render/BlockRenderer.js +++ b/src/js/net/minecraft/client/render/BlockRenderer.js @@ -9,6 +9,17 @@ window.BlockRenderer = class { } renderBlock(world, block, x, y, z) { + switch (block.getRenderType()) { + case BlockRenderType.BLOCK: + this.renderSolidBlock(world, block, x, y, z); + break; + case BlockRenderType.TORCH: + this.renderTorch(world, block, x, y, z); + break; + } + } + + renderSolidBlock(world, block, x, y, z) { let boundingBox = block.getBoundingBox(world, x, y, z); // Render all faces @@ -17,7 +28,7 @@ window.BlockRenderer = class { let face = values[i]; // Check if face is hidden by other block - if (block.shouldRenderFace(world, x, y, z, face)) { + if (world === null || block.shouldRenderFace(world, x, y, z, face)) { // Render face this.renderFace(world, block, boundingBox, face, x, y, z); @@ -53,41 +64,46 @@ window.BlockRenderer = class { this.tessellator.setColor(color, color, color); } + // Add face to tessellator + this.addFace(world, face, minX, minY, minZ, maxX, maxY, maxZ, minU, minV, maxU, maxV); + } + + addFace(world, face, minX, minY, minZ, maxX, maxY, maxZ, minU, minV, maxU, maxV) { if (face === EnumBlockFace.BOTTOM) { - this.addBlockCorner(world, face, minX, minY, maxZ, minU, maxV); - this.addBlockCorner(world, face, minX, minY, minZ, minU, minV); - this.addBlockCorner(world, face, maxX, minY, minZ, maxU, minV); this.addBlockCorner(world, face, maxX, minY, maxZ, maxU, maxV); + this.addBlockCorner(world, face, maxX, minY, minZ, maxU, minV); + this.addBlockCorner(world, face, minX, minY, minZ, minU, minV); + this.addBlockCorner(world, face, minX, minY, maxZ, minU, maxV); } if (face === EnumBlockFace.TOP) { - this.addBlockCorner(world, face, maxX, maxY, maxZ, maxU, maxV); - this.addBlockCorner(world, face, maxX, maxY, minZ, maxU, minV); - this.addBlockCorner(world, face, minX, maxY, minZ, minU, minV); this.addBlockCorner(world, face, minX, maxY, maxZ, minU, maxV); + this.addBlockCorner(world, face, minX, maxY, minZ, minU, minV); + this.addBlockCorner(world, face, maxX, maxY, minZ, maxU, minV); + this.addBlockCorner(world, face, maxX, maxY, maxZ, maxU, maxV); } if (face === EnumBlockFace.NORTH) { - this.addBlockCorner(world, face, minX, maxY, minZ, maxU, minV); - this.addBlockCorner(world, face, maxX, maxY, minZ, minU, minV); - this.addBlockCorner(world, face, maxX, minY, minZ, minU, maxV); - this.addBlockCorner(world, face, minX, minY, minZ, maxU, maxV); - } - if (face === EnumBlockFace.SOUTH) { - this.addBlockCorner(world, face, minX, maxY, maxZ, minU, minV); - this.addBlockCorner(world, face, minX, minY, maxZ, minU, maxV); - this.addBlockCorner(world, face, maxX, minY, maxZ, maxU, maxV); - this.addBlockCorner(world, face, maxX, maxY, maxZ, maxU, minV); - } - if (face === EnumBlockFace.WEST) { - this.addBlockCorner(world, face, minX, maxY, maxZ, maxU, minV); this.addBlockCorner(world, face, minX, maxY, minZ, minU, minV); this.addBlockCorner(world, face, minX, minY, minZ, minU, maxV); - this.addBlockCorner(world, face, minX, minY, maxZ, maxU, maxV); - } - if (face === EnumBlockFace.EAST) { - this.addBlockCorner(world, face, maxX, minY, maxZ, minU, maxV); this.addBlockCorner(world, face, maxX, minY, minZ, maxU, maxV); this.addBlockCorner(world, face, maxX, maxY, minZ, maxU, minV); + } + if (face === EnumBlockFace.SOUTH) { + this.addBlockCorner(world, face, minX, maxY, maxZ, maxU, minV); this.addBlockCorner(world, face, maxX, maxY, maxZ, minU, minV); + this.addBlockCorner(world, face, maxX, minY, maxZ, minU, maxV); + this.addBlockCorner(world, face, minX, minY, maxZ, maxU, maxV); + } + if (face === EnumBlockFace.WEST) { + this.addBlockCorner(world, face, minX, minY, maxZ, minU, maxV); + this.addBlockCorner(world, face, minX, minY, minZ, maxU, maxV); + this.addBlockCorner(world, face, minX, maxY, minZ, maxU, minV); + this.addBlockCorner(world, face, minX, maxY, maxZ, minU, minV); + } + if (face === EnumBlockFace.EAST) { + this.addBlockCorner(world, face, maxX, maxY, maxZ, maxU, minV); + this.addBlockCorner(world, face, maxX, maxY, minZ, minU, minV); + this.addBlockCorner(world, face, maxX, minY, minZ, minU, maxV); + this.addBlockCorner(world, face, maxX, minY, maxZ, maxU, maxV); } } @@ -127,7 +143,7 @@ window.BlockRenderer = class { let typeId = world.getBlockAt(x + offsetX, y + offsetY, z + offsetZ); // Does it contain air? - if (typeId === 0 || Block.getById(typeId).isTransparent()) { + if (typeId === 0 || !Block.getById(typeId).isSolid()) { // Sum up the light levels totalLightLevel += world.getTotalLightAt(x + offsetX, y + offsetY, z + offsetZ); @@ -141,25 +157,111 @@ window.BlockRenderer = class { return totalBlocks === 0 ? 0 : totalLightLevel / totalBlocks; } + renderTorch(world, block, x, y, z) { + let boundingBox = block.getBoundingBox(world, x, y, z); + + // Thickness of the torch + let size = 1 / 16; + + // Vertex mappings + let minX = x + 0.5 - size; + let minY = y; + let minZ = z + 0.5 - size; + let maxX = x + 0.5 + size; + let maxY = y + 10 / 16; + let maxZ = z + 0.5 + size; + + // UV Mapping + let textureIndex = block.getTextureForFace(EnumBlockFace.NORTH); + let minU = (textureIndex % 16) / 16.0; + let minV = Math.floor(textureIndex / 16) / 16.0; + + // Cut to torch texture at 7:6 + minU += 7 / 256; + minV += 6 / 256; + + // Size of torch texture (2x10) + let maxU = minU + 2 / 256; + let maxV = minV + 10 / 256; + + // Flip V + minV = 1 - minV; + maxV = 1 - maxV; + + // Set color with shading + this.tessellator.setColor(1, 1, 1); + + // Add faceS to tessellator + this.addFace(world, EnumBlockFace.NORTH, minX, minY, minZ, maxX, maxY, maxZ, minU, minV, maxU, maxV); + this.addFace(world, EnumBlockFace.EAST, minX, minY, minZ, maxX, maxY, maxZ, minU, minV, maxU, maxV); + this.addFace(world, EnumBlockFace.SOUTH, minX, minY, minZ, maxX, maxY, maxZ, minU, minV, maxU, maxV); + this.addFace(world, EnumBlockFace.WEST, minX, minY, minZ, maxX, maxY, maxZ, minU, minV, maxU, maxV); + this.addFace(world, EnumBlockFace.TOP, minX, minY, minZ, maxX, maxY, maxZ, minU, minV, maxU, maxV + 8 / 256); + } + + renderGuiItem(block) { + // Vertex mappings + let minX = 0; + let minY = 0; + let minZ = 0; + let maxX = 1; + let maxY = 1; + let maxZ = 1; + + // UV Mapping + let textureIndex = block.getTextureForFace(EnumBlockFace.NORTH); + let minU = (textureIndex % 16) / 16.0; + let maxU = minU + (16 / 256); + let minV = Math.floor(textureIndex / 16) / 16.0; + let maxV = minV + (16 / 256); + + // Flip V + minV = 1 - minV; + maxV = 1 - maxV; + + // Render item + this.addFace(null, EnumBlockFace.NORTH, minX, minY, minZ, maxX, maxY, maxZ, minU, minV, maxU, maxV); + } + renderGuiBlock(group, block, x, y, size) { this.tessellator.startDrawing(); let boundingBox = block.getBoundingBox(null, 0, 0, 0); - this.renderFace(null, block, boundingBox, EnumBlockFace.TOP, 0, 0, 0); - this.renderFace(null, block, boundingBox, EnumBlockFace.NORTH, 0, 0, 0); - this.renderFace(null, block, boundingBox, EnumBlockFace.EAST, 0, 0, 0); + // Render block by type + switch (block.getRenderType()) { + case BlockRenderType.BLOCK: + this.renderFace(null, block, boundingBox, EnumBlockFace.TOP, 0, 0, 0); + this.renderFace(null, block, boundingBox, EnumBlockFace.NORTH, 0, 0, 0); + this.renderFace(null, block, boundingBox, EnumBlockFace.EAST, 0, 0, 0); + break; + default: + this.renderGuiItem(block); + break; + } + + // Create mesh let mesh = this.tessellator.draw(group); mesh.geometry.center(); - mesh.rotation.x = -MathHelper.toRadians(45 / 1.5); - mesh.rotation.y = MathHelper.toRadians(45); + // Rotate block + switch (block.getRenderType()) { + case BlockRenderType.BLOCK: + mesh.rotation.x = MathHelper.toRadians(45 / 1.5); + mesh.rotation.y = -MathHelper.toRadians(45 + 90); + break; + default: + mesh.rotation.y = MathHelper.toRadians(180); + size += 5; + break; + } + // Relative position mesh.position.x = x; mesh.position.y = -y; - mesh.position.z = -3; + mesh.position.z = -10; - //let scale = Math.cos(Date.now() / 1000) * 100; + // Scale mesh.scale.x = size; mesh.scale.y = size; mesh.scale.z = size; diff --git a/src/js/net/minecraft/client/render/Tessellator.js b/src/js/net/minecraft/client/render/Tessellator.js index 758cbe3..b3d8a2e 100644 --- a/src/js/net/minecraft/client/render/Tessellator.js +++ b/src/js/net/minecraft/client/render/Tessellator.js @@ -3,7 +3,7 @@ window.Tessellator = class { constructor() { this.material = new THREE.MeshBasicMaterial({ vertexColors: THREE.VertexColors, - side: THREE.BackSide, + side: THREE.FrontSide, transparent: true, depthTest: true }); diff --git a/src/js/net/minecraft/client/render/WorldRenderer.js b/src/js/net/minecraft/client/render/WorldRenderer.js index 91c77a5..1ffe78e 100644 --- a/src/js/net/minecraft/client/render/WorldRenderer.js +++ b/src/js/net/minecraft/client/render/WorldRenderer.js @@ -17,6 +17,11 @@ window.WorldRenderer = class { this.textureSun.magFilter = THREE.NearestFilter; this.textureSun.minFilter = THREE.NearestFilter; + // Load moon texture + this.textureMoon = new THREE.TextureLoader().load('src/resources/terrain/moon.png'); + this.textureMoon.magFilter = THREE.NearestFilter; + this.textureMoon.minFilter = THREE.NearestFilter; + // Block Renderer this.blockRenderer = new BlockRenderer(this); @@ -181,6 +186,13 @@ window.WorldRenderer = class { } } + rebuildAll() { + let world = this.minecraft.world; + for (let [index, chunk] of world.chunks) { + chunk.setModifiedAllSections(); + } + } + generateSky() { // Create sky group this.skyGroup = new THREE.Scene(); @@ -188,19 +200,32 @@ window.WorldRenderer = class { // Create sun let geometry = new THREE.PlaneGeometry(1, 1); - let material = new THREE.MeshBasicMaterial({ - color: 0xffff00, + let materialSun = new THREE.MeshBasicMaterial({ side: THREE.FrontSide, map: this.textureSun, alphaMap: this.textureSun, blending: THREE.AdditiveBlending, transparent: true }); - this.sun = new THREE.Mesh(geometry, material); + this.sun = new THREE.Mesh(geometry, materialSun); this.sun.translateZ(-2); this.sun.renderOrder = 999; this.sun.material.depthTest = false; this.skyGroup.add(this.sun); + + // Create moon + let materialMoon = new THREE.MeshBasicMaterial({ + side: THREE.BackSide, + map: this.textureMoon, + alphaMap: this.textureMoon, + blending: THREE.AdditiveBlending, + transparent: true + }); + this.moon = new THREE.Mesh(geometry, materialMoon); + this.moon.translateZ(2); + this.moon.renderOrder = 999; + this.moon.material.depthTest = false; + this.skyGroup.add(this.moon); } renderSky(partialTicks) { diff --git a/src/js/net/minecraft/client/world/Chunk.js b/src/js/net/minecraft/client/world/Chunk.js index 9e78950..98724e7 100644 --- a/src/js/net/minecraft/client/world/Chunk.js +++ b/src/js/net/minecraft/client/world/Chunk.js @@ -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(); } diff --git a/src/js/net/minecraft/client/world/ChunkSection.js b/src/js/net/minecraft/client/world/ChunkSection.js index 427aec3..0e6055c 100644 --- a/src/js/net/minecraft/client/world/ChunkSection.js +++ b/src/js/net/minecraft/client/world/ChunkSection.js @@ -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; diff --git a/src/js/net/minecraft/client/world/World.js b/src/js/net/minecraft/client/world/World.js index b3fae4c..9681ae3 100644 --- a/src/js/net/minecraft/client/world/World.js +++ b/src/js/net/minecraft/client/world/World.js @@ -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); + } } \ No newline at end of file diff --git a/src/js/net/minecraft/client/world/block/Block.js b/src/js/net/minecraft/client/world/block/Block.js index 8388fd0..4a67817 100644 --- a/src/js/net/minecraft/client/world/block/Block.js +++ b/src/js/net/minecraft/client/world/block/Block.js @@ -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; } diff --git a/src/js/net/minecraft/client/world/block/BlockTorch.js b/src/js/net/minecraft/client/world/block/BlockTorch.js new file mode 100644 index 0000000..da013ea --- /dev/null +++ b/src/js/net/minecraft/client/world/block/BlockTorch.js @@ -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; + } +} \ No newline at end of file diff --git a/src/js/net/minecraft/client/world/block/BlockWater.js b/src/js/net/minecraft/client/world/block/BlockWater.js index dc504dd..6755cb7 100644 --- a/src/js/net/minecraft/client/world/block/BlockWater.js +++ b/src/js/net/minecraft/client/world/block/BlockWater.js @@ -12,7 +12,7 @@ window.BlockWater = class extends Block { return false; } - canCollide() { + canInteract() { return false; } diff --git a/src/js/net/minecraft/util/BlockRenderType.js b/src/js/net/minecraft/util/BlockRenderType.js new file mode 100644 index 0000000..f902edf --- /dev/null +++ b/src/js/net/minecraft/util/BlockRenderType.js @@ -0,0 +1,4 @@ +window.BlockRenderType = class { + static BLOCK = 0; + static TORCH = 1; +} \ No newline at end of file diff --git a/src/js/net/minecraft/util/MetadataChunkBlock.js b/src/js/net/minecraft/util/MetadataChunkBlock.js index 0205b1b..a52af31 100644 --- a/src/js/net/minecraft/util/MetadataChunkBlock.js +++ b/src/js/net/minecraft/util/MetadataChunkBlock.js @@ -46,7 +46,7 @@ window.MetadataChunkBlock = class { level = 15; } } else if (this.type === EnumSkyBlock.BLOCK) { - level = 0; // TODO + level = typeId === 0 ? 0 : block.getLightValue(); } if (opacity >= 15 && level === 0) { diff --git a/src/resources/terrain/terrain.png b/src/resources/terrain/terrain.png index 20e058b..23f3947 100644 Binary files a/src/resources/terrain/terrain.png and b/src/resources/terrain/terrain.png differ diff --git a/src/start.js b/src/start.js index 2f9cd86..38af26a 100644 --- a/src/start.js +++ b/src/start.js @@ -96,6 +96,7 @@ loadTexture([ "src/js/net/minecraft/util/MathHelper.js", "src/js/net/minecraft/util/BoundingBox.js", "src/js/net/minecraft/util/Keyboard.js", + "src/js/net/minecraft/util/BlockRenderType.js", "src/js/net/minecraft/client/gui/Gui.js", "src/js/net/minecraft/client/gui/GuiScreen.js", "src/js/net/minecraft/client/gui/widgets/GuiButton.js", @@ -113,6 +114,7 @@ loadTexture([ "src/js/net/minecraft/client/world/block/BlockLeave.js", "src/js/net/minecraft/client/world/block/BlockWater.js", "src/js/net/minecraft/client/world/block/BlockSand.js", + "src/js/net/minecraft/client/world/block/BlockTorch.js", "src/js/net/minecraft/client/world/ChunkSection.js", "src/js/net/minecraft/client/world/Chunk.js", "src/js/net/minecraft/client/world/World.js",