implement player spawning, movement, animation, metadata and destroying, implement NBT serialization, version 1.1.7

This commit is contained in:
LabyStudio
2022-06-20 03:16:20 +02:00
parent 4ac61adbb5
commit 19a131bd94
44 changed files with 1481 additions and 138 deletions
@@ -3,11 +3,13 @@ import ClientPlayerMovementPacket from "../network/packet/play/client/ClientPlay
import ClientPlayerRotationPacket from "../network/packet/play/client/ClientPlayerRotationPacket.js";
import ClientPlayerPositionPacket from "../network/packet/play/client/ClientPlayerPositionPacket.js";
import ClientPlayerPositionRotationPacket from "../network/packet/play/client/ClientPlayerPositionRotationPacket.js";
import ClientPlayerStatePacket from "../network/packet/play/client/ClientPlayerStatePacket.js";
import ClientSwingArmPacket from "../network/packet/play/client/ClientSwingArmPacket.js";
export default class PlayerEntityMultiplayer extends PlayerEntity {
constructor(minecraft, world, networkHandler) {
super(minecraft, world);
constructor(minecraft, world, networkHandler, id) {
super(minecraft, world, id);
this.networkHandler = networkHandler;
@@ -19,6 +21,9 @@ export default class PlayerEntityMultiplayer extends PlayerEntity {
this.lastReportedYaw = 0;
this.lastReportedPitch = 0;
this.serverSprintState = false;
this.serverSneakState = false;
}
onUpdate() {
@@ -26,7 +31,98 @@ export default class PlayerEntityMultiplayer extends PlayerEntity {
this.onUpdateWalkingPlayer();
}
swingArm() {
super.swingArm();
this.networkHandler.sendPacket(new ClientSwingArmPacket());
}
onLivingUpdate() {
this.prevCameraYaw = this.cameraYaw;
this.prevCameraPitch = this.cameraPitch;
if (this.sprintToggleTimer > 0) {
--this.sprintToggleTimer;
}
if (this.flyToggleTimer > 0) {
--this.flyToggleTimer;
}
let prevMoveForward = this.moveForward;
let prevJumping = this.jumping;
this.updateKeyboardInput();
// Toggle jumping
if (!prevJumping && this.jumping) {
if (this.flyToggleTimer === 0) {
this.flyToggleTimer = 7;
} else {
this.flying = !this.flying;
this.flyToggleTimer = 0;
this.updateFOVModifier();
}
}
// Toggle sprint
if (prevMoveForward === 0 && this.moveForward > 0) {
if (this.sprintToggleTimer === 0) {
this.sprintToggleTimer = 7;
} else {
this.sprinting = true;
this.sprintToggleTimer = 0;
this.updateFOVModifier();
}
}
if (this.sprinting && (this.moveForward <= 0 || this.collision || this.isSneaking())) {
this.sprinting = false;
this.updateFOVModifier();
}
super.onLivingUpdate();
this.jumpMovementFactor = this.speedInAir;
if (this.sprinting) {
this.jumpMovementFactor = this.jumpMovementFactor + this.speedInAir * 0.3;
}
let speedXZ = Math.sqrt(this.motionX * this.motionX + this.motionZ * this.motionZ);
let speedY = (Math.atan(-this.motionY * 0.2) * 15.0);
if (speedXZ > 0.1) {
speedXZ = 0.1;
}
if (!this.onGround || this.health <= 0.0) {
speedXZ = 0.0;
}
if (this.onGround || this.health <= 0.0) {
speedY = 0.0;
}
this.cameraYaw += (speedXZ - this.cameraYaw) * 0.4;
this.cameraPitch += (speedY - this.cameraPitch) * 0.8;
}
onUpdateWalkingPlayer() {
// Send sprinting to server
let isSprinting = this.isSprinting();
if (isSprinting !== this.serverSprintState) {
let state = isSprinting ? ClientPlayerStatePacket.START_SPRINTING : ClientPlayerStatePacket.STOP_SPRINTING;
this.networkHandler.sendPacket(new ClientPlayerStatePacket(this.id, state));
this.serverSprintState = isSprinting;
}
// Send sneaking to server
let isSneaking = this.isSneaking();
if (isSneaking !== this.serverSneakState) {
let state = isSneaking ? ClientPlayerStatePacket.START_SNEAKING : ClientPlayerStatePacket.STOP_SNEAKING;
this.networkHandler.sendPacket(new ClientPlayerStatePacket(this.id, state));
this.serverSneakState = isSneaking;
}
let movementX = this.x - this.lastReportedX;
let movementY = this.y - this.lastReportedY;
let movementZ = this.z - this.lastReportedZ;
@@ -37,6 +133,7 @@ export default class PlayerEntityMultiplayer extends PlayerEntity {
let reportPosition = movementX * movementX + movementY * movementY + movementZ * movementZ > 9.0E-4 || this.positionUpdateTicks >= 20;
let reportRotation = movementYaw !== 0.0 || movementPitch !== 0.0;
// Send position and rotation to server
if (reportPosition && reportRotation) {
this.networkHandler.sendPacket(new ClientPlayerPositionRotationPacket(this.onGround, this.x, this.y, this.z, this.rotationYaw, this.rotationPitch));
} else if (reportPosition) {