implement block in hand

(cherry picked from commit 75fd4edd9f67b2cbf5abffc1ea417920106b1353)
This commit is contained in:
LabyStudio
2022-04-14 20:50:18 +02:00
parent 96b287bedc
commit 5c47dd6599
13 changed files with 145 additions and 68 deletions
+4 -5
View File
@@ -51,7 +51,6 @@ window.Minecraft = class {
// Create player // Create player
this.player = new PlayerEntity(this, this.world); this.player = new PlayerEntity(this, this.world);
this.world.addEntity(this.player); this.world.addEntity(this.player);
this.inventory = new Inventory();
this.displayScreen(this.loadingScreen); this.displayScreen(this.loadingScreen);
@@ -192,7 +191,7 @@ window.Minecraft = class {
onKeyPressed(button) { onKeyPressed(button) {
for (let i = 1; i <= 9; i++) { for (let i = 1; i <= 9; i++) {
if (button === 'Digit' + i) { if (button === 'Digit' + i) {
this.inventory.selectedSlotIndex = i - 1; this.player.inventory.selectedSlotIndex = i - 1;
} }
} }
@@ -238,7 +237,7 @@ window.Minecraft = class {
if (hitResult != null) { if (hitResult != null) {
let typeId = this.world.getBlockAt(hitResult.x, hitResult.y, hitResult.z); let typeId = this.world.getBlockAt(hitResult.x, hitResult.y, hitResult.z);
if (typeId !== 0) { if (typeId !== 0) {
this.inventory.setItemInSelectedSlot(typeId); this.player.inventory.setItemInSelectedSlot(typeId);
} }
} }
} }
@@ -254,7 +253,7 @@ window.Minecraft = class {
// Don't place blocks if the player is standing there // Don't place blocks if the player is standing there
if (!placedBoundingBox.intersects(this.player.boundingBox)) { if (!placedBoundingBox.intersects(this.player.boundingBox)) {
let typeId = this.inventory.getItemInSelectedSlot(); let typeId = this.player.inventory.getItemInSelectedSlot();
// Get previous block // Get previous block
let prevTypeId = this.world.getBlockAt(x, y, z); let prevTypeId = this.world.getBlockAt(x, y, z);
@@ -286,7 +285,7 @@ window.Minecraft = class {
} }
onMouseScroll(delta) { onMouseScroll(delta) {
this.inventory.shiftSelectedSlot(delta); this.player.inventory.shiftSelectedSlot(delta);
} }
isPaused() { isPaused() {
@@ -13,7 +13,7 @@ window.EntityLiving = class extends Entity {
this.swingProgress = 0; this.swingProgress = 0;
this.prevSwingProgress = 0; this.prevSwingProgress = 0;
this.swingProgressInt = 0; this.swingProgressInt = 0;
this.swingInProgress = false; this.isSwingInProgress = false;
this.renderYawOffset = 0; this.renderYawOffset = 0;
this.rotationYawHead = 0; this.rotationYawHead = 0;
@@ -29,26 +29,7 @@ window.EntityLiving = class extends Entity {
onUpdate() { onUpdate() {
super.onUpdate(); super.onUpdate();
this.onLivingUpdate(); this.onLivingUpdate();
this.updateBodyRotation();
let motionX = this.x - this.prevX;
let motionZ = this.z - this.prevZ;
let bodyRotation = this.renderYawOffset;
let distanceTravelled = motionX * motionX + motionZ * motionZ;
let distanceTravelledSqrt = 0.0;
if (distanceTravelled > 0.0025000002) {
distanceTravelledSqrt = Math.sqrt(distanceTravelled) * 3.0;
bodyRotation = Math.atan2(motionZ, motionX) * 180.0 / Math.PI - 90.0;
}
if (this.swingProgress > 0.0) {
bodyRotation = this.rotationYaw;
}
// TODO handle travel distance
distanceTravelledSqrt = this.updateBodyRotation(bodyRotation, distanceTravelledSqrt);
while (this.rotationYaw - this.prevRotationYaw < -180.0) { while (this.rotationYaw - this.prevRotationYaw < -180.0) {
this.prevRotationYaw -= 360.0; this.prevRotationYaw -= 360.0;
@@ -151,13 +132,25 @@ window.EntityLiving = class extends Entity {
super.onEntityUpdate(); super.onEntityUpdate();
} }
updateBodyRotation(bodyRotation, distanceTravelledSqrt) { updateBodyRotation() {
let motionX = this.x - this.prevX;
let motionZ = this.z - this.prevZ;
let bodyRotation = this.renderYawOffset;
let distanceTravelled = motionX * motionX + motionZ * motionZ;
if (distanceTravelled > 0.0025000002) {
bodyRotation = Math.atan2(motionZ, motionX) * 180.0 / Math.PI - 90.0;
}
if (this.swingProgress > 0.0) {
bodyRotation = this.rotationYaw;
}
let bodyRotationDifference = MathHelper.wrapAngleTo180(bodyRotation - this.renderYawOffset); let bodyRotationDifference = MathHelper.wrapAngleTo180(bodyRotation - this.renderYawOffset);
this.renderYawOffset += bodyRotationDifference * 0.3; this.renderYawOffset += bodyRotationDifference * 0.3;
let yaw = MathHelper.wrapAngleTo180(this.rotationYaw - this.renderYawOffset); let yaw = MathHelper.wrapAngleTo180(this.rotationYaw - this.renderYawOffset);
let turn = yaw < -90.0 || yaw >= 90.0;
if (yaw < -75.0) { if (yaw < -75.0) {
yaw = -75.0; yaw = -75.0;
} }
@@ -169,10 +162,6 @@ window.EntityLiving = class extends Entity {
if (yaw * yaw > 2500.0) { if (yaw * yaw > 2500.0) {
this.renderYawOffset += yaw * 0.2; this.renderYawOffset += yaw * 0.2;
} }
if (turn) {
distanceTravelledSqrt *= -1.0;
}
return distanceTravelledSqrt;
} }
swingArm() { swingArm() {
@@ -185,7 +174,6 @@ window.EntityLiving = class extends Entity {
updateArmSwingProgress() { updateArmSwingProgress() {
let swingAnimationEnd = 6; let swingAnimationEnd = 6;
if (this.isSwingInProgress) { if (this.isSwingInProgress) {
++this.swingProgressInt; ++this.swingProgressInt;
@@ -5,6 +5,8 @@ window.PlayerEntity = class extends EntityLiving {
constructor(minecraft, world) { constructor(minecraft, world) {
super(minecraft, world); super(minecraft, world);
this.inventory = new Inventory();
this.collision = false; this.collision = false;
this.jumpMovementFactor = 0.02; this.jumpMovementFactor = 0.02;
@@ -124,7 +126,12 @@ window.PlayerEntity = class extends EntityLiving {
} }
isHeadInWater() { isHeadInWater() {
return this.world.getBlockAt(this.getBlockPosX(), Math.floor(this.y + this.getEyeHeight() + 0.12), this.getBlockPosZ()) === Block.WATER.getId(); let cameraPosition = this.world.minecraft.worldRenderer.camera.position;
return this.world.getBlockAt(
Math.floor(cameraPosition.x),
Math.floor(cameraPosition.y + 0.12),
Math.floor(cameraPosition.z)
) === Block.WATER.getId()
} }
jump() { jump() {
@@ -37,13 +37,13 @@ window.IngameOverlay = class extends Gui {
this.textureHotbar, this.textureHotbar,
0, 22, 0, 22,
24, 24, 24, 24,
x + this.minecraft.inventory.selectedSlotIndex * 20 - 1, y - 1, x + this.minecraft.player.inventory.selectedSlotIndex * 20 - 1, y - 1,
24, 24 24, 24
) )
// Render items // Render items
for (let i = 0; i < 9; i++) { for (let i = 0; i < 9; i++) {
let typeId = this.minecraft.inventory.getItemInSlot(i); let typeId = this.minecraft.player.inventory.getItemInSlot(i);
if (typeId !== 0) { if (typeId !== 0) {
let renderId = "hotbar" + i; let renderId = "hotbar" + i;
let block = Block.getById(typeId); let block = Block.getById(typeId);
@@ -165,19 +165,21 @@ window.BlockRenderer = class {
let distortZ = 0; let distortZ = 0;
// Attach torch at wall // Attach torch at wall
switch (world.getBlockDataAt(x, y, z)) { if (world != null) {
case 1: switch (world.getBlockDataAt(x, y, z)) {
distortX = -0.2; case 1:
break; distortX = -0.2;
case 2: break;
distortX = 0.2; case 2:
break; distortX = 0.2;
case 3: break;
distortZ = -0.2; case 3:
break; distortZ = -0.2;
case 4: break;
distortZ = 0.2; case 4:
break; distortZ = 0.2;
break;
}
} }
// Model type // Model type
@@ -252,6 +254,33 @@ window.BlockRenderer = class {
} }
} }
renderBlockInHand(group, block, brightness) {
this.tessellator.startDrawing();
// Change brightness
this.tessellator.transformBrightness(brightness);
// Render block
this.renderBlock(null, block, 0, 0, 0);
// Create mesh
let mesh = this.tessellator.draw(group);
mesh.geometry.center();
// Relative position
mesh.position.x = 0;
mesh.position.y = 9;
mesh.position.z = -5;
// Rotation
mesh.rotation.y = Math.PI / 4;
// Scale
mesh.scale.x = 6;
mesh.scale.y = -6;
mesh.scale.z = 6;
}
renderGuiBlock(group, block, x, y, size, brightness) { renderGuiBlock(group, block, x, y, size, brightness) {
this.tessellator.startDrawing(); this.tessellator.startDrawing();
@@ -27,7 +27,7 @@ window.WorldRenderer = class {
this.blockRenderer = new BlockRenderer(this); this.blockRenderer = new BlockRenderer(this);
// Entity render manager // Entity render manager
this.entityRenderManager = new EntityRenderManager(); this.entityRenderManager = new EntityRenderManager(this);
this.initialize(); this.initialize();
} }
@@ -92,8 +92,8 @@ window.WorldRenderer = class {
// Render entities // Render entities
for (let entity of this.minecraft.world.entities) { for (let entity of this.minecraft.world.entities) {
if (entity === player && this.minecraft.settings.thirdPersonView === 0) { if (entity === player && this.minecraft.settings.thirdPersonView === 0) {
entity.group.clear(); entity.group.clear(); // Remove entity from scene
entity.lastRenderedBrightness = -1; // TODO: Find a better way to trigger this delete entity.group.buildMeta; // To trigger a rebuild on the next render
continue; continue;
} }
this.renderEntity(entity, partialTicks); this.renderEntity(entity, partialTicks);
@@ -1,8 +1,10 @@
window.EntityRenderManager = class { window.EntityRenderManager = class {
constructor() { constructor(worldRenderer) {
this.worldRenderer = worldRenderer;
this.renderers = []; this.renderers = [];
this.push(PlayerEntity, new PlayerRenderer()); this.push(PlayerEntity, new PlayerRenderer(worldRenderer));
} }
push(entityType, entityRenderer) { push(entityType, entityRenderer) {
@@ -6,19 +6,41 @@ window.EntityRenderer = class {
} }
rebuild(entity) { rebuild(entity) {
let brightness = entity.getEntityBrightness(); // Create meta for group
entity.lastRenderedBrightness = brightness; let group = entity.group;
let meta = {};
this.fillMeta(entity, meta);
group.buildMeta = meta;
// Apply brightness // Clear meshes
group.clear();
// Apply brightness and rebuild
let brightness = group.buildMeta.brightness;
this.tessellator.setColor(brightness, brightness, brightness); this.tessellator.setColor(brightness, brightness, brightness);
this.model.rebuild(this.tessellator, group);
}
// Rebuild fillMeta(entity, meta) {
this.model.rebuild(this.tessellator, entity.group); meta.brightness = entity.getEntityBrightness();
meta.itemInHand = entity.inventory.getItemInSelectedSlot();
}
isRebuildRequired(entity) {
let group = entity.group;
if (typeof group.buildMeta === "undefined") {
return true;
}
// Compare meta of group
let currentMeta = {};
this.fillMeta(entity, currentMeta);
let previousMeta = group.buildMeta;
return JSON.stringify(currentMeta) !== JSON.stringify(previousMeta);
} }
render(entity, partialTicks) { render(entity, partialTicks) {
let brightness = entity.getEntityBrightness(); if (this.isRebuildRequired(entity)) {
if (entity.lastRenderedBrightness !== brightness) {
this.rebuild(entity); this.rebuild(entity);
} }
@@ -45,7 +67,7 @@ window.EntityRenderer = class {
// Actual size of the entity // Actual size of the entity
let scale = 7.0 / 120.0; let scale = 7.0 / 120.0;
group.scale.set(-scale,- scale, scale); group.scale.set(-scale, -scale, scale);
// Rotate entity model // Rotate entity model
group.rotation.y = MathHelper.toRadians(-rotationBody + 180); group.rotation.y = MathHelper.toRadians(-rotationBody + 180);
@@ -1,21 +1,36 @@
window.PlayerRenderer = class extends EntityRenderer { window.PlayerRenderer = class extends EntityRenderer {
constructor() { constructor(worldRenderer) {
super(new ModelPlayer()); super(new ModelPlayer());
this.worldRenderer = worldRenderer;
// Load character texture // Load character texture
this.textureCharacter = new THREE.TextureLoader().load('src/resources/char.png'); this.textureCharacter = new THREE.TextureLoader().load('src/resources/char.png');
this.textureCharacter.magFilter = THREE.NearestFilter; this.textureCharacter.magFilter = THREE.NearestFilter;
this.textureCharacter.minFilter = THREE.NearestFilter; this.textureCharacter.minFilter = THREE.NearestFilter;
} }
rebuild(tessellator, entity) { rebuild(entity) {
this.tessellator.bindTexture(this.textureCharacter); this.tessellator.bindTexture(this.textureCharacter);
super.rebuild(tessellator, entity); super.rebuild(entity);
// Render item in hand
let group = this.model.rightArm.bone;
let id = entity.inventory.getItemInSelectedSlot();
if (id !== 0) {
let block = Block.getById(id);
this.worldRenderer.blockRenderer.renderBlockInHand(group, block, 1);
}
} }
render(entity, partialTicks) { render(entity, partialTicks) {
super.render(entity, partialTicks); super.render(entity, partialTicks);
} }
fillMeta(entity, meta) {
super.fillMeta(entity, meta);
meta.itemInHand = entity.inventory.getItemInSelectedSlot();
}
} }
@@ -58,8 +58,11 @@ window.ModelPlayer = class extends ModelBase {
render(entity, limbSwingAmount, limbSwing, timeAlive, yaw, pitch, partialTicks) { render(entity, limbSwingAmount, limbSwing, timeAlive, yaw, pitch, partialTicks) {
let group = entity.group; let group = entity.group;
// Head rotation
this.head.rotateAngleY = MathHelper.toRadians(yaw); this.head.rotateAngleY = MathHelper.toRadians(yaw);
this.head.rotateAngleX = MathHelper.toRadians(pitch); this.head.rotateAngleX = MathHelper.toRadians(pitch);
// Limb swing leg animation
this.rightArm.rotateAngleX = Math.cos(limbSwingAmount * 0.6662 + Math.PI) * 2.0 * limbSwing * 0.5; this.rightArm.rotateAngleX = Math.cos(limbSwingAmount * 0.6662 + Math.PI) * 2.0 * limbSwing * 0.5;
this.leftArm.rotateAngleX = Math.cos(limbSwingAmount * 0.6662) * 2.0 * limbSwing * 0.5; this.leftArm.rotateAngleX = Math.cos(limbSwingAmount * 0.6662) * 2.0 * limbSwing * 0.5;
this.rightArm.rotateAngleZ = 0.0; this.rightArm.rotateAngleZ = 0.0;
@@ -69,10 +72,16 @@ window.ModelPlayer = class extends ModelBase {
this.rightLeg.rotateAngleY = 0.0; this.rightLeg.rotateAngleY = 0.0;
this.leftLeg.rotateAngleY = 0.0; this.leftLeg.rotateAngleY = 0.0;
// Reset arms for swing progress
this.rightArm.rotateAngleY = 0.0; this.rightArm.rotateAngleY = 0.0;
this.rightArm.rotateAngleZ = 0.0; this.rightArm.rotateAngleZ = 0.0;
this.leftArm.rotateAngleY = 0.0; this.leftArm.rotateAngleY = 0.0;
// Held item animation
if (entity.inventory.getItemInSelectedSlot() !== 0) {
this.rightArm.rotateAngleX = this.rightArm.rotateAngleX * 0.5 - (Math.PI / 10);
}
// Swing progress // Swing progress
let swingProgress = entity.swingProgress - entity.prevSwingProgress; let swingProgress = entity.swingProgress - entity.prevSwingProgress;
if (swingProgress < 0.0) { if (swingProgress < 0.0) {
@@ -106,6 +115,7 @@ window.ModelPlayer = class extends ModelBase {
this.rightArm.rotateAngleZ += Math.sin(interpolatedSwingProgress * Math.PI) * -0.4; this.rightArm.rotateAngleZ += Math.sin(interpolatedSwingProgress * Math.PI) * -0.4;
} }
// Sneaking animation
if (entity.sneaking) { if (entity.sneaking) {
this.body.rotateAngleX = 0.5; this.body.rotateAngleX = 0.5;
this.rightArm.rotateAngleX += 0.4; this.rightArm.rotateAngleX += 0.4;
@@ -126,6 +136,7 @@ window.ModelPlayer = class extends ModelBase {
this.head.rotationPointY = 0.0; this.head.rotationPointY = 0.0;
} }
// Limb swing arm animation
this.rightArm.rotateAngleZ += Math.cos(timeAlive * 0.09) * 0.05 + 0.05; this.rightArm.rotateAngleZ += Math.cos(timeAlive * 0.09) * 0.05 + 0.05;
this.leftArm.rotateAngleZ -= Math.cos(timeAlive * 0.09) * 0.05 + 0.05; this.leftArm.rotateAngleZ -= Math.cos(timeAlive * 0.09) * 0.05 + 0.05;
this.rightArm.rotateAngleX += Math.sin(timeAlive * 0.067) * 0.05; this.rightArm.rotateAngleX += Math.sin(timeAlive * 0.067) * 0.05;
@@ -136,6 +136,8 @@ window.ModelRenderer = class {
} }
rebuild(tessellator, group) { rebuild(tessellator, group) {
this.bone.clear();
// Start drawing // Start drawing
tessellator.startDrawing(); tessellator.startDrawing();
@@ -33,7 +33,7 @@ window.MetadataChunkBlock = class {
let newLevel = 0; let newLevel = 0;
let typeId = world.getBlockAt(x, y, z); let typeId = world.getBlockAt(x, y, z);
let block = Block.getById(typeId); let block = Block.getById(typeId);
let opacity = typeId === 0 ? 0 : block.getOpacity() * 255; let opacity = typeId === 0 ? 0 : Math.round(block.getOpacity() * 255);
if (opacity === 0) { if (opacity === 0) {
opacity = 1; opacity = 1;
+3 -1
View File
@@ -1,6 +1,8 @@
window.Random = class { window.Random = class {
constructor(seed) { static instances = 0;
constructor(seed = Date.now() % 1000000000 ^ Random.instances++ * 1000) {
this.mask = 0xffffffff; this.mask = 0xffffffff;
this.m_w = (123456789 + seed) & this.mask; this.m_w = (123456789 + seed) & this.mask;
this.m_z = (987654321 - seed) & this.mask; this.m_z = (987654321 - seed) & this.mask;