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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use super::component_prelude::*;
/// Entities, which have `Velocity` and `DecreaseVelocity`, will decrease their velocity
/// every frame, by a certain amount (`x`, `y`).
/// Note that, the given `x` and `y` fields must _always_ be positive.
#[derive(Serialize, Deserialize)]
pub struct DecreaseVelocity {
pub x: f32,
pub y: f32,
/// Should decrease X velocity, when X velocity is POSITIVE
#[serde(default)]
pub(crate) should_decrease_x_pos: bool,
/// Should decrease X velocity, when X velocity is NEGATIVE
#[serde(default)]
pub(crate) should_decrease_x_neg: bool,
/// Should decrease Y velocity, when X velocity is POSITIVE
#[serde(default)]
pub(crate) should_decrease_y_pos: bool,
/// Should decrease Y velocity, when X velocity is NEGATIVE
#[serde(default)]
pub(crate) should_decrease_y_neg: bool,
}
impl DecreaseVelocity {
/// Create a new `DecreaseVelocity` component with the given `x` and `y` values.
/// `x` and `y` _must_ be positive.
pub fn new(x: f32, y: f32) -> Self {
Self {
x,
y,
should_decrease_x_pos: true,
should_decrease_x_neg: true,
should_decrease_y_pos: true,
should_decrease_y_neg: true,
}
}
/// Call this method, when you don't want this entity to decrease
/// its `x` velocity at all, in the next frame.
pub fn dont_decrease_x(&mut self) {
self.dont_decrease_x_when_pos();
self.dont_decrease_x_when_neg();
}
/// Call this method, when you don't want this entity to decrease
/// its `y` velocity at all, in the next frame.
pub fn dont_decrease_y(&mut self) {
self.dont_decrease_y_when_pos();
self.dont_decrease_y_when_neg();
}
/// Call this method, when you don't want this entity to decrease
/// its `x` velocity, when the `x` velocity is _positive_, in the next frame.
pub fn dont_decrease_x_when_pos(&mut self) {
self.should_decrease_x_pos = false;
}
/// Call this method, when you don't want this entity to decrease
/// its `x` velocity, when the `x` velocity is _negative_, in the next frame.
pub fn dont_decrease_x_when_neg(&mut self) {
self.should_decrease_x_neg = false;
}
/// Call this method, when you don't want this entity to decrease
/// its `y` velocity, when the `y` velocity is _positive_, in the next frame.
pub fn dont_decrease_y_when_pos(&mut self) {
self.should_decrease_y_pos = false;
}
/// Call this method, when you don't want this entity to decrease
/// its `y` velocity, when the `y` velocity is _positive_, in the next frame.
pub fn dont_decrease_y_when_neg(&mut self) {
self.should_decrease_y_neg = false;
}
}
impl Component for DecreaseVelocity {
type Storage = VecStorage<Self>;
}
impl From<(f32, f32)> for DecreaseVelocity {
fn from(data: (f32, f32)) -> Self {
Self::new(data.0, data.1)
}
}
impl From<Vector<f32>> for DecreaseVelocity {
fn from(data: Vector<f32>) -> Self {
Self::new(data.0, data.1)
}
}
impl Default for DecreaseVelocity {
fn default() -> Self {
Self {
x: 0.0,
y: 0.0,
should_decrease_x_pos: true,
should_decrease_x_neg: true,
should_decrease_y_pos: true,
should_decrease_y_neg: true,
}
}
}