diff options
| author | mat <27899617+mat-1@users.noreply.github.com> | 2024-12-11 19:51:12 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-12-11 19:51:12 -0600 |
| commit | e9136c9cbbf9010b8352127e129c1cd290f377bd (patch) | |
| tree | db83316a273153106dd3b343c9d6d4fce234d132 /azalea-entity/src/vec_delta_codec.rs | |
| parent | 23932003d98db0f5f976146aa9a11e5d04a74695 (diff) | |
| download | azalea-drasl-e9136c9cbbf9010b8352127e129c1cd290f377bd.tar.xz | |
Implement EntityPositionSync (#196)
* implement EntityPositionSync
* fix EntityPositionSync setting the wrong vec_delta_codec and also move into a RelativeEntityUpdate
Diffstat (limited to 'azalea-entity/src/vec_delta_codec.rs')
| -rw-r--r-- | azalea-entity/src/vec_delta_codec.rs | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/azalea-entity/src/vec_delta_codec.rs b/azalea-entity/src/vec_delta_codec.rs new file mode 100644 index 00000000..51aa7cea --- /dev/null +++ b/azalea-entity/src/vec_delta_codec.rs @@ -0,0 +1,60 @@ +use azalea_core::position::Vec3; + +#[derive(Debug, Clone, Default)] +pub struct VecDeltaCodec { + base: Vec3, +} + +impl VecDeltaCodec { + pub fn new(base: Vec3) -> Self { + Self { base } + } + + pub fn decode(&self, x: i64, y: i64, z: i64) -> Vec3 { + if x == 0 && y == 0 && z == 0 { + return self.base; + } + + let new_x = if x == 0 { + self.base.x + } else { + decode(encode(self.base.x) + x) + }; + let new_y = if y == 0 { + self.base.y + } else { + decode(encode(self.base.y) + y) + }; + let new_z = if z == 0 { + self.base.z + } else { + decode(encode(self.base.z) + z) + }; + + Vec3::new(new_x, new_y, new_z) + } + + pub fn encode_x(&self, pos: Vec3) -> i64 { + encode(pos.x) - encode(self.base.x) + } + pub fn encode_y(&self, pos: Vec3) -> i64 { + encode(pos.y) - encode(self.base.y) + } + pub fn encode_z(&self, pos: Vec3) -> i64 { + encode(pos.z) - encode(self.base.z) + } + + pub fn set_base(&mut self, pos: Vec3) { + self.base = pos; + } + pub fn base(&self) -> Vec3 { + self.base + } +} + +fn encode(value: f64) -> i64 { + (value * 4096.).round() as i64 +} +fn decode(value: i64) -> f64 { + (value as f64) / 4096. +} |
