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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use super::{SpriteType, Vector, Sprite};
use std::collections::HashSet;
use crate::Window;
impl Sprite {
/// Set the sprite's velocity vector.
///
/// If a velocity vector already exists, it is replaced.
/// Otherwise, a new velocity vector is added.
///
/// Values represent `(dx, dy)` in pixels per update tick.
pub fn set_velocity(&mut self, vx: i32, vy: i32) {
if let Some(Vector::Velocity(x, y)) =
self.vectors.iter_mut().find(|v| matches!(v, Vector::Velocity(_, _)))
{
*x = vx;
*y = vy;
} else {
self.vectors.push(Vector::Velocity(vx, vy));
}
}
/// Update one or both components of the sprite's velocity.
///
/// - `None` leaves the corresponding component unchanged.
/// - If no velocity vector exists, one is created.
pub fn update_velocity(&mut self, vx: Option<i32>, vy: Option<i32>) {
if let Some(Vector::Velocity(x, y)) =
self.vectors.iter_mut().find(|v| matches!(v, Vector::Velocity(_, _)))
{
if let Some(vx) = vx { *x = vx; }
if let Some(vy) = vy { *y = vy; }
} else {
self.vectors.push(Vector::Velocity(
vx.unwrap_or(0),
vy.unwrap_or(0),
));
}
}
/// Remove the sprite's velocity vector.
///
/// After calling this method, the sprite will no longer
/// move unless a new velocity is added.
pub fn remove_velocity(&mut self) {
self.vectors.retain(|v| !matches!(v, Vector::Velocity(_, _)));
}
/// Retrieve the current velocity of the sprite.
///
/// Returns `Some((dx, dy))` if a velocity vector exists,
/// or `None` if the sprite has no velocity.
pub fn velocity(&self) -> Option<(i32, i32)> {
self.vectors.iter().find_map(|v| {
if let Vector::Velocity(x, y) = *v {
Some((x, y))
} else {
None
}
})
}
/// Set the sprite's acceleration vector.
///
/// If an acceleration vector already exists, it is replaced.
/// Otherwise, a new acceleration vector is added.
///
/// Values represent `(ax, ay)` in pixels per tick².
pub fn set_acceleration(&mut self, ax: i32, ay: i32) {
if let Some(Vector::Acceleration(x, y)) =
self.vectors.iter_mut().find(|v| matches!(v, Vector::Acceleration(_, _)))
{
*x = ax;
*y = ay;
} else {
self.vectors.push(Vector::Acceleration(ax, ay));
}
}
/// Remove the sprite's acceleration vector.
///
/// After calling this method, the sprite's velocity
/// will no longer be modified by acceleration.
pub fn remove_acceleration(&mut self) {
self.vectors.retain(|v| !matches!(v, Vector::Acceleration(_, _)));
}
}
impl Window {
/// Add one or multiple vectors to all sprites of a given type
pub fn add_vector(&mut self, sprite_type: SpriteType, vector: Vector) {
for sprite in self.sprites.iter_mut() {
if sprite.sprite_type != sprite_type {
continue;
}
let mut replaced = false;
for existing in sprite.vectors.iter_mut() {
match (existing, vector) {
(Vector::Velocity(vx, vy), Vector::Velocity(nx, ny)) => {
*vx = nx;
*vy = ny;
replaced = true;
break;
}
(Vector::Acceleration(ax, ay), Vector::Acceleration(nx, ny)) => {
*ax = nx;
*ay = ny;
replaced = true;
break;
}
_ => {}
}
}
if !replaced {
sprite.vectors.push(vector);
}
}
}
/// Add multiple vectors at once, preventing duplicates
pub fn add_vectors(&mut self, sprite_type: SpriteType, vectors: impl Into<Vec<Vector>>) {
let vecs: Vec<Vector> = vectors.into();
for sprite in self.sprites.iter_mut() {
if sprite.sprite_type == sprite_type {
for &v in &vecs {
if !sprite.vectors.contains(&v) {
sprite.vectors.push(v);
}
}
}
}
}
/// Remove specific vectors or all vectors of a type
pub fn remove_vectors(&mut self, sprite_type: SpriteType, vector_to_remove: Option<Vector>) {
for sprite in self.sprites.iter_mut() {
if sprite.sprite_type != sprite_type { continue; }
if let Some(v) = vector_to_remove {
sprite.vectors.retain(|&vec| vec != v);
} else {
sprite.vectors.clear();
}
}
}
/// Update sprite positions based on velocity and acceleration vectors
/// Apply vectors to update sprite positions
pub fn apply_vectors(&mut self) {
for sprite in self.sprites.iter_mut() {
let mut dx = 0;
let mut dy = 0;
let mut seen = HashSet::new();
// --- First pass: apply acceleration to velocity ---
let mut accel_list = Vec::new();
for vec in &sprite.vectors {
if seen.contains(vec) { continue; }
seen.insert(*vec);
if let Vector::Acceleration(ax, ay) = vec {
accel_list.push((*ax, *ay));
}
}
for (ax, ay) in accel_list {
// find a velocity vector
if let Some(Vector::Velocity(vx, vy)) = sprite.vectors.iter_mut()
.find(|v| matches!(v, Vector::Velocity(_, _)))
{
*vx += ax;
*vy += ay;
} else {
sprite.vectors.push(Vector::Velocity(ax, ay));
}
}
// --- Second pass: apply velocity to position ---
for vec in &sprite.vectors {
if let Vector::Velocity(vx, vy) = vec {
dx += *vx;
dy += *vy;
}
}
let (x, y) = sprite.position;
sprite.position = ((x as i32 + dx) as usize, (y as i32 + dy) as usize);
}
}
}