Skip to content

Commit

Permalink
Refactor fn plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
Waridley committed Dec 16, 2023
1 parent b8d6f82 commit 84ab1be
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 30 deletions.
4 changes: 2 additions & 2 deletions rs/src/enemies.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::util::FnPluginExt;
use crate::util::IntoFnPlugin;
use bevy::prelude::*;
use enum_components::EnumComponent;

pub mod aa;
pub mod dummy;

pub fn plugin(app: &mut App) -> &mut App {
app.fn_plugin(dummy::plugin)
app.add_plugins(dummy::plugin.plugfn())
}

#[derive(EnumComponent, Reflect)]
Expand Down
14 changes: 7 additions & 7 deletions rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use bevy_rapier3d::prelude::*;
use particles::ParticlesPlugin;
use player::ctrl::CtrlVel;
use std::{f32::consts::*, fmt::Debug, time::Duration};
use util::FnPluginExt;
use util::IntoFnPlugin;

pub mod enemies;
pub mod mats;
Expand Down Expand Up @@ -67,18 +67,18 @@ pub fn run() {
ParticlesPlugin,
offloading::OffloadingPlugin,
planet::sky::SkyPlugin,
enemies::plugin.plugfn(),
player::plugin.plugfn(),
pickups::plugin.plugfn(),
settings::plugin.plugfn(),
planet::terrain::plugin.plugfn(),
ui::plugin.plugfn(),
))
.insert_resource(PkvStore::new_with_qualifier(
"studio",
"sonday",
env!("CARGO_PKG_NAME"),
))
.fn_plugin(enemies::plugin)
.fn_plugin(player::plugin)
.fn_plugin(pickups::plugin)
.fn_plugin(settings::plugin)
.fn_plugin(planet::terrain::plugin)
.fn_plugin(ui::plugin)
.add_plugins((
RonAssetPlugin::<BubbleMaterial>::new(&["mat.ron"]),
MaterialPlugin::<BubbleMaterial>::default(),
Expand Down
4 changes: 2 additions & 2 deletions rs/src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub const HOVER_HEIGHT: f32 = 2.0;
const G1: Group = Group::GROUP_1;

pub fn plugin(app: &mut App) -> &mut App {
app.fn_plugin(input::plugin)
app.add_plugins(input::plugin.plugfn())
.add_systems(Startup, setup)
.add_systems(
Update,
Expand Down Expand Up @@ -182,7 +182,7 @@ use crate::{
prefs::PlayerPrefs,
},
settings::Settings,
util::FnPluginExt,
util::IntoFnPlugin,
};
use player_entity::*;

Expand Down
5 changes: 2 additions & 3 deletions rs/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{util::FnPluginExt, DT};
use crate::{util::IntoFnPlugin, DT};
use bevy::{app::AppExit, prelude::*, utils::HashMap};
use bevy_rapier3d::prelude::*;
use colored::Colorize;
Expand Down Expand Up @@ -62,8 +62,7 @@ pub fn app() -> App {
.add_systems(Update, timeout)
.add_systems(Update, check_test_results);

app.fn_plugin(app_started)
.fn_plugin(slope_angles::angle_stability)
app.add_plugins((app_started.plugfn(), slope_angles::angle_stability.plugfn()))
.add_systems(Startup, crate::offloading::tests::spawning);

app
Expand Down
6 changes: 3 additions & 3 deletions rs/src/ui.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use crate::util::FnPluginExt;
use crate::util::IntoFnPlugin;
use bevy::prelude::*;
use enum_components::EnumComponent;

pub mod game_ui;
// pub mod pause_menu;

pub fn plugin(app: &mut App) -> &mut App {
app.fn_plugin(game_ui::plugin)
// .fn_plugin(pause_menu::plugin)
app.add_plugins(game_ui::plugin.plugfn())
// .add_plugins(pause_menu::plugin.plugfn())
}

#[derive(Debug, EnumComponent)]
Expand Down
23 changes: 16 additions & 7 deletions rs/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,27 @@ pub fn quantize<const BITS: u32>(value: f32) -> f32 {
d - t
}

/// Like [seldom_fn_plugin](https://crates.io/crates/seldom_fn_plugin) but fns must return `&mut App`
/// just so they don't have to have a semicolon at the end
pub trait FnPluginExt {
fn fn_plugin(&mut self, f: impl FnOnce(&mut App) -> &mut App) -> &mut Self;
pub struct FnPlugin<F: for<'a> Fn(&'a mut App) -> &'a mut App + Send + Sync + 'static>(F);

impl<F> Plugin for FnPlugin<F>
where
F: for<'a> Fn(&'a mut App) -> &'a mut App + Send + Sync + 'static,
{
fn build(&self, app: &mut App) {
(self.0)(app);
}
}

impl FnPluginExt for App {
fn fn_plugin(&mut self, f: impl FnOnce(&mut App) -> &mut App) -> &mut Self {
(f)(self)
pub trait IntoFnPlugin:
for<'a> Fn(&'a mut App) -> &'a mut App + Sized + Send + Sync + 'static
{
fn plugfn(self) -> FnPlugin<Self> {
FnPlugin(self)
}
}

impl<F: for<'a> Fn(&'a mut App) -> &'a mut App + Send + Sync + 'static> IntoFnPlugin for F {}

pub trait Spawnable {
type Params: SystemParam + 'static;
type InstanceData;
Expand Down
9 changes: 5 additions & 4 deletions rs/tools/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ use proc_macro::TokenStream;
use proc_macro2::Ident;
use quote::quote;
use std::f64::consts::{PI, TAU};
use syn::parse::{Parse, ParseStream};
use syn::spanned::Spanned;
use syn::{
braced, parse_macro_input, parse_quote, Error, Expr, ExprLit, FieldValue, Lit, LitInt, Member,
Path, Token,
braced,
parse::{Parse, ParseStream},
parse_macro_input, parse_quote,
spanned::Spanned,
Error, Expr, ExprLit, FieldValue, Lit, LitInt, Member, Path, Token,
};

/// Generates a kernel for 2-dimensional convolutional blurring of type `[[f64; SIZE]; SIZE]`.
Expand Down
2 changes: 1 addition & 1 deletion sond-bevy-enum-components
2 changes: 1 addition & 1 deletion sond-bevy-particles

0 comments on commit 84ab1be

Please sign in to comment.