Skip to content

Commit

Permalink
feat: deferred camera
Browse files Browse the repository at this point in the history
  • Loading branch information
eerii committed Aug 3, 2024
1 parent 4b9e8ad commit 1ac3745
Show file tree
Hide file tree
Showing 14 changed files with 890 additions and 273 deletions.
572 changes: 437 additions & 135 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ release = ["common", "embedded"]
common = []
# Individual features
embedded = ["include_dir"]
pixel_perfect = ["deferred"]
deferred = []
inspector = ["bevy-inspector-egui"]

[dependencies]
# Bevy and plugins
Expand All @@ -32,6 +35,7 @@ bevy_mod_picking = { version = "0.20", default-features = false, features = [
] }
# TODO: https://github.com/JoJoJet/bevy-trait-query/issues/59#issuecomment-2212516814
bevy-trait-query = { git = "https://github.com/RobWalt/bevy-trait-query.git", branch = "bevy-0.14-partial-update" }
bevy-inspector-egui = { version = "0.25", optional = true }

# Local crates
macros = { path = "macros" }
Expand Down
12 changes: 6 additions & 6 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 10 additions & 7 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,19 @@ pub fn asset_key(args: pm::TokenStream, input: pm::TokenStream) -> pm::TokenStre
.variants
.iter()
.map(|v| {
// TODO: Check that the value path is "asset" and allow multiple attrs
let name = v.ident.clone();
let attr = v
let asset_path = v
.attrs
.get(0)
.iter()
.filter_map(|attr| {
let Meta::NameValue(value) = attr.meta.clone() else { return None };
if value.path.get_ident()? != "asset" {
return None;
};
Some(value.value)
})
.next()
.expect("Each asset must provide a path attribute");
let Meta::NameValue(value) = attr.meta.clone() else {
panic!("The asset attribute must be in the form #[asset = \"path\"]");
};
let asset_path = value.value;
quote!((#ident::#name, asset_server.load(#asset_path)))
})
.collect();
Expand Down
57 changes: 34 additions & 23 deletions src/base/data.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Defines persistent data structures.
//! For a more complete solution, look at <https://github.com/umut-sahin/bevy-persistent>

use bevy::window::{PrimaryWindow, WindowResized};
use serde::{de::DeserializeOwned, Deserialize, Serialize};

use crate::prelude::*;
Expand All @@ -16,7 +17,9 @@ pub(super) fn plugin(app: &mut App) {
warn!("Couldn't create the save directory {}: {}", DATA_PATH, e);
};
app.insert_resource(SaveData::load())
.insert_resource(GameOptions::load());
.insert_resource(GameOptions::load())
.add_systems(OnEnter(GameState::Startup), init)
.add_systems(Update, on_resize.run_if(on_event::<WindowResized>()));
}

// Resources
Expand All @@ -25,38 +28,25 @@ pub(super) fn plugin(app: &mut App) {
/// Stores options that can be configured on the menu, related to accesibility
/// and customization.
#[persistent]
#[derive(Default)]
pub struct GameOptions {
/// The user configurable color palette of the game.
pub palette: ColorPalette,
/// If the window is allowed to resize
pub resizable: bool,
/// The last saved resolution of the window
pub resolution: UVec2,
}

/// Base colors used in the game and the ui.
#[derive(Debug, Reflect, Serialize, Deserialize, Copy!)]
pub struct ColorPalette {
pub light: Color,
pub primary: Color,
pub dark: Color,
pub darker: Color,
}

impl ColorPalette {
pub fn monocrome(base: Color) -> Self {
impl Default for GameOptions {
fn default() -> Self {
Self {
light: base.with_luminance(0.7).lighter(0.6),
primary: base.with_luminance(0.5),
dark: base.with_luminance(0.3),
darker: base.with_luminance(0.3).darker(0.07),
palette: ColorPalette::default(),
resizable: false,
resolution: UVec2::new(600, 600),
}
}
}

impl Default for ColorPalette {
fn default() -> Self {
Self::monocrome(css::ROYAL_BLUE.into())
}
}

/// Used to store information about the player, the level and game progress.
#[persistent]
#[derive(Default)]
Expand All @@ -65,6 +55,27 @@ pub struct SaveData {
pub test: bool,
}

// Systems
// ---

/// When the game starts, set the window resolution and resizability to the
/// value read in the options
fn init(mut window: Query<&mut Window, With<PrimaryWindow>>, options: Res<GameOptions>) {
let mut window = single_mut!(window);
let res = options.resolution.as_vec2();
window.resolution.set(res.x, res.y);
window.resizable = options.resizable;
}

/// When the window is resized, updates the saved resolution
fn on_resize(mut resize_events: EventReader<WindowResized>, mut options: ResMut<GameOptions>) {
for event in resize_events.read() {
let _ = options.update(|options| {
options.resolution = UVec2::new(event.width as u32, event.height as u32);
});
}
}

// Helpers
// ---

Expand Down
25 changes: 23 additions & 2 deletions src/components.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,36 @@
//! Some common components and their associated systems.

use bevy::sprite::MaterialMesh2dBundle;

use crate::prelude::*;

pub mod camera;
pub mod music;

pub(super) fn plugin(app: &mut App) {
app.add_plugins((camera::plugin, music::plugin));
app.add_plugins((camera::plugin, music::plugin))
.add_systems(OnEnter(GameState::Play), init);
}

/// The prelude for this module.
pub mod prelude {
pub use super::camera::GameCamera;
pub use super::camera::{FinalCamera, GameCamera};
}

/// Spawn test mesh (DELETE)
fn init(
mut cmd: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
) {
let color: Color = css::SEA_GREEN.into();
cmd.spawn((
MaterialMesh2dBundle {
mesh: meshes.add(Capsule2d::default()).into(),
transform: Transform::from_xyz(0., 0., 2.).with_scale(Vec3::splat(32.)),
material: materials.add(color),
..default()
},
StateScoped(GameState::Play),
));
}
28 changes: 21 additions & 7 deletions src/components/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@

use crate::prelude::*;

#[cfg(feature = "deferred")]
pub mod deferred;

pub(super) fn plugin(app: &mut App) {
app.add_systems(OnEnter(GameState::Startup), init);

#[cfg(feature = "deferred")]
app.add_plugins(deferred::plugin);
}

// Components
// ---

/// The camera where the game is being rendered.
#[derive(Component)]
pub struct GameCamera;
Expand All @@ -16,15 +25,20 @@ pub struct GameCamera;
#[derive(Component)]
pub struct FinalCamera;

// Systems
// ---

/// Spawn the main cameras.
fn init(mut cmd: Commands, options: Res<GameOptions>) {
let clear_color = ClearColorConfig::Custom(options.palette.darker);
let camera_bundle = Camera2dBundle {
camera: Camera {
clear_color,
cmd.spawn((
Camera2dBundle {
camera: Camera {
clear_color: ClearColorConfig::Custom(options.palette.darker),
..default()
},
..default()
},
..default()
};
cmd.spawn((camera_bundle, GameCamera, FinalCamera));
GameCamera,
FinalCamera,
));
}
Loading

0 comments on commit 1ac3745

Please sign in to comment.