From 7df2806456db194b4cab543afcbfa2c4810741ab Mon Sep 17 00:00:00 2001 From: LabyStudio Date: Wed, 13 Apr 2022 17:42:19 +0200 Subject: [PATCH] restructure entity update (cherry picked from commit 21c460ee8e62fb81cdd9ac607b1e8e9c2eabc211) --- src/js/net/minecraft/client/Minecraft.js | 2 +- src/js/net/minecraft/client/entity/Entity.js | 45 +++++++- .../minecraft/client/entity/EntityLiving.js | 82 +++++++++++++ .../minecraft/client/entity/PlayerEntity.js | 108 +++--------------- .../client/render/entity/EntityRenderer.js | 12 ++ src/start.js | 1 + 6 files changed, 159 insertions(+), 91 deletions(-) create mode 100644 src/js/net/minecraft/client/entity/EntityLiving.js diff --git a/src/js/net/minecraft/client/Minecraft.js b/src/js/net/minecraft/client/Minecraft.js index d12b057..e037068 100644 --- a/src/js/net/minecraft/client/Minecraft.js +++ b/src/js/net/minecraft/client/Minecraft.js @@ -173,7 +173,7 @@ window.Minecraft = class { this.world.onTick(); // Tick the player - this.player.onTick(); + this.player.onUpdate(); } // Update loading progress diff --git a/src/js/net/minecraft/client/entity/Entity.js b/src/js/net/minecraft/client/entity/Entity.js index 0f601ed..ba3bc0f 100644 --- a/src/js/net/minecraft/client/entity/Entity.js +++ b/src/js/net/minecraft/client/entity/Entity.js @@ -1,7 +1,50 @@ window.Entity = class { - constructor() { + constructor(minecraft, world) { + this.minecraft = minecraft; + this.world = world; + this.group = new THREE.Object3D(); + + this.x = 0; + this.y = 0; + this.z = 0; + + this.motionX = 0; + this.motionY = 0; + this.motionZ = 0; + + this.onGround = false; + + this.sneaking = false; + + this.yaw = 0; + this.pitch = 0; + this.renderYawOffset = 0; + + this.prevX = 0; + this.prevY = 0; + this.prevZ = 0; + + this.prevYaw = 0; + this.prevPitch = 0; + this.prevRenderYawOffset = 0; + + this.distanceWalked = 0; + this.nextStepDistance = 1; + } + + onUpdate() { + this.onEntityUpdate(); + } + + onEntityUpdate() { + this.prevX = this.x; + this.prevY = this.y; + this.prevZ = this.z; + + this.prevPitch = this.pitch; + this.prevYaw = this.yaw; } } \ No newline at end of file diff --git a/src/js/net/minecraft/client/entity/EntityLiving.js b/src/js/net/minecraft/client/entity/EntityLiving.js new file mode 100644 index 0000000..c7309e4 --- /dev/null +++ b/src/js/net/minecraft/client/entity/EntityLiving.js @@ -0,0 +1,82 @@ +window.EntityLiving = class extends Entity { + + constructor(minecraft, world) { + super(minecraft, world); + + this.jumpTicks = 0; + + this.jumping = false; + + this.moveForward = 0.0; + this.moveStrafing = 0.0; + } + + onUpdate() { + super.onUpdate(); + this.onLivingUpdate(); + + while (this.renderYawOffset - this.prevRenderYawOffset < -180.0) { + this.prevRenderYawOffset -= 360.0; + } + + while (this.renderYawOffset - this.prevRenderYawOffset >= 180.0) { + this.prevRenderYawOffset += 360.0; + } + } + + onLivingUpdate() { + if (this.jumpTicks > 0) { + --this.jumpTicks; + } + + // Stop if too slow + if (Math.abs(this.motionX) < 0.003) { + this.motionX = 0.0; + } + if (Math.abs(this.motionY) < 0.003) { + this.motionY = 0.0; + } + if (Math.abs(this.motionZ) < 0.003) { + this.motionZ = 0.0; + } + + // Jump + if (this.jumping) { + if (this.isInWater()) { + this.motionY += 0.04; + } else if (this.onGround && this.jumpTicks === 0) { + this.jump(); + this.jumpTicks = 10; + } + } else { + this.jumpTicks = 0; + } + + this.moveForward *= 0.98; + this.moveStrafing *= 0.98; + + this.moveEntityWithHeading(this.moveForward, this.moveStrafing); + } + + moveEntityWithHeading(moveForward, moveStrafing) { + if (this.flying) { + this.travelFlying(moveForward, 0, moveStrafing); + } else { + if (this.isInWater()) { + // Is inside of water + this.travelInWater(moveForward, 0, moveStrafing); + } else { + // Is on land + this.travel(moveForward, 0, moveStrafing); + } + } + } + + onEntityUpdate() { + this.prevRenderYawOffset = this.renderYawOffset; + + super.onEntityUpdate(); + + } + +} \ No newline at end of file diff --git a/src/js/net/minecraft/client/entity/PlayerEntity.js b/src/js/net/minecraft/client/entity/PlayerEntity.js index e572002..f04ee02 100644 --- a/src/js/net/minecraft/client/entity/PlayerEntity.js +++ b/src/js/net/minecraft/client/entity/PlayerEntity.js @@ -1,29 +1,9 @@ -window.PlayerEntity = class extends Entity { +window.PlayerEntity = class extends EntityLiving { static name = "PlayerEntity"; constructor(minecraft, world) { - super(); - - this.minecraft = minecraft; - this.world = world; - - this.prevX = 0; - this.prevY = 0; - this.prevZ = 0; - - this.x = 0; - this.y = 0; - this.z = 0; - - this.motionX = 0; - this.motionY = 0; - this.motionZ = 0; - - this.yaw = 0; - this.pitch = 0; - - this.onGround = false; + super(minecraft, world); this.collision = false; @@ -32,24 +12,15 @@ window.PlayerEntity = class extends Entity { this.flySpeed = 0.05; this.stepHeight = 0.5; - this.moveForward = 0.0; - this.moveStrafing = 0.0; - - this.jumpTicks = 0; this.flyToggleTimer = 0; this.sprintToggleTimer = 0; - this.jumping = false; this.sprinting = false; - this.sneaking = false; this.flying = false; this.prevFovModifier = 0; this.fovModifier = 0; this.timeFovChanged = 0; - - this.distanceWalked = 0; - this.nextStepDistance = 1; } respawn() { @@ -92,7 +63,18 @@ window.PlayerEntity = class extends Entity { } } - onTick() { + onUpdate() { + super.onUpdate(); + } + + onLivingUpdate() { + if (this.sprintToggleTimer > 0) { + --this.sprintToggleTimer; + } + if (this.flyToggleTimer > 0) { + --this.flyToggleTimer; + } + let prevMoveForward = this.moveForward; let prevJumping = this.jumping; @@ -122,70 +104,18 @@ window.PlayerEntity = class extends Entity { } } - if (this.jumpTicks > 0) { - --this.jumpTicks; + if (this.sprinting && (this.moveForward <= 0 || this.collision || this.sneaking)) { + this.sprinting = false; + + this.updateFOVModifier(); } - if (this.flyToggleTimer > 0) { - --this.flyToggleTimer; - } - - if (this.sprintToggleTimer > 0) { - --this.sprintToggleTimer; - } - - this.prevX = this.x; - this.prevY = this.y; - this.prevZ = this.z; - - // Stop if too slow - if (Math.abs(this.motionX) < 0.003) { - this.motionX = 0.0; - } - if (Math.abs(this.motionY) < 0.003) { - this.motionY = 0.0; - } - if (Math.abs(this.motionZ) < 0.003) { - this.motionZ = 0.0; - } - - // Jump - if (this.jumping) { - if (this.isInWater()) { - this.motionY += 0.04; - } else if (this.onGround && this.jumpTicks === 0) { - this.jump(); - this.jumpTicks = 10; - } - } else { - this.jumpTicks = 0; - } - - this.moveStrafing *= 0.98; - this.moveForward *= 0.98; - - if (this.flying) { - this.travelFlying(this.moveForward, 0, this.moveStrafing); - } else { - if (this.isInWater()) { - // Is inside of water - this.travelInWater(this.moveForward, 0, this.moveStrafing); - } else { - // Is on land - this.travel(this.moveForward, 0, this.moveStrafing); - } - } + super.onLivingUpdate(); this.jumpMovementFactor = this.speedInAir; if (this.sprinting) { this.jumpMovementFactor = this.jumpMovementFactor + this.speedInAir * 0.3; - - if (this.moveForward <= 0 || this.collision || this.sneaking) { - this.sprinting = false; - - this.updateFOVModifier(); - } } } diff --git a/src/js/net/minecraft/client/render/entity/EntityRenderer.js b/src/js/net/minecraft/client/render/entity/EntityRenderer.js index d1db4a5..24cf50e 100644 --- a/src/js/net/minecraft/client/render/entity/EntityRenderer.js +++ b/src/js/net/minecraft/client/render/entity/EntityRenderer.js @@ -11,6 +11,9 @@ window.EntityRenderer = class { render(entity, partialTicks) { let group = entity.group; + let rotationOffset = this.interpolateRotation(entity.prevRenderYawOffset, entity.renderYawOffset, partialTicks); + let rotationHead = this.interpolateRotation(entity.prevRotationYawHead, entity.rotationYawHead, partialTicks); + // Interpolate entity position let interpolatedX = entity.prevX + (entity.x - entity.prevX) * partialTicks; let interpolatedY = entity.prevY + (entity.y - entity.prevY) * partialTicks; @@ -33,4 +36,13 @@ window.EntityRenderer = class { this.model.render(group, time); } + interpolateRotation(prevValue, value, partialTicks) { + let factor; + for (factor = value - prevValue; factor < -180.0; factor += 360.0) {} + while (factor >= 180.0) { + factor -= 360.0; + } + return prevValue + partialTicks * factor; + } + } \ No newline at end of file diff --git a/src/start.js b/src/start.js index d2b1aa5..2782cb3 100644 --- a/src/start.js +++ b/src/start.js @@ -127,6 +127,7 @@ loadTexture([ "src/js/net/minecraft/client/world/generator/noise/NoiseGeneratorCombined.js", "src/js/net/minecraft/client/world/generator/WorldGenerator.js", "src/js/net/minecraft/client/entity/Entity.js", + "src/js/net/minecraft/client/entity/EntityLiving.js", "src/js/net/minecraft/client/entity/PlayerEntity.js", "src/js/net/minecraft/client/inventory/Inventory.js", "src/js/net/minecraft/client/GameSettings.js",