nurtex_protocol/types/
rotation.rs1use std::io::{self, Cursor, Write};
2
3use nurtex_codec::Buffer;
4
5#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
7pub struct Rotation {
8 pub yaw: f32,
9 pub pitch: f32,
10}
11
12impl Rotation {
13 pub fn new(yaw: f32, pitch: f32) -> Self {
15 Self { yaw, pitch }
16 }
17
18 pub fn from_angle(yaw_angle: i8, pitch_angle: i8) -> Self {
20 let yaw = (yaw_angle as f32) / 256.0 * 360.0;
21 let pitch = (pitch_angle as f32) / 256.0 * 360.0;
22
23 Self { yaw, pitch }
24 }
25
26 pub fn zero() -> Self {
28 Self { yaw: 0.0, pitch: 0.0 }
29 }
30
31 pub fn delta(&self, other: Rotation) -> Self {
33 let dyaw = self.yaw - other.yaw;
34 let dpitch = self.pitch - other.pitch;
35
36 Self { yaw: dyaw, pitch: dpitch }
37 }
38}
39
40impl Buffer for Rotation {
41 fn read_buf(buffer: &mut Cursor<&[u8]>) -> Option<Self> {
42 Some(Self {
43 yaw: f32::read_buf(buffer)?,
44 pitch: f32::read_buf(buffer)?,
45 })
46 }
47
48 fn write_buf(&self, buffer: &mut impl Write) -> io::Result<()> {
49 self.yaw.write_buf(buffer)?;
50 self.pitch.write_buf(buffer)?;
51 Ok(())
52 }
53}