diff --git a/src/js/net/minecraft/client/render/WorldRenderer.js b/src/js/net/minecraft/client/render/WorldRenderer.js index 1ffe78e..66f2f9d 100644 --- a/src/js/net/minecraft/client/render/WorldRenderer.js +++ b/src/js/net/minecraft/client/render/WorldRenderer.js @@ -57,7 +57,16 @@ window.WorldRenderer = class { this.webRenderer.setClearColor(0x000000, 0); this.webRenderer.clear(); + // Create sky this.generateSky(); + + // Create block hit box + let geometry = new THREE.BoxGeometry(1, 1, 1); + let edges = new THREE.EdgesGeometry(geometry); + this.blockHitBox = new THREE.LineSegments(edges, new THREE.LineBasicMaterial({ + color: 0x000000, + })); + this.scene.add(this.blockHitBox); } render(partialTicks) { @@ -73,6 +82,9 @@ window.WorldRenderer = class { // Render sky this.renderSky(partialTicks); + // Render target block + this.renderBlockHitBox(player, partialTicks); + // Render actual scene this.webRenderer.render(this.scene, this.camera); } @@ -236,4 +248,43 @@ window.WorldRenderer = class { let angle = this.minecraft.world.getCelestialAngle(partialTicks); this.skyGroup.rotation.set(angle * Math.PI * 2 + Math.PI / 2, 0, 0); } + + renderBlockHitBox(player, partialTicks) { + let hitResult = player.rayTrace(5, partialTicks); + let hitBoxVisible = !(hitResult === null); + if ((this.blockHitBox.visible = hitBoxVisible)) { + let x = hitResult.x; + let y = hitResult.y; + let z = hitResult.z; + + // Get block type + let world = this.minecraft.world; + let typeId = world.getBlockAt(x, y, z); + let block = Block.getById(typeId); + + if (typeId !== 0) { + let boundingBox = block.getBoundingBox(world, x, y, z); + + let offset = 0.01; + + let width = boundingBox.width() + offset; + let height = boundingBox.height() + offset; + let depth = boundingBox.depth() + offset; + + // Update size of hit box + this.blockHitBox.scale.set( + width, + height, + depth + ); + + // Update position of hit box + this.blockHitBox.position.set( + x + width / 2 / width - 0.5 + boundingBox.maxX - width / 2 + offset / 2, + y + height / 2 / height - 0.5 + boundingBox.maxY - height / 2 + offset / 2, + z + depth / 2 / depth - 0.5 + boundingBox.maxZ - depth / 2 + offset / 2, + ); + } + } + } } \ No newline at end of file diff --git a/src/js/net/minecraft/client/world/World.js b/src/js/net/minecraft/client/world/World.js index 2c3da68..fc37468 100644 --- a/src/js/net/minecraft/client/world/World.js +++ b/src/js/net/minecraft/client/world/World.js @@ -341,7 +341,7 @@ window.World = class { let block = Block.getById(blockId); if (block != null && block.canInteract()) { - let hit = block.collisionRayTrace(x, y, z, from, to); + let hit = block.collisionRayTrace(this, x, y, z, from, to); if (hit != null) { return hit; } @@ -435,7 +435,7 @@ window.World = class { let block = Block.getById(blockId); if (block != null && block.canInteract()) { - let hit = block.collisionRayTrace(x, y, z, from, to); + let hit = block.collisionRayTrace(this, x, y, z, from, to); if (hit != null) { return hit; } diff --git a/src/js/net/minecraft/client/world/block/Block.js b/src/js/net/minecraft/client/world/block/Block.js index f4eeb18..49e24d9 100644 --- a/src/js/net/minecraft/client/world/block/Block.js +++ b/src/js/net/minecraft/client/world/block/Block.js @@ -72,7 +72,7 @@ window.Block = class { } - collisionRayTrace(x, y, z, start, end) { + collisionRayTrace(world, x, y, z, start, end) { start = start.addVector(-x, -y, -z); end = end.addVector(-x, -y, -z); diff --git a/src/js/net/minecraft/client/world/block/BlockTorch.js b/src/js/net/minecraft/client/world/block/BlockTorch.js index 0ab0cea..3664a5e 100644 --- a/src/js/net/minecraft/client/world/block/BlockTorch.js +++ b/src/js/net/minecraft/client/world/block/BlockTorch.js @@ -5,6 +5,7 @@ window.BlockTorch = class extends Block { this.boundingBox = new BoundingBox(0.4, 0.0, 0.4, 0.6, 0.6, 0.6); + // Create data faces this.dataFaces = [ EnumBlockFace.WEST, EnumBlockFace.EAST, @@ -31,24 +32,52 @@ window.BlockTorch = class extends Block { let dataFace = this.dataFaces[i]; if (world.isSolidBlockAt(x + dataFace.x, y + dataFace.y, z + dataFace.z)) { - world.setBlockDataAt(x, y, z, i + 1); + let data = i + 1; + + // Update block data in world + world.setBlockDataAt(x, y, z, data); break; } } } onBlockPlaced(world, x, y, z, face) { - let meta = world.getBlockDataAt(x, y, z); + let data = world.getBlockDataAt(x, y, z); for (let i in this.dataFaces) { let dataFace = this.dataFaces[i]; if (face === dataFace.opposite() && world.isSolidBlockAt(x + dataFace.x, y + dataFace.y, z + dataFace.z)) { - meta = parseInt(i) + 1; + data = parseInt(i) + 1; break; } } - world.getChunkSectionAt(x >> 4, y >> 4, z >> 4).setBlockDataAt(x & 15, y & 15, z & 15, meta); + // Update block data in chunk section directly to avoid notify + world.getChunkSectionAt(x >> 4, y >> 4, z >> 4).setBlockDataAt(x & 15, y & 15, z & 15, data); + } + + collisionRayTrace(world, x, y, z, start, end) { + let data = world.getBlockDataAt(x, y, z) & 7; + + switch (data) { + case 1: + this.boundingBox = new BoundingBox(0.0, 0.2, 0.35, 0.3, 0.8, 0.65); + break; + case 2: + this.boundingBox = new BoundingBox(0.7, 0.2, 0.35, 1.0, 0.8, 0.65); + break; + case 3: + this.boundingBox = new BoundingBox(0.35, 0.2, 0.0, 0.65, 0.8, 0.3); + break; + case 4: + this.boundingBox = new BoundingBox(0.35, 0.2, 0.7, 0.65, 0.8, 1.0); + break; + default: + this.boundingBox = new BoundingBox(0.4, 0.0, 0.4, 0.6, 0.6, 0.6); + break; + } + + return super.collisionRayTrace(world, x, y, z, start, end); } } \ No newline at end of file diff --git a/src/js/net/minecraft/util/BoundingBox.js b/src/js/net/minecraft/util/BoundingBox.js index 9dd29f6..851d0cc 100644 --- a/src/js/net/minecraft/util/BoundingBox.js +++ b/src/js/net/minecraft/util/BoundingBox.js @@ -21,6 +21,17 @@ window.BoundingBox = class { this.maxZ = maxZ; } + width() { + return this.maxX - this.minX; + } + + height() { + return this.maxY - this.minY; + } + + depth() { + return this.maxZ - this.minZ; + } /** * Copy the current bounding box object