azalea_physics/
client_movement.rs1use azalea_core::position::Vec2;
2use bevy_ecs::component::Component;
3
4#[derive(Clone, Component, Default)]
10pub struct ClientMovementState {
11 pub position_remainder: u32,
14 pub was_sprinting: bool,
15 pub trying_to_sprint: bool,
18
19 pub trying_to_crouch: bool,
30
31 pub move_direction: WalkDirection,
32 pub move_vector: Vec2,
33}
34
35#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
41pub enum WalkDirection {
42 #[default]
43 None,
44 Forward,
45 Backward,
46 Left,
47 Right,
48 ForwardRight,
49 ForwardLeft,
50 BackwardRight,
51 BackwardLeft,
52}
53impl WalkDirection {
54 pub fn forward(self) -> bool {
57 DirectionStates::from(self).forward
58 }
59 pub fn backward(self) -> bool {
62 DirectionStates::from(self).backward
63 }
64 pub fn left(self) -> bool {
66 DirectionStates::from(self).left
67 }
68 pub fn right(self) -> bool {
71 DirectionStates::from(self).right
72 }
73
74 pub fn set_forward(&mut self, value: bool) {
75 let mut d = DirectionStates::from(*self);
76 d.forward = value;
77 *self = d.into();
78 }
79 pub fn set_backward(&mut self, value: bool) {
80 let mut d = DirectionStates::from(*self);
81 d.backward = value;
82 *self = d.into();
83 }
84 pub fn set_left(&mut self, value: bool) {
85 let mut d = DirectionStates::from(*self);
86 d.left = value;
87 *self = d.into();
88 }
89 pub fn set_right(&mut self, value: bool) {
90 let mut d = DirectionStates::from(*self);
91 d.right = value;
92 *self = d.into();
93 }
94
95 pub fn opposite(self) -> Self {
108 match self {
109 Self::None => Self::None,
110 Self::Forward => Self::Backward,
111 Self::Backward => Self::Forward,
112 Self::Left => Self::Right,
113 Self::Right => Self::Left,
114 Self::ForwardRight => Self::BackwardLeft,
115 Self::ForwardLeft => Self::BackwardRight,
116 Self::BackwardRight => Self::ForwardLeft,
117 Self::BackwardLeft => Self::ForwardRight,
118 }
119 }
120}
121#[derive(Default)]
126pub struct DirectionStates {
127 pub forward: bool,
128 pub backward: bool,
129 pub left: bool,
130 pub right: bool,
131}
132impl From<WalkDirection> for DirectionStates {
133 fn from(d: WalkDirection) -> Self {
134 let mut s = Self::default();
135 match d {
136 WalkDirection::None => {}
137 WalkDirection::Forward => s.forward = true,
138 WalkDirection::Backward => s.backward = true,
139 WalkDirection::Left => s.left = true,
140 WalkDirection::Right => s.right = true,
141 WalkDirection::ForwardRight => {
142 s.forward = true;
143 s.right = true
144 }
145 WalkDirection::ForwardLeft => {
146 s.forward = true;
147 s.left = true
148 }
149 WalkDirection::BackwardRight => {
150 s.backward = true;
151 s.right = true
152 }
153 WalkDirection::BackwardLeft => {
154 s.backward = true;
155 s.left = true
156 }
157 };
158 s
159 }
160}
161impl From<DirectionStates> for WalkDirection {
162 fn from(d: DirectionStates) -> Self {
163 let left = d.left && !d.right;
164 let right = d.right && !d.left;
165
166 if d.forward && !d.backward {
167 return if right {
168 Self::ForwardRight
169 } else if left {
170 Self::ForwardLeft
171 } else {
172 Self::Forward
173 };
174 } else if d.backward && !d.forward {
175 if right {
176 Self::BackwardRight
177 } else if left {
178 Self::BackwardLeft
179 } else {
180 Self::Backward
181 };
182 }
183 if right {
184 Self::Right
185 } else if left {
186 Self::Left
187 } else {
188 Self::None
189 }
190 }
191}
192
193#[derive(Clone, Copy, Debug)]
196pub enum SprintDirection {
197 Forward,
198 ForwardRight,
199 ForwardLeft,
200}
201
202impl From<SprintDirection> for WalkDirection {
203 fn from(d: SprintDirection) -> Self {
204 match d {
205 SprintDirection::Forward => WalkDirection::Forward,
206 SprintDirection::ForwardRight => WalkDirection::ForwardRight,
207 SprintDirection::ForwardLeft => WalkDirection::ForwardLeft,
208 }
209 }
210}