1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use super::component_prelude::*;
/// For entities which have `Velocity` and `MaxVelocity`, their velocities will never
/// be larger than the positive values of `MaxVelocity`, and will never be
/// smaller than the negative values of `MaxVelocity`.
/// Fields are both optional; if a field is `None`, then there is no cap for that value.
#[derive(Serialize, Deserialize)]
pub struct MaxVelocity {
pub x: Option<f32>,
pub y: Option<f32>,
pub(crate) should_limit_x: bool,
pub(crate) should_limit_y: bool,
}
impl MaxVelocity {
/// Create a new `MaxVelocity` component with both `x` and `y` fields.
pub fn with_xy(x: f32, y: f32) -> Self {
Self {
x: Some(x),
y: Some(y),
should_limit_x: true,
should_limit_y: true,
}
}
/// Create a new `MaxVelocity` component with only a max `x` value.
pub fn with_x(x: f32) -> Self {
Self {
x: Some(x),
y: None,
should_limit_x: true,
should_limit_y: true,
}
}
/// Create a new `MaxVelocity` component with only a max `y` value.
pub fn with_y(y: f32) -> Self {
Self {
x: None,
y: Some(y),
should_limit_x: true,
should_limit_y: true,
}
}
/// Skip velocity limitation next frame.
pub fn dont_limit(&mut self) {
self.dont_limit_x();
self.dont_limit_y();
}
/// Skip velocity limitation (x) next frame.
pub fn dont_limit_x(&mut self) {
self.should_limit_x = true;
}
/// Skip velocity limitation (y) next frame.
pub fn dont_limit_y(&mut self) {
self.should_limit_y = true;
}
}
impl Component for MaxVelocity {
type Storage = VecStorage<Self>;
}
impl From<(f32, f32)> for MaxVelocity {
fn from(data: (f32, f32)) -> Self {
Self::with_xy(data.0, data.1)
}
}
impl From<Vector<f32>> for MaxVelocity {
fn from(data: Vector<f32>) -> Self {
Self::with_xy(data.0, data.1)
}
}
impl From<(Option<f32>, Option<f32>)> for MaxVelocity {
fn from(data: (Option<f32>, Option<f32>)) -> Self {
Self {
x: data.0,
y: data.1,
should_limit_x: true,
should_limit_y: true,
}
}
}