Skip to content

Commit

Permalink
feat(bevy): Update to bevy 0.11 (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
NiseVoid committed Jul 12, 2023
1 parent 0b9c989 commit 72bb861
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 70 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ homepage = "https://github.com/zkat/big-brain"
[workspace]

[dependencies]
bevy = { version = "0.10", default-features = false }
bevy = { version = "0.11", default-features = false }
big-brain-derive = { version = "=0.17.0", path = "./derive" }

[dev-dependencies]
bevy = { version = "0.10", default-features = true }
bevy = { version = "0.11", default-features = true }
rand = { version = "0.8.5", features = ["small_rng"] }

[features]
Expand Down
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,20 @@ Once all that's done, we just add our systems and off we go!
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugin(BigBrainPlugin)
.add_startup_system(init_entities)
.add_system(thirst_system)
.add_system_to_stage(BigBrainStage::Actions, drink_action_system)
.add_system_to_stage(BigBrainStage::Scorers, thirsty_scorer_system)
.add_plugins(BigBrainPlugin::new(PreUpdate))
.add_systems(Startup, init_entities)
.add_systems(Update, thirst_system)
.add_systems(PreUpdate, (
drink_action_system.in_set(BigBrainSet::Actions),
thirsty_scorer_system.in_set(BigBrainSet::Scorers),
))
.run();
}
```

#### bevy version

The current version of `big-brain` is compatible with `bevy` 0.9.0.
The current version of `big-brain` is compatible with `bevy` 0.11.0.

#### Contributing

Expand Down
13 changes: 9 additions & 4 deletions examples/concurrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,14 @@ fn main() {
filter: "big_brain=warn,concurrent=debug".to_string(),
..default()
}))
.add_plugin(BigBrainPlugin)
.add_startup_system(init_entities)
.add_system(guess_number_action.in_set(BigBrainSet::Actions))
.add_system(dummy_scorer_system.in_set(BigBrainSet::Scorers))
.add_plugins(BigBrainPlugin::new(PreUpdate))
.add_systems(Startup, init_entities)
.add_systems(
PreUpdate,
(
guess_number_action.in_set(BigBrainSet::Actions),
dummy_scorer_system.in_set(BigBrainSet::Scorers),
),
)
.run();
}
8 changes: 5 additions & 3 deletions examples/custom_measure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,17 +193,19 @@ fn main() {
filter: "big_brain=debug,custom_measure=debug".to_string(),
..default()
}))
.add_plugin(BigBrainPlugin)
.add_startup_system(init_entities)
.add_system(eat_dessert)
.add_plugins(BigBrainPlugin::new(PreUpdate))
.add_systems(Startup, init_entities)
.add_systems(Update, eat_dessert)
.add_systems(
PreUpdate,
(
eat_thing_action::<EatPancakes, Pancakes>,
eat_thing_action::<EatWaffles, Waffles>,
)
.in_set(BigBrainSet::Actions),
)
.add_systems(
PreUpdate,
(
craving_food_scorer::<CravingPancakes, Pancakes>,
craving_food_scorer::<CravingWaffles, Waffles>,
Expand Down
13 changes: 8 additions & 5 deletions examples/one_off.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,15 @@ fn main() {
filter: "big_brain=debug,one_off=debug".to_string(),
..default()
}))
.add_plugin(BigBrainPlugin)
.add_startup_system(init_entities)
.add_system(thirst_system)
// Big Brain has specific stages for Scorers and Actions. If
.add_plugins(BigBrainPlugin::new(PreUpdate))
.add_systems(Startup, init_entities)
.add_systems(Update, thirst_system)
// Big Brain has specific sets for Scorers and Actions. If
// determinism matters a lot to you, you should add your action and
// scorer systems to these stages.
.add_system(one_off_action_system.in_set(BigBrainSet::Actions))
.add_systems(
PreUpdate,
one_off_action_system.in_set(BigBrainSet::Actions),
)
.run();
}
9 changes: 5 additions & 4 deletions examples/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,12 +286,13 @@ fn main() {
filter: "big_brain=debug,sequence=debug".to_string(),
..default()
}))
.add_plugin(BigBrainPlugin)
.add_startup_system(init_entities)
.add_system(thirst_system)
.add_plugins(BigBrainPlugin::new(PreUpdate))
.add_systems(Startup, init_entities)
.add_systems(Update, thirst_system)
.add_systems(
PreUpdate,
(drink_action_system, move_to_water_source_action_system).in_set(BigBrainSet::Actions),
)
.add_system(thirsty_scorer_system.in_set(BigBrainSet::Scorers))
.add_systems(First, thirsty_scorer_system)
.run();
}
17 changes: 11 additions & 6 deletions examples/thirst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,18 @@ fn main() {
filter: "big_brain=debug,thirst=debug".to_string(),
..default()
}))
.add_plugin(BigBrainPlugin)
.add_startup_system(init_entities)
.add_system(thirst_system)
// Big Brain has specific stages for Scorers and Actions. If
.add_plugins(BigBrainPlugin::new(PreUpdate))
.add_systems(Startup, init_entities)
.add_systems(Update, thirst_system)
// Big Brain has specific sets for Scorers and Actions. If
// determinism matters a lot to you, you should add your action and
// scorer systems to these stages.
.add_system(drink_action_system.in_set(BigBrainSet::Actions))
.add_system(thirsty_scorer_system.in_set(BigBrainSet::Scorers))
.add_systems(
PreUpdate,
(
drink_action_system.in_set(BigBrainSet::Actions),
thirsty_scorer_system.in_set(BigBrainSet::Scorers),
),
)
.run();
}
2 changes: 2 additions & 0 deletions src/choices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::{

/// Contains different types of Considerations and Actions
#[derive(Debug, Clone, Reflect)]
#[reflect(from_reflect = false)]
pub struct Choice {
pub(crate) scorer: Scorer,
#[reflect(ignore)]
Expand All @@ -28,6 +29,7 @@ impl Choice {

/// Builds a new [`Choice`].
#[derive(Debug, Reflect)]
#[reflect(from_reflect = false)]
pub struct ChoiceBuilder {
when_label: Option<String>,
#[reflect(ignore)]
Expand Down
79 changes: 53 additions & 26 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,11 @@
//! fn main() {
//! App::new()
//! .add_plugins(DefaultPlugins)
//! .add_plugin(BigBrainPlugin)
//! .add_startup_system(init_entities)
//! .add_system(thirst_system)
//! .add_system(drink_action_system.in_set(BigBrainSet::Actions))
//! .add_system(thirsty_scorer_system.in_set(BigBrainSet::Scorers))
//! .add_plugins(BigBrainPlugin::new(PreUpdate))
//! .add_systems(Startup, init_entities)
//! .add_systems(Update, thirst_system)
//! .add_systems(PreUpdate, drink_action_system.in_set(BigBrainSet::Actions))
//! .add_systems(PreUpdate, thirsty_scorer_system.in_set(BigBrainSet::Scorers))
//! .run();
//! }
//! ```
Expand Down Expand Up @@ -202,7 +202,7 @@ pub mod prelude {
};
}

use bevy::prelude::*;
use bevy::{ecs::schedule::ScheduleLabel, prelude::*};

/// Core [`Plugin`] for Big Brain behavior. Required for any of the
/// [`Thinker`](thinker::Thinker)-related magic to work.
Expand All @@ -215,24 +215,49 @@ use bevy::prelude::*;
///
/// App::new()
/// .add_plugins(DefaultPlugins)
/// .add_plugin(BigBrainPlugin)
/// .add_plugin(BigBrainPlugin::new(Update))
/// // ...insert entities and other systems.
/// .run();
#[derive(Debug, Clone, Reflect)]
pub struct BigBrainPlugin;
#[reflect(from_reflect = false)]
pub struct BigBrainPlugin {
#[reflect(ignore)]
schedule: Box<dyn ScheduleLabel>,
#[reflect(ignore)]
cleanup_schedule: Box<dyn ScheduleLabel>,
}

impl BigBrainPlugin {
/// Create the BigBrain plugin which runs the scorers, thinker and actions in the specified
/// schedule
pub fn new(schedule: impl ScheduleLabel) -> Self {
Self {
schedule: Box::new(schedule),
cleanup_schedule: Box::new(Last),
}
}

/// Overwrite the Schedule that is used to run cleanup tasks. By default this happens in Last.
pub fn set_cleanup_schedule(mut self, cleanup_schedule: impl ScheduleLabel) -> Self {
self.cleanup_schedule = Box::new(cleanup_schedule);
self
}
}

impl Plugin for BigBrainPlugin {
fn build(&self, app: &mut App) {
app.configure_sets((
BigBrainSet::Scorers.in_base_set(CoreSet::First),
BigBrainSet::Thinkers
.in_base_set(CoreSet::First)
.after(BigBrainSet::Scorers),
BigBrainSet::Actions.in_base_set(CoreSet::PreUpdate),
BigBrainSet::Cleanup.in_base_set(CoreSet::Last),
));

app.add_systems(
app.configure_sets(
self.schedule.dyn_clone(),
(
BigBrainSet::Scorers,
BigBrainSet::Thinkers,
BigBrainSet::Actions,
)
.chain(),
)
.configure_set(self.cleanup_schedule.dyn_clone(), BigBrainSet::Cleanup)
.add_systems(
self.schedule.dyn_clone(),
(
scorers::fixed_score_system,
scorers::measured_scorers_system,
Expand All @@ -243,15 +268,17 @@ impl Plugin for BigBrainPlugin {
scorers::evaluating_scorer_system,
)
.in_set(BigBrainSet::Scorers),
);

app.add_system(thinker::thinker_system.in_set(BigBrainSet::Thinkers));

app.add_systems(
)
.add_systems(
self.schedule.dyn_clone(),
thinker::thinker_system.in_set(BigBrainSet::Thinkers),
)
.add_systems(
self.schedule.dyn_clone(),
(actions::steps_system, actions::concurrent_system).in_set(BigBrainSet::Actions),
);

app.add_systems(
)
.add_systems(
self.cleanup_schedule.dyn_clone(),
(
thinker::thinker_component_attach_system,
thinker::thinker_component_detach_system,
Expand Down
4 changes: 4 additions & 0 deletions src/scorers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,7 @@ impl ScorerBuilder for WinningScorerBuilder {
/// # }
/// ```
#[derive(Component, Debug, Reflect)]
#[reflect(from_reflect = false)]
pub struct EvaluatingScorer {
scorer: Scorer,
evaluator_string: String,
Expand Down Expand Up @@ -777,6 +778,7 @@ pub fn evaluating_scorer_system(
}

#[derive(Debug, Reflect)]
#[reflect(from_reflect = false)]
pub struct EvaluatingScorerBuilder {
#[reflect(ignore)]
scorer: Arc<dyn ScorerBuilder>,
Expand Down Expand Up @@ -855,6 +857,7 @@ impl ScorerBuilder for EvaluatingScorerBuilder {
/// ```

#[derive(Component, Debug, Reflect)]
#[reflect(from_reflect = false)]
pub struct MeasuredScorer {
threshold: f32,
#[reflect(ignore)]
Expand Down Expand Up @@ -917,6 +920,7 @@ pub fn measured_scorers_system(
}

#[derive(Debug, Reflect)]
#[reflect(from_reflect = false)]
pub struct MeasuredScorerBuilder {
threshold: f32,
#[reflect(ignore)]
Expand Down
5 changes: 3 additions & 2 deletions src/thinker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::{
#[derive(Debug, Clone, Component, Copy, Reflect)]
pub struct Actor(pub Entity);

#[derive(Debug, Clone, Copy, Reflect, FromReflect)]
#[derive(Debug, Clone, Copy, Reflect)]
pub struct Action(pub Entity);

impl Action {
Expand Down Expand Up @@ -61,7 +61,7 @@ impl ActionSpan {
}
}

#[derive(Debug, Clone, Copy, Reflect, FromReflect)]
#[derive(Debug, Clone, Copy, Reflect)]
pub struct Scorer(pub Entity);

#[derive(Debug, Clone, Component)]
Expand Down Expand Up @@ -128,6 +128,7 @@ impl ScorerSpan {
/// }
/// ```
#[derive(Component, Debug, Reflect)]
#[reflect(from_reflect = false)]
pub struct Thinker {
#[reflect(ignore)]
picker: Arc<dyn Picker>,
Expand Down
19 changes: 7 additions & 12 deletions tests/steps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,15 @@ use big_brain::{pickers, prelude::*};
fn steps() {
println!("steps test");
App::new()
.add_plugins(MinimalPlugins)
.add_plugin(BigBrainPlugin)
.add_plugins((MinimalPlugins, BigBrainPlugin::new(PreUpdate)))
.init_resource::<GlobalState>()
.add_startup_system(setup)
.add_system(
no_failure_score
.in_base_set(CoreSet::First)
.before(BigBrainSet::Scorers),
.add_systems(Startup, setup)
.add_systems(Update, no_failure_score.before(BigBrainSet::Scorers))
.add_systems(
PreUpdate,
(action1, action2, exit_action, failure_action).in_set(BigBrainSet::Actions),
)
.add_system(action1)
.add_system(action2)
.add_system(exit_action)
.add_system(failure_action)
.add_system(last.in_base_set(CoreSet::Last).before(BigBrainSet::Cleanup))
.add_systems(Last, last.before(BigBrainSet::Cleanup))
.run();
println!("end");
}
Expand Down

0 comments on commit 72bb861

Please sign in to comment.