mirror of
https://github.com/italicsjenga/valence.git
synced 2024-12-25 23:31:30 +11:00
560163fd2e
This could be useful for building proxies or clients in the future.
30 lines
721 B
Rust
30 lines
721 B
Rust
use std::io::{Read, Write};
|
|
|
|
use crate::protocol::{Decode, Encode};
|
|
|
|
/// Represents an angle in steps of 1/256 of a full turn.
|
|
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
|
|
pub struct ByteAngle(pub u8);
|
|
|
|
impl ByteAngle {
|
|
pub fn from_degrees(f: f32) -> ByteAngle {
|
|
ByteAngle((f.rem_euclid(360.0) / 360.0 * 256.0).round() as u8)
|
|
}
|
|
|
|
// pub fn to_degrees(self) -> f32 {
|
|
// self.0 as f32 / 256.0 * 360.0
|
|
// }
|
|
}
|
|
|
|
impl Encode for ByteAngle {
|
|
fn encode(&self, w: &mut impl Write) -> anyhow::Result<()> {
|
|
self.0.encode(w)
|
|
}
|
|
}
|
|
|
|
impl Decode for ByteAngle {
|
|
fn decode(r: &mut impl Read) -> anyhow::Result<Self> {
|
|
Ok(ByteAngle(u8::decode(r)?))
|
|
}
|
|
}
|