Skip to main content

euv_engine/sprite/
struct.rs

1use crate::*;
2
3/// Defines a single frame in a sprite animation.
4#[derive(Clone, Copy, Data, Debug, Default, New, PartialEq, PartialOrd)]
5pub struct SpriteFrame {
6    /// The source rectangle within the sprite sheet image.
7    #[get(type(copy))]
8    pub(crate) source: Rect,
9    /// The duration this frame should be displayed, in seconds.
10    #[get(type(copy))]
11    pub(crate) duration: f64,
12}
13
14/// Defines a sprite sheet with uniform frame grid dimensions.
15#[derive(Clone, Data, Debug, New, PartialEq)]
16pub struct SpriteSheet {
17    /// The source image element loaded from an asset.
18    pub(crate) image: HtmlImageElement,
19    /// The width of each individual frame in pixels.
20    #[get(type(copy))]
21    pub(crate) frame_width: f64,
22    /// The height of each individual frame in pixels.
23    #[get(type(copy))]
24    pub(crate) frame_height: f64,
25    /// The number of columns in the sprite sheet grid.
26    #[get(pub(crate), type(copy))]
27    #[get_mut(pub(crate))]
28    #[set(pub(crate))]
29    pub(crate) columns: u32,
30    /// The number of rows in the sprite sheet grid.
31    #[get(pub(crate), type(copy))]
32    #[get_mut(pub(crate))]
33    #[set(pub(crate))]
34    pub(crate) rows: u32,
35}
36
37/// A named sequence of frames that form an animation.
38#[derive(Clone, Data, Debug, New, PartialEq)]
39pub struct SpriteAnimation {
40    /// The name identifying this animation (e.g., `"idle"`, `"walk"`).
41    pub(crate) name: String,
42    /// The ordered list of frames in this animation.
43    pub(crate) frames: Vec<SpriteFrame>,
44    /// The playback mode (loop, once, ping-pong).
45    #[get(type(copy))]
46    pub(crate) mode: AnimationMode,
47}
48
49/// Manages the playback state of sprite animations.
50#[derive(Clone, Data, Debug, New, PartialEq)]
51pub struct Animator {
52    /// The currently active animation, if any.
53    #[get(type(clone))]
54    #[get_mut(pub(crate))]
55    #[new(skip)]
56    pub(crate) current_animation: Option<SpriteAnimation>,
57    /// The index of the current frame being displayed.
58    #[get(pub(crate), type(copy))]
59    #[get_mut(pub(crate))]
60    #[set(pub(crate))]
61    #[new(skip)]
62    pub(crate) current_frame_index: usize,
63    /// The elapsed time within the current frame, in seconds.
64    #[get(pub(crate), type(copy))]
65    #[get_mut(pub(crate))]
66    #[set(pub(crate))]
67    #[new(skip)]
68    pub(crate) elapsed_time: f64,
69    /// The current playback state.
70    #[get(type(copy))]
71    #[get_mut(pub(crate))]
72    pub(crate) state: AnimationState,
73    /// The direction of playback (1 = forward, -1 = backward) for ping-pong mode.
74    #[get(pub(crate), type(copy))]
75    #[get_mut(pub(crate))]
76    #[set(pub(crate))]
77    pub(crate) direction: i32,
78    /// Whether to flip the sprite horizontally when rendering.
79    #[get(type(copy))]
80    #[get_mut(pub(crate))]
81    #[new(skip)]
82    pub(crate) flip_x: bool,
83    /// Whether to flip the sprite vertically when rendering.
84    #[get(type(copy))]
85    #[get_mut(pub(crate))]
86    #[new(skip)]
87    pub(crate) flip_y: bool,
88}