terraria_protocol/structures/
vec2.rs1use crate::{Deserializable, Serializable, SliceCursor};
2
3pub const TILE_TO_POS_SCALE: f32 = 16.0;
4
5#[derive(Debug, Default, Clone, Copy)]
6pub struct Vec2 {
7 pub x: f32,
8 pub y: f32,
9}
10
11impl Vec2 {
12 pub fn new(x: f32, y: f32) -> Self {
13 Vec2 { x, y }
14 }
15
16 pub fn from_tile_pos(x: i16, y: i16) -> Self {
17 Vec2 {
18 x: (x as f32) * TILE_TO_POS_SCALE,
19 y: (y as f32 - 2.625) * TILE_TO_POS_SCALE,
21 }
22 }
23}
24
25impl Serializable for Vec2 {
26 fn serialize(&self, cursor: &mut SliceCursor) {
27 cursor.write(&self.x);
28 cursor.write(&self.y);
29 }
30}
31
32impl Deserializable for Vec2 {
33 fn deserialize(cursor: &mut SliceCursor) -> Self {
34 Self {
35 x: cursor.read(),
36 y: cursor.read(),
37 }
38 }
39}