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
//! ECS components

use bevy::{prelude::*, reflect::TypeUuid};
use serde::{Deserialize, Serialize};

pub(crate) fn add_components(app: &mut AppBuilder) {
    app.register_type::<Camera>()
        .register_type::<Color>()
        .register_type::<CameraSize>()
        .register_type::<Sprite>()
        .register_type::<SpriteSheet>()
        .register_type::<Visible>();
}

/// A floating point RGBA color
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Reflect)]
#[reflect_value(Serialize, Deserialize, PartialEq, Component)]
pub struct Color {
    pub r: f32,
    pub g: f32,
    pub b: f32,
    pub a: f32,
}

impl Color {
    pub fn new(r: f32, g: f32, b: f32, a: f32) -> Self {
        Self { r, g, b, a }
    }

    pub fn from_rgba8(r: u8, g: u8, b: u8, a: u8) -> Self {
        Self {
            r: r as f32 / 255.0,
            g: g as f32 / 255.0,
            b: b as f32 / 255.0,
            a: a as f32 / 255.0,
        }
    }
}

impl Default for Color {
    fn default() -> Self {
        Self {
            r: 0.,
            g: 0.,
            b: 0.,
            a: 1.,
        }
    }
}

/// The camera component
#[derive(Debug, Clone, Reflect)]
#[reflect(Component)]
pub struct Camera {
    /// The size of the camera along the fixed axis, which is by default the vertical axis
    pub size: CameraSize,
    /// Whether the camera should be centered about it's position. Defaults to `true`. If set to
    /// false `false`, the top-left corner of the camera will be at its [`Transform`].
    pub centered: bool,
    /// The background color of the camera
    ///
    /// This is the color that will be scene in the viewport when there are no sprites in the game
    /// area.
    pub background_color: Color,
    /// The color of the letter box
    ///
    /// The letter box is only visible when the camera size is set to [`LetterBoxed`][CameraSize::LetterBoxed].
    pub letterbox_color: Color,
    /// The aspect ratio of the pxiels when rendered through this camera
    pub pixel_aspect_ratio: f32,
    /// Additional shader code that will be added to the camera rendering that can be used for
    /// post-processing
    ///
    /// This must be a [OpenGL ES Shading Language 1.0][essl1] string.
    ///
    /// [essl1]: https://www.khronos.org/registry/OpenGL/specs/es/2.0/GLSL_ES_Specification_1.00.pdf
    ///
    /// ```ignore
    /// // Spawn the camera
    /// commands.spawn().insert_bundle(CameraBundle {
    ///     camera: Camera {
    ///         // Set our camera to have a fixed height and an auto-resized width
    ///         size: CameraSize::FixedHeight(100),
    ///         background_color: Color::new(0.2, 0.2, 0.2, 1.0),
    ///         custom_shader: Some(
    ///             CrtShader {
    ///                 // You can configure shader options here
    ///                 ..Default::default()
    ///             }
    ///             .get_shader(),
    ///         ),
    ///         ..Default::default()
    ///     },
    ///     ..Default::default()
    /// });
    /// ```
    pub custom_shader: Option<String>,
}

impl Default for Camera {
    fn default() -> Self {
        Self {
            size: Default::default(),
            centered: true,
            background_color: Color::default(),
            letterbox_color: Color::default(),
            pixel_aspect_ratio: 1.0,
            custom_shader: None,
        }
    }
}

/// The size of the 2D camera
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Reflect)]
#[reflect_value(PartialEq, Serialize, Deserialize)]
pub enum CameraSize {
    /// Fix the camera height in pixels and make the the width scale to whatever the window/screen
    /// size is.
    FixedHeight(u32),
    /// Fix the camera width in pixels and make the the height scale to whatever the window/screen
    /// size is.
    FixedWidth(u32),
    /// Fix the camera width and height in pixels and fill the empty space with the camera
    /// background color.
    LetterBoxed { width: u32, height: u32 },
}

impl Default for CameraSize {
    fn default() -> Self {
        Self::FixedHeight(200)
    }
}

#[derive(Debug, Clone)]
pub struct CameraTargetSizes {
    /// The target retro resolution of the camera
    pub low: UVec2,
    /// The target screen-ish resolution of the camera
    pub high: UVec2,
}

impl Camera {
    /// Get the size in game pixels ( retro-sized, not screen pixels ) of the camera view
    pub fn get_target_sizes(&self, window: &bevy::window::Window) -> CameraTargetSizes {
        let window_width = window.width();
        let window_height = window.height();
        let aspect_ratio = window_width / window_height;
        let low_res = match self.size {
            CameraSize::FixedHeight(height) => UVec2::new(
                // The width must be an even number to keep the alignment with non-pixel-perfect
                // sprites working ( for some reason I have not yet fully understood )
                {
                    let x = (aspect_ratio * height as f32 / self.pixel_aspect_ratio).floor() as u32;
                    if x % 2 != 0 {
                        x - 1
                    } else {
                        x
                    }
                },
                height,
            ),
            CameraSize::FixedWidth(width) => UVec2::new(width, {
                // The width must be an even number to keep the alignment with non-pixel-perfect
                // sprites working ( for some reason I have not yet fully understood )
                let y = (width as f32 / aspect_ratio * self.pixel_aspect_ratio).floor() as u32;
                if y % 2 != 0 {
                    y - 1
                } else {
                    y
                }
            }),
            CameraSize::LetterBoxed { width, height } => UVec2::new(width, height),
        };

        let multiple = (window_width as f32 / low_res.x as f32).ceil() as u32;
        let high_res = low_res * multiple;

        CameraTargetSizes {
            low: low_res,
            high: high_res,
        }
    }
}

/// Sprite options
#[derive(Debug, Clone, Reflect)]
#[reflect(Component)]
pub struct Sprite {
    /// Whether or not the sprite is centered about its position
    pub centered: bool,
    /// Flip the sprite on x
    pub flip_x: bool,
    /// Flip the sprite on y
    pub flip_y: bool,
    /// A visual offset for the sprite
    pub offset: Vec2,
    /// Whether or not to constrain the sprite rendering to perfect pixel alignment with the
    /// virtual, low resolution of the camera
    pub pixel_perfect: bool,
}

impl Default for Sprite {
    fn default() -> Self {
        Self {
            centered: true,
            flip_x: false,
            flip_y: false,
            offset: Vec2::default(),
            pixel_perfect: true,
        }
    }
}

/// Settings for a sprite sheet
#[derive(Debug, Clone, TypeUuid, Reflect)]
#[uuid = "64746631-1afe-4ca6-8398-7c0df62f7813"]
#[reflect(Component)]
pub struct SpriteSheet {
    pub grid_size: UVec2,
    pub tile_index: u32,
}

impl Default for SpriteSheet {
    fn default() -> Self {
        Self {
            grid_size: UVec2::splat(16),
            tile_index: 0,
        }
    }
}

/// Indicates whether or not an object should be rendered
#[derive(Debug, Clone, Copy, Reflect)]
#[reflect(Component)]
pub struct Visible(pub bool);
bevy_retrograde_macros::impl_deref!(Visible, bool);

impl Default for Visible {
    fn default() -> Self {
        Visible(true)
    }
}