Skip to content

Commit

Permalink
fix: ml plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
mrchantey committed Jul 11, 2024
1 parent 87835f1 commit 8beb222
Show file tree
Hide file tree
Showing 13 changed files with 45 additions and 145 deletions.
4 changes: 2 additions & 2 deletions crates/beet_ecs/macros/src/action/derive_action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ fn impl_action_systems(
};

Ok(quote! {
impl #impl_generics ActionSystems for #ident #type_generics #where_clause {
fn on_build(app: &mut App, config: &BeetConfig) {
impl #impl_generics ActionBuilder for #ident #type_generics #where_clause {
fn build(app: &mut App, config: &BeetConfig) {
#add_systems
#add_global_observers
#component_hooks
Expand Down
27 changes: 0 additions & 27 deletions crates/beet_ecs/src/action/action_systems.rs

This file was deleted.

12 changes: 0 additions & 12 deletions crates/beet_ecs/src/action/mod.rs

This file was deleted.

105 changes: 20 additions & 85 deletions crates/beet_ecs/src/action_builder/action_builder.rs
Original file line number Diff line number Diff line change
@@ -1,92 +1,27 @@
use crate::action::ActionCategory;
use bevy::ecs::component::ComponentId;
use bevy::ecs::world::DeferredWorld;
use crate::prelude::*;
use bevy::prelude::*;
use std::marker::PhantomData;
use bevy::utils::all_tuples;


pub struct ActionBuilder<T: ActionBuilderTypes> {
category: ActionCategory,
on_add: Option<fn(DeferredWorld, Entity, ComponentId)>,
on_remove: Option<fn(DeferredWorld, Entity, ComponentId)>,
// observers: T::Observers,
phantom: PhantomData<T>,
}

impl<T: ActionBuilderComponent> Default
for ActionBuilder<DefaultActionTypes<T>>
{
fn default() -> Self {
Self {
category: ActionCategory::default(),
on_add: None,
on_remove: None,
phantom: PhantomData,
}
}
}

// just use name as a placeholder
impl ActionBuilder<DefaultActionTypes<Name>> {
pub fn new<C: ActionBuilderComponent>(
) -> ActionBuilder<DefaultActionTypes<C>> {
default()
}
}


impl<T: ActionBuilderTypes> ActionBuilder<T> {
// pub fn add_observers(

pub fn on_add(
mut self,
on_add: fn(DeferredWorld, Entity, ComponentId),
) -> Self {
self.on_add = Some(on_add);
self
}
pub fn on_remove(
mut self,
on_remove: fn(DeferredWorld, Entity, ComponentId),
) -> Self {
self.on_remove = Some(on_remove);
self
}

pub fn with_category(mut self, category: ActionCategory) -> Self {
self.category = category;
self
}
pub trait ActionBuilder: Sized {
fn build(app: &mut App, config: &BeetConfig);
}

impl<T: ActionBuilderTypes> Plugin for ActionBuilder<T> {
fn build(&self, app: &mut App) {
#[cfg(feature = "reflect")]
app.register_type::<T::Component>();

let world = app.world_mut();
let hooks = world.register_component_hooks::<T::Component>();
if let Some(on_add) = self.on_add {
hooks.on_add(on_add);
}
if let Some(on_remove) = self.on_remove {
hooks.on_remove(on_remove);
}
macro_rules! impl_plugins_tuples {
($($param: ident),*) => {
impl<$($param),*> ActionBuilder for ($($param,)*)
where
$($param: ActionBuilder),*
{
#[allow(non_snake_case, unused_variables)]
#[track_caller]
fn build(app:&mut App, config: &BeetConfig){
$(
$param::build(app, config);
)*
// ($($param::systems(),)*).into_configs()
}
}
}
}
#[cfg(feature = "reflect")]
pub trait ActionBuilderComponent: Component + bevy::reflect::GetTypeRegistration {}
#[cfg(feature = "reflect")]
impl<T: Component + bevy::reflect::GetTypeRegistration> ActionBuilderComponent for T {}
#[cfg(not(feature = "reflect"))]
pub trait ActionBuilderComponent: Component {}
#[cfg(not(feature = "reflect"))]
impl<T: Component> ActionBuilderComponent for T {}

pub trait ActionBuilderTypes: 'static + Send + Sync {
type Component: ActionBuilderComponent;
}
pub struct DefaultActionTypes<T: ActionBuilderComponent>(PhantomData<T>);
impl<T: ActionBuilderComponent> ActionBuilderTypes for DefaultActionTypes<T> {
type Component = T;
}
all_tuples!(impl_plugins_tuples, 1, 15, P);
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ use bevy::prelude::*;
use std::marker::PhantomData;


/// Plugin that adds all [`ActionSystems`] to the schedule in [`BeetConfig`], and inits the components.
/// Plugin that adds all [`ActionBuilder`] to the schedule in [`BeetConfig`], and inits the components.
#[derive(Debug, Copy, Clone)]
pub struct ActionPlugin<T: 'static + Send + Sync + Bundle + ActionSystems> {
pub struct ActionPlugin<T: 'static + Send + Sync + Bundle + ActionBuilder> {
phantom: PhantomData<T>,
}

impl<T: 'static + Send + Sync + Bundle + ActionSystems> Default
impl<T: 'static + Send + Sync + Bundle + ActionBuilder> Default
for ActionPlugin<T>
{
fn default() -> Self {
Expand All @@ -27,7 +27,7 @@ impl<
+ Bundle
+ Reflect
+ bevy::reflect::GetTypeRegistration
+ ActionSystems,
+ ActionBuilder,
> Plugin for ActionPlugin<T>
// where
// Self: ActionMeta,
Expand All @@ -39,19 +39,19 @@ impl<
}

#[cfg(not(feature = "reflect"))]
impl<T: 'static + Send + Sync + Bundle + ActionSystems> Plugin
impl<T: 'static + Send + Sync + Bundle + ActionBuilder> Plugin
for ActionPlugin<T>
{
fn build(&self, app: &mut App) { build_common::<T>(app); }
}

fn build_common<T: 'static + Send + Sync + Bundle + ActionSystems>(
fn build_common<T: 'static + Send + Sync + Bundle + ActionBuilder>(
app: &mut App,
) {
let world = app.world_mut();
world.init_bundle::<T>();

app.init_resource::<BeetConfig>();
let settings = app.world().resource::<BeetConfig>();
T::on_build(app, &settings.clone());
T::build(app, &settings.clone());
}
File renamed without changes.
9 changes: 9 additions & 0 deletions crates/beet_ecs/src/action_builder/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
pub mod action_builder;
#[allow(unused_imports)]
pub use self::action_builder::*;
pub mod action_meta;
#[allow(unused_imports)]
pub use self::action_meta::*;
pub mod action_plugin;
#[allow(unused_imports)]
pub use self::action_plugin::*;
pub mod beet_config;
#[allow(unused_imports)]
pub use self::beet_config::*;
2 changes: 0 additions & 2 deletions crates/beet_ecs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#![allow(deprecated)] // TODO remove deprecated
#![feature(result_flattening, let_chains, associated_type_defaults)]
pub mod action;
pub mod action_builder;
pub mod actions;
pub mod events;
Expand All @@ -17,7 +16,6 @@ pub mod tree;
extern crate self as beet_ecs;

pub mod prelude {
pub use crate::action::*;
pub use crate::action_builder::*;
pub use crate::actions::flow::*;
#[allow(ambiguous_glob_reexports)]
Expand Down
9 changes: 5 additions & 4 deletions crates/beet_examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ keywords.workspace = true
categories.workspace = true

[features]
default = ["beet_ecs/reflect"]
default = []
# to enable reflect we need OnInsert to derive reflect https://github.com/bevyengine/bevy/pull/14259
reflect = ["beet_ecs/reflect"]
tokio = ["beet_net/tokio"]


[dependencies]
forky_core.workspace = true
beet_ecs.workspace = true
Expand Down Expand Up @@ -47,5 +48,5 @@ tokio.workspace = true


[[example]]
name="export_scenes"
path="./examples/export_scenes.rs"
name = "export_scenes"
path = "./examples/export_scenes.rs"
3 changes: 0 additions & 3 deletions crates/beet_examples/src/serde_utils/ready_on_asset_load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@ pub fn ready_on_asset_load<A: Asset>(
}
_ => {}
}
for asset in all_blocks.iter() {
log::info!("remaining {}", asset);
}
}
let total_blocks = all_blocks.iter().count();
if total_blocks > 0 && total_blocks == total_ready {
Expand Down
3 changes: 1 addition & 2 deletions crates/beet_ml/src/language/bert_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ impl Plugin for BertPlugin {
app.add_plugins(ActionPlugin::<(
SentenceFlow,
SetSentenceOnUserInput,
//we need OnInsert to derive reflect https://github.com/bevyengine/bevy/pull/14259
// RunOnSentenceChange
RunOnSentenceChange
)>::default())
.init_asset::<Bert>()
.init_asset_loader::<BertLoader>()
Expand Down
2 changes: 1 addition & 1 deletion examples/hello_action.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use beet::prelude::*;
use bevy::prelude::*;

#[derive(Action)]
#[derive(Component, Action)]
#[observers(log_on_run)]
struct LogOnRun(pub String);

Expand Down

0 comments on commit 8beb222

Please sign in to comment.