bones_render/
sprite.rs

1//! Sprite rendering components.
2
3use crate::prelude::*;
4
5/// Image asset type, contains no data, but [`Handle<Image>`] is still useful because it uniquely
6/// represents an image that may be rendered outside of the core.
7#[derive(Copy, Clone, TypeUlid, Debug, Default)]
8#[ulid = "01GNJGPQ8TKA234G1EA510BD96"]
9pub struct Image;
10
11/// An atlas image asset type, contains no data, but [`Handle<Atlas>`] is still useful becaause it
12/// uniquely represents an atlas asset that may be rendered outside of the core.
13#[derive(Copy, Clone, TypeUlid, Debug, Default)]
14#[ulid = "01GNYXD7FVC46C7A3273HMEBRA"]
15pub struct Atlas;
16
17/// A 2D sprite component
18#[derive(Clone, TypeUlid, Debug, Default)]
19#[ulid = "01GNJXPWZKS6BHJEG1SX5B93DA"]
20pub struct Sprite {
21    /// The sprite's color tint
22    pub color: Color,
23    /// The sprite image handle.
24    pub image: Handle<Image>,
25    /// Whether or not the flip the sprite horizontally.
26    pub flip_x: bool,
27    /// Whether or not the flip the sprite vertically.
28    pub flip_y: bool,
29}
30
31/// An animated sprite component.
32///
33/// Represents one or more [`Atlas`]s stacked on top of each other, and possibly animated through a
34/// range of frames out of the atlas.
35#[derive(Debug, Default, Clone, TypeUlid)]
36#[ulid = "01GNYXFHC6T3NS061GMVFBXFYE"]
37pub struct AtlasSprite {
38    /// The sprite's color tint
39    pub color: Color,
40    /// This is the current index in the animation, with an `idx` of `0` meaning that the index in
41    /// the sprite sheet will be `start`.
42    ///
43    /// If the idx is greater than `end - start`, then the animation will loop around.
44    pub index: usize,
45    /// The atlas handle.
46    pub atlas: Handle<Atlas>,
47    /// Whether or not the flip the sprite horizontally.
48    pub flip_x: bool,
49    /// Whether or not the flip the sprite vertically.
50    pub flip_y: bool,
51}
52
53impl AtlasSprite {
54    /// Create a new [`AtlasSprite`] from the given atlas handle.
55    pub fn new(atlas: Handle<Atlas>) -> Self {
56        Self {
57            atlas,
58            color: Color::WHITE,
59            ..default()
60        }
61    }
62}