restructure entity update

(cherry picked from commit 21c460ee8e62fb81cdd9ac607b1e8e9c2eabc211)
This commit is contained in:
LabyStudio
2022-04-13 17:42:19 +02:00
parent 62a43c2468
commit 7df2806456
6 changed files with 159 additions and 91 deletions
+1 -1
View File
@@ -173,7 +173,7 @@ window.Minecraft = class {
this.world.onTick();
// Tick the player
this.player.onTick();
this.player.onUpdate();
}
// Update loading progress
+44 -1
View File
@@ -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;
}
}
@@ -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();
}
}
@@ -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();
}
}
}
@@ -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;
}
}
+1
View File
@@ -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",