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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
use crate::*;
/// Implements frame extraction, rendering, and animation creation for `SpriteSheet`.
impl SpriteSheet {
/// Creates a new sprite sheet from an image element and uniform frame dimensions.
///
/// Computes the grid columns and rows from the image dimensions and frame size.
///
/// # Arguments
///
/// - `HtmlImageElement` - The sprite sheet image.
/// - `f64` - The width of each frame.
/// - `f64` - The height of each frame.
///
/// # Returns
///
/// - `SpriteSheet` - The new sprite sheet.
pub fn from_image(image: HtmlImageElement, frame_width: f64, frame_height: f64) -> SpriteSheet {
let total_width: f64 = image.width() as f64;
let total_height: f64 = image.height() as f64;
let columns: u32 = (total_width / frame_width).max(1.0) as u32;
let rows: u32 = (total_height / frame_height).max(1.0) as u32;
SpriteSheet::new(image, frame_width, frame_height, columns, rows)
}
/// Returns the source rectangle for the frame at the given grid index.
///
/// # Arguments
///
/// - `u32` - The frame index (left-to-right, top-to-bottom).
///
/// # Returns
///
/// - `Rect` - The source rectangle.
pub fn frame_source(&self, index: u32) -> Rect {
let column: u32 = index % self.get_columns();
let row: u32 = index / self.get_columns();
Rect::new(
column as f64 * self.get_frame_width(),
row as f64 * self.get_frame_height(),
self.get_frame_width(),
self.get_frame_height(),
)
}
/// Creates a frame at the given grid index with a default duration.
///
/// # Arguments
///
/// - `u32` - The frame index.
///
/// # Returns
///
/// - `SpriteFrame` - The new frame.
pub fn frame(&self, index: u32) -> SpriteFrame {
SpriteFrame::new(self.frame_source(index), SPRITE_DEFAULT_FRAME_DURATION)
}
/// Creates an animation from a range of frame indices.
///
/// # Arguments
///
/// - `&str` - The animation name.
/// - `u32` - The starting frame index (inclusive).
/// - `u32` - The ending frame index (exclusive).
/// - `AnimationMode` - The playback mode.
///
/// # Returns
///
/// - `SpriteAnimation` - The new animation.
pub fn animation(
&self,
name: &str,
start: u32,
end: u32,
mode: AnimationMode,
) -> SpriteAnimation {
let frames: Vec<SpriteFrame> = (start..end).map(|index: u32| self.frame(index)).collect();
SpriteAnimation::new(name.to_string(), frames, mode)
}
/// Draws a static (non-animated) sprite frame onto the canvas.
///
/// # Arguments
///
/// - `&CanvasRenderingContext2d` - The canvas context.
/// - `u32` - The frame index to draw.
/// - `&Transform2D` - The world-space transform to apply.
pub fn draw_frame(
&self,
context: &CanvasRenderingContext2d,
frame_index: u32,
transform: &Transform2D,
) {
let source: Rect = self.frame_source(frame_index);
context.save();
let _ = context.translate(
transform.get_position().get_x(),
transform.get_position().get_y(),
);
let _ = context.rotate(transform.get_rotation());
let _ = context.scale(transform.get_scale().get_x(), transform.get_scale().get_y());
let _ = context
.draw_image_with_html_image_element_and_sw_and_sh_and_dx_and_dy_and_dw_and_dh(
self.get_image(),
source.get_x(),
source.get_y(),
source.get_width(),
source.get_height(),
-source.get_width() * 0.5,
-source.get_height() * 0.5,
source.get_width(),
source.get_height(),
);
context.restore();
}
}
/// Implements playback control for `Animator`.
impl Animator {
/// Creates a new idle animator with no animation.
///
/// # Returns
///
/// - `Animator` - The new animator.
pub fn create() -> Animator {
Animator::new(AnimationState::Paused, 1)
}
/// Plays the given animation from the beginning.
///
/// # Arguments
///
/// - `SpriteAnimation` - The animation to play.
pub fn play(&mut self, animation: SpriteAnimation) {
self.set_current_animation(Some(animation));
self.set_current_frame_index(0);
self.set_elapsed_time(0.0);
self.set_state(AnimationState::Playing);
self.set_direction(1);
}
/// Pauses the current animation.
pub fn pause(&mut self) {
if self.get_state() == AnimationState::Playing {
self.set_state(AnimationState::Paused);
}
}
/// Resumes the current animation from where it was paused.
pub fn resume(&mut self) {
if self.get_state() == AnimationState::Paused {
self.set_state(AnimationState::Playing);
}
}
/// Stops the current animation and resets to the first frame.
pub fn stop(&mut self) {
self.set_current_frame_index(0);
self.set_elapsed_time(0.0);
self.set_state(AnimationState::Paused);
self.set_direction(1);
}
/// Advances the animation by the given delta time.
///
/// # Arguments
///
/// - `f64` - The time elapsed since the last update, in seconds.
pub fn update(&mut self, delta_time: f64) {
if self.get_state() != AnimationState::Playing {
return;
}
let current_frame_index: usize = self.get_current_frame_index();
let (frame_count, current_duration, mode) = {
let Some(animation) = self.get_mut_current_animation().as_ref() else {
return;
};
if animation.get_frames().is_empty() {
return;
}
(
animation.get_frames().len(),
animation.get_frames()[current_frame_index].get_duration(),
animation.get_mode(),
)
};
*self.get_mut_elapsed_time() += delta_time;
if self.get_elapsed_time() < current_duration {
return;
}
self.set_elapsed_time(0.0);
self.advance_frame(frame_count, mode);
}
/// Returns the source rectangle of the current frame, or `None` if no animation is playing.
///
/// # Returns
///
/// - `Option<Rect>` - The current frame's source rectangle.
pub fn current_frame_source(&self) -> Option<Rect> {
let animation: Option<SpriteAnimation> = self.get_current_animation();
let animation: &SpriteAnimation = animation.as_ref()?;
let frame: &SpriteFrame = animation.get_frames().get(self.get_current_frame_index())?;
Some(frame.get_source())
}
/// Advances to the next frame according to the animation mode.
///
/// # Arguments
///
/// - `&SpriteAnimation` - The current animation.
fn advance_frame(&mut self, frame_count: usize, mode: AnimationMode) {
match mode {
AnimationMode::Loop => {
self.set_current_frame_index((self.get_current_frame_index() + 1) % frame_count);
}
AnimationMode::Once => {
if self.get_current_frame_index() + 1 < frame_count {
*self.get_mut_current_frame_index() += 1;
} else {
self.set_state(AnimationState::Finished);
}
}
AnimationMode::PingPong => {
if self.get_direction() > 0 {
if self.get_current_frame_index() + 1 < frame_count {
*self.get_mut_current_frame_index() += 1;
} else {
self.set_direction(-1);
if self.get_current_frame_index() > 0 {
*self.get_mut_current_frame_index() -= 1;
}
}
} else {
if self.get_current_frame_index() > 0 {
*self.get_mut_current_frame_index() -= 1;
} else {
self.set_direction(1);
if self.get_current_frame_index() + 1 < frame_count {
*self.get_mut_current_frame_index() += 1;
}
}
}
}
}
}
}
/// Implements animated sprite rendering for `Animator`.
impl Animator {
/// Draws the current frame of this animator onto the canvas.
///
/// # Arguments
///
/// - `&CanvasRenderingContext2d` - The canvas context.
/// - `&SpriteSheet` - The sprite sheet containing the image.
/// - `&Transform2D` - The world-space transform to apply.
pub fn draw(
&self,
context: &CanvasRenderingContext2d,
sheet: &SpriteSheet,
transform: &Transform2D,
) {
let Some(source) = self.current_frame_source() else {
return;
};
context.save();
let _ = context.translate(
transform.get_position().get_x(),
transform.get_position().get_y(),
);
let _ = context.rotate(transform.get_rotation());
let _ = context.scale(
if self.get_flip_x() {
-transform.get_scale().get_x()
} else {
transform.get_scale().get_x()
},
if self.get_flip_y() {
-transform.get_scale().get_y()
} else {
transform.get_scale().get_y()
},
);
let _ = context
.draw_image_with_html_image_element_and_sw_and_sh_and_dx_and_dy_and_dw_and_dh(
sheet.get_image(),
source.get_x(),
source.get_y(),
source.get_width(),
source.get_height(),
-source.get_width() * 0.5,
-source.get_height() * 0.5,
source.get_width(),
source.get_height(),
);
context.restore();
}
}
/// Implements `Default` for `Animator` as a new idle animator.
impl Default for Animator {
fn default() -> Animator {
Animator::create()
}
}