Skip to content

Commit

Permalink
feat: beet reflect feature
Browse files Browse the repository at this point in the history
  • Loading branch information
mrchantey committed Jul 3, 2024
1 parent 29c980d commit acfca97
Show file tree
Hide file tree
Showing 17 changed files with 141 additions and 67 deletions.
36 changes: 22 additions & 14 deletions crates/beet_core/src/asset_actions/insert_on_asset_event.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
use super::*;
use beet_ecs::prelude::*;
use bevy::asset::LoadState;
// use bevy::asset::LoadState;
use bevy::ecs::schedule::SystemConfigs;
use bevy::prelude::*;

/// Inserts the given component when a matching asset event is received.
#[derive(PartialEq, Debug, Clone, Component, Reflect)]
#[derive(Debug, Clone, PartialEq, Component, Reflect)]
#[reflect(Component, ActionMeta)]
pub struct InsertOnAssetEvent<T: GenericActionComponent, A: Asset> {
pub struct InsertOnAssetEvent<T: GenericActionComponent, A: GenericActionAsset>
{
pub value: T,
pub asset_event: AssetEvent<A>,
pub asset_event: ReflectedAssetEvent<A>,
}

impl<T: GenericActionComponent, A: Asset> ActionMeta
impl<T: GenericActionComponent, A: GenericActionAsset> ActionMeta
for InsertOnAssetEvent<T, A>
{
fn category(&self) -> ActionCategory { ActionCategory::Behavior }
}

impl<T: GenericActionComponent, A: Asset> ActionSystems
impl<T: GenericActionComponent, A: GenericActionAsset> ActionSystems
for InsertOnAssetEvent<T, A>
{
fn systems() -> SystemConfigs {
Expand All @@ -30,9 +32,14 @@ impl<T: GenericActionComponent, A: Asset> ActionSystems
}
}

impl<T: GenericActionComponent, A: Asset> InsertOnAssetEvent<T, A> {
impl<T: GenericActionComponent, A: GenericActionAsset>
InsertOnAssetEvent<T, A>
{
pub fn new(value: T, asset_event: AssetEvent<A>) -> Self {
Self { value, asset_event }
Self {
value,
asset_event: asset_event.into(),
}
}
pub fn loaded(value: T, handle: &Handle<A>) -> Self {
Self::new(value, AssetEvent::LoadedWithDependencies {
Expand All @@ -42,36 +49,37 @@ impl<T: GenericActionComponent, A: Asset> InsertOnAssetEvent<T, A> {

pub fn matches_load_state(&self, state: LoadState) -> bool {
match (self.asset_event, state) {
(AssetEvent::Added { .. }, LoadState::Loaded) => true,
(AssetEvent::LoadedWithDependencies { .. }, LoadState::Loaded) => {
(ReflectedAssetEvent::Added { .. }, LoadState::Loaded) => true,
(ReflectedAssetEvent::LoadedWithDependencies { .. }, LoadState::Loaded) => {
true
}
(AssetEvent::Removed { .. }, LoadState::NotLoaded) => true,
(ReflectedAssetEvent::Removed { .. }, LoadState::NotLoaded) => true,
(_, _) => false,
}
}
}

fn insert_on_asset_event<T: GenericActionComponent, A: Asset>(
fn insert_on_asset_event<T: GenericActionComponent, A: GenericActionAsset>(
mut commands: Commands,
mut asset_events: EventReader<AssetEvent<A>>,
query: Query<(Entity, &InsertOnAssetEvent<T, A>), With<Running>>,
) {
for ev in asset_events.read() {
for (entity, action) in query.iter() {
if action.asset_event == *ev {
let action_event:AssetEvent<A> = action.asset_event.into();
if action_event == *ev {
commands.entity(entity).insert(action.value.clone());
}
}
}
}
fn insert_on_asset_status<T: GenericActionComponent, A: Asset>(
fn insert_on_asset_status<T: GenericActionComponent, A: GenericActionAsset>(
mut commands: Commands,
asset_server: Res<AssetServer>,
query: Query<(Entity, &InsertOnAssetEvent<T, A>), Added<Running>>,
) {
for (entity, action) in query.iter() {
let id = asset_event_id(action.asset_event);
let id = asset_event_id(action.asset_event.into());
let Some(state) = asset_server.get_load_state(id) else {
continue;
};
Expand Down
3 changes: 3 additions & 0 deletions crates/beet_core/src/asset_actions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
pub mod insert_on_asset_event;
#[allow(unused_imports)]
pub use self::insert_on_asset_event::*;
pub mod reflected_asset_event;
#[allow(unused_imports)]
pub use self::reflected_asset_event::*;
56 changes: 56 additions & 0 deletions crates/beet_core/src/asset_actions/reflected_asset_event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use bevy::prelude::*;



/// The vanilla asset event doesnt implement reflect so..
#[derive(Debug, Reflect, PartialEq, Event)]
pub enum ReflectedAssetEvent<A: Asset> {
/// Emitted whenever an [`Asset`] is added.
Added { id: AssetId<A> },
/// Emitted whenever an [`Asset`] value is modified.
Modified { id: AssetId<A> },
/// Emitted whenever an [`Asset`] is removed.
Removed { id: AssetId<A> },
/// Emitted when the last [`super::Handle::Strong`] of an [`Asset`] is dropped.
Unused { id: AssetId<A> },
/// Emitted whenever an [`Asset`] has been fully loaded (including its dependencies and all "recursive dependencies").
LoadedWithDependencies { id: AssetId<A> },
}


impl<A: Asset> Into<AssetEvent<A>> for ReflectedAssetEvent<A> {
fn into(self) -> AssetEvent<A> {
match self {
ReflectedAssetEvent::Added { id } => AssetEvent::Added { id },
ReflectedAssetEvent::Modified { id } => AssetEvent::Modified { id },
ReflectedAssetEvent::Removed { id } => AssetEvent::Removed { id },
ReflectedAssetEvent::Unused { id } => AssetEvent::Unused { id },
ReflectedAssetEvent::LoadedWithDependencies { id } => {
AssetEvent::LoadedWithDependencies { id }
}
}
}
}

impl<A: Asset> From<AssetEvent<A>> for ReflectedAssetEvent<A> {
fn from(event: AssetEvent<A>) -> Self {
match event {
AssetEvent::Added { id } => ReflectedAssetEvent::Added { id },
AssetEvent::Modified { id } => ReflectedAssetEvent::Modified { id },
AssetEvent::Removed { id } => ReflectedAssetEvent::Removed { id },
AssetEvent::Unused { id } => ReflectedAssetEvent::Unused { id },
AssetEvent::LoadedWithDependencies { id } => {
ReflectedAssetEvent::LoadedWithDependencies { id }
}
}
}
}


impl<A: Asset> Clone for ReflectedAssetEvent<A> {
fn clone(&self) -> Self {
*self
}
}

impl<A: Asset> Copy for ReflectedAssetEvent<A> {}
14 changes: 7 additions & 7 deletions crates/beet_core/src/ui/set_text_on_run.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use beet_ecs::prelude::*;
use bevy::ecs::query::QueryFilter;
use bevy::ecs::schedule::SystemConfigs;
use bevy::prelude::*;
use std::borrow::Cow;
Expand All @@ -9,13 +8,14 @@ use std::marker::PhantomData;
#[derive(Debug, Clone, PartialEq, Component, Reflect)]
#[reflect(Component, ActionMeta)]
/// Sets the [`Text`] of all entities matching the query on run.
pub struct SetTextOnRun<F: QueryFilter> {
pub struct SetTextOnRun<F: GenericActionComponent> {
pub value: Cow<'static, str>,
pub section: usize,
#[reflect(ignore)]
phantom: PhantomData<F>,
}

impl<F: QueryFilter> SetTextOnRun<F> {
impl<F: GenericActionComponent> SetTextOnRun<F> {
pub fn new(value: impl Into<Cow<'static, str>>) -> Self {
Self {
value: value.into(),
Expand All @@ -41,8 +41,8 @@ impl<F: QueryFilter> SetTextOnRun<F> {
}
}

fn set_text_on_run<F: 'static + Send + Sync + QueryFilter>(
mut texts: Query<&mut Text, F>,
fn set_text_on_run<F: GenericActionComponent>(
mut texts: Query<&mut Text, With<F>>,
query: Query<&SetTextOnRun<F>, Added<Running>>,
) {
for set_text_on_run in query.iter() {
Expand All @@ -53,10 +53,10 @@ fn set_text_on_run<F: 'static + Send + Sync + QueryFilter>(
}
}

impl<F: QueryFilter> ActionMeta for SetTextOnRun<F> {
impl<F: GenericActionComponent> ActionMeta for SetTextOnRun<F> {
fn category(&self) -> ActionCategory { ActionCategory::World }
}

impl<F: 'static + Send + Sync + QueryFilter> ActionSystems for SetTextOnRun<F> {
impl<F: GenericActionComponent> ActionSystems for SetTextOnRun<F> {
fn systems() -> SystemConfigs { set_text_on_run::<F>.in_set(TickSet) }
}
1 change: 1 addition & 0 deletions crates/beet_ecs/src/lifecycle/actions/insert_on_trigger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::ops::DerefMut;
#[reflect(Component, ActionMeta)]
pub struct InsertOnTrigger<E: GenericActionEvent, T: GenericActionComponent> {
pub value: T,
#[reflect(ignore)]
phantom: PhantomData<E>,
}
impl<E: GenericActionEvent, T: GenericActionComponent> Deref
Expand Down
18 changes: 12 additions & 6 deletions crates/beet_ecs/src/lifecycle/actions/lifecycle_actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,25 @@ use bevy::reflect::GetTypeRegistration;

/// Minimal traits generally required for an action component.
pub trait GenericActionComponent:
Clone + Component + FromReflect + GetTypeRegistration
Clone + FromReflect + GetTypeRegistration + Component
{
}
impl<T: Clone + Component + FromReflect + GetTypeRegistration>
GenericActionComponent for T
impl<T: Clone + FromReflect + GetTypeRegistration + Component>
GenericActionComponent for T
{
}
/// Minimal traits generally required for an action event.
pub trait GenericActionEvent:
Clone + Event + FromReflect + GetTypeRegistration
Clone + FromReflect + GetTypeRegistration + Event
{
}
impl<T: Clone + Event + FromReflect + GetTypeRegistration>
GenericActionEvent for T
impl<T: Clone + FromReflect + GetTypeRegistration + Event> GenericActionEvent
for T
{
}
/// Minimal traits generally required for an action asset type.
pub trait GenericActionAsset:
'static + Send + Sync + TypePath + Asset
{
}
impl<T: 'static + Send + Sync + TypePath + Asset> GenericActionAsset for T {}
6 changes: 3 additions & 3 deletions crates/beet_ecs/src/lifecycle/actions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ pub use self::insert_in_duration::*;
pub mod insert_on_run;
#[allow(unused_imports)]
pub use self::insert_on_run::*;
pub mod insert_on_trigger;
#[allow(unused_imports)]
pub use self::insert_on_trigger::*;
pub mod lifecycle_actions;
#[allow(unused_imports)]
pub use self::lifecycle_actions::*;
Expand All @@ -22,9 +25,6 @@ pub use self::remove_agent_on_run::*;
pub mod repeat;
#[allow(unused_imports)]
pub use self::repeat::*;
pub mod insert_on_trigger;
#[allow(unused_imports)]
pub use self::insert_on_trigger::*;
pub mod set_agent_on_run;
#[allow(unused_imports)]
pub use self::set_agent_on_run::*;
Expand Down
4 changes: 3 additions & 1 deletion crates/beet_ecs/src/lifecycle/actions/remove_agent_on_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use std::marker::PhantomData;

/// Removes a component on the agent when this behavior starts running.
#[derive(PartialEq, Deref, DerefMut, Debug, Clone, Component, Reflect)]
pub struct RemoveAgentOnRun<T: GenericActionComponent>(pub PhantomData<T>);
pub struct RemoveAgentOnRun<T: GenericActionComponent>(
#[reflect(ignore)] pub PhantomData<T>,
);

impl<T: GenericActionComponent> Default for RemoveAgentOnRun<T> {
fn default() -> Self { Self(PhantomData) }
Expand Down
4 changes: 2 additions & 2 deletions crates/beet_examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ tokio = ["beet/tokio"]

[dependencies]
forky_core.workspace = true
beet = { workspace = true }
# beet = { workspace = true, features = ["reflect"] }
# beet = { workspace = true }
beet = { workspace = true, features = ["reflect"] }
bevy = { workspace = true, default-features = true }
log.workspace = true
pretty_env_logger.workspace = true
Expand Down
15 changes: 9 additions & 6 deletions crates/beet_examples/examples/export_scenes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ mod basics_scenes;


struct SceneItem {
pub name: &'static str,
pub app: &'static str,
pub name: &'static str,
pub system: SystemConfigs,
}

Expand All @@ -19,17 +19,20 @@ impl SceneItem {
pub fn save(self) {
let mut app = App::new();
app.add_plugins((
TaskPoolPlugin::default(),
DefaultBeetPlugins::default(),
ExamplePlugin::default(),
// ExamplePlugin::default(),
))
.finish();
Schedule::default()
.add_systems(self.system)
.run(app.world_mut());

let filename = format!(
"crates/beet_examples/examples/{}/scenes/{}.ron",
self.app, self.name
"target/scenes/{}/{}.ron",
// "crates/beet_examples/examples/{}/scenes/{}.ron",
self.app,
self.name
);
Schedule::default()
.add_systems(save_scene(&filename))
Expand All @@ -41,13 +44,13 @@ impl SceneItem {
fn main() {
let scenes = vec![
SceneItem {
name: "hello_world",
app: "basics",
name: "hello_world",
system: basics_scenes::hello_world.into_configs(),
},
SceneItem {
name: "hello_net",
app: "basics",
name: "hello_net",
system: basics_scenes::hello_net.into_configs(),
},
];
Expand Down
5 changes: 3 additions & 2 deletions crates/beet_examples/src/components/dialog_panel.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::prelude::*;
use bevy::input::keyboard::Key;
use bevy::input::keyboard::KeyboardInput;
use bevy::prelude::*;
use crate::prelude::*;
use serde::Deserialize;
use serde::Serialize;

Expand Down Expand Up @@ -35,7 +35,8 @@ pub struct PlayerInput;
#[derive(Component)]
pub struct PlayerOutput;

#[derive(Component)]
#[derive(Clone, Component, Reflect)]
#[reflect(Component)]
pub struct StatusOutput;

#[derive(Component)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl EpisodeParams for FrozenLakeEpParams {

pub type FrozenLakeQTable = QTable<GridPos, GridDirection>;


#[derive(Debug, Reflect)]
pub struct FrozenLakeQTableSession;

impl RlSessionTypes for FrozenLakeQTableSession {
Expand Down
2 changes: 1 addition & 1 deletion crates/beet_ml/src/language/bert_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl Plugin for BertPlugin {

#[cfg(feature = "beet_core")]
app.add_plugins(
ActionPlugin::<FindSentenceSteerTarget<With<Sentence>>>::default(),
ActionPlugin::<FindSentenceSteerTarget<Sentence>>::default(),
);


Expand Down
Loading

0 comments on commit acfca97

Please sign in to comment.