Skip to content

Commit

Permalink
feat(deps): Update to Bevy 0.9 (#59)
Browse files Browse the repository at this point in the history
* Update toml

* Use new Plugin settings setup

* Derive Resource

* `spawn()` -> `spawn_empty()`

* Use new bundle-spawning setup
  • Loading branch information
Weibye committed Nov 13, 2022
1 parent 4486af1 commit 8ce5cd1
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 69 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ repository = "https://github.com/zkat/big-brain"
homepage = "https://github.com/zkat/big-brain"

[dependencies]
bevy = { version = "0.8.1", default-features = false }
bevy = { version = "0.9.0", default-features = false }

[dev-dependencies]
bevy = { version = "0.8.1", default-features = true }
bevy = { version = "0.9.0", default-features = true }

[features]
trace = []
62 changes: 30 additions & 32 deletions examples/custom_measure.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! This example demonstrates how to build a custom measure and use that
//! in a Thinker.

use bevy::log::LogSettings;
use bevy::log::LogPlugin;
use bevy::prelude::*;
use bevy::utils::tracing::debug;
use big_brain::prelude::*;
Expand Down Expand Up @@ -156,45 +156,43 @@ pub fn craving_food_scorer<

// Let's set up our world
pub fn init_entities(mut cmd: Commands) {
cmd.spawn()
.insert(Pancakes(50.0))
.insert(Waffles(50.0))
.insert(
Thinker::build()
.label("Hungry Thinker")
.picker(FirstToScore::new(0.5))
// we use our custom measure here. The impact of the custom measure is that the
// pancakes should be down-weighted. This means despite this being listed first,
// all things being equal we should consume pancakes before waffles.
.when(
MeasuredScorer::build(0.1)
.label("eat some waffles")
.measure(SumWithDecreasingWeightMeasure)
.push(CravingWaffles, 1.0)
.push(CravingPancakes, 1.0),
EatWaffles,
)
// we use the default measure here
.when(
MeasuredScorer::build(0.1)
.label("eat some pancakes")
.push(CravingPancakes, 1.0)
.push(CravingWaffles, 1.0),
EatPancakes,
),
);
cmd.spawn((
Pancakes(50.0),
Waffles(50.0),
Thinker::build()
.label("Hungry Thinker")
.picker(FirstToScore::new(0.5))
// we use our custom measure here. The impact of the custom measure is that the
// pancakes should be down-weighted. This means despite this being listed first,
// all things being equal we should consume pancakes before waffles.
.when(
MeasuredScorer::build(0.1)
.label("eat some waffles")
.measure(SumWithDecreasingWeightMeasure)
.push(CravingWaffles, 1.0)
.push(CravingPancakes, 1.0),
EatWaffles,
)
// we use the default measure here
.when(
MeasuredScorer::build(0.1)
.label("eat some pancakes")
.push(CravingPancakes, 1.0)
.push(CravingWaffles, 1.0),
EatPancakes,
),
));
}

fn main() {
// Once all that's done, we just add our systems and off we go!
App::new()
.insert_resource(LogSettings {
.add_plugins(DefaultPlugins.set(LogPlugin {
// Use `RUST_LOG=big_brain=trace,custom_measure=trace cargo run --example
// custom_measure --features=trace` to see extra tracing output.
filter: "big_brain=debug,custom_measure=debug".to_string(),
..Default::default()
})
.add_plugins(DefaultPlugins)
..default()
}))
.add_plugin(BigBrainPlugin)
.add_startup_system(init_entities)
.add_system(eat_dessert)
Expand Down
14 changes: 7 additions & 7 deletions examples/one_off.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bevy::log::LogSettings;
use bevy::log::LogPlugin;
use bevy::prelude::*;
use bevy::utils::tracing::{debug, trace};
use big_brain::prelude::*;
Expand Down Expand Up @@ -26,11 +26,12 @@ fn one_off_action_system(mut query: Query<(&mut ActionState, &ActionSpan), With<
pub fn init_entities(mut cmd: Commands) {
// You at least need to have a Thinker in order to schedule one-off
// actions. It's not a general-purpose task scheduler.
cmd.spawn().insert(Thirst::new(75.0, 2.0)).insert(
cmd.spawn((
Thirst::new(75.0, 2.0),
Thinker::build()
.label("My Thinker")
.picker(FirstToScore { threshold: 0.8 }),
);
));
}

#[derive(Component, Debug)]
Expand Down Expand Up @@ -69,13 +70,12 @@ pub fn thirst_system(
fn main() {
// Once all that's done, we just add our systems and off we go!
App::new()
.insert_resource(LogSettings {
.add_plugins(DefaultPlugins.set(LogPlugin {
// Use `RUST_LOG=big_brain=trace,thirst=trace cargo run --example
// one_off --features=trace` to see extra tracing output.
filter: "big_brain=debug,one_off=debug".to_string(),
..Default::default()
})
.add_plugins(DefaultPlugins)
..default()
}))
.add_plugin(BigBrainPlugin)
.add_startup_system(init_entities)
.add_system(thirst_system)
Expand Down
40 changes: 23 additions & 17 deletions examples/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//! Note that it does not matter if the actor is already near a water source:
//! the MoveToWaterSource action will simply terminate immediately.

use bevy::log::LogSettings;
use bevy::log::LogPlugin;
use bevy::prelude::*;
use bevy::utils::tracing::{debug, trace};
use big_brain::prelude::*;
Expand Down Expand Up @@ -239,13 +239,19 @@ pub fn thirsty_scorer_system(

pub fn init_entities(mut cmd: Commands) {
// Spawn two water sources.
cmd.spawn().insert(WaterSource).insert(Position {
position: Vec2::new(10.0, 10.0),
});

cmd.spawn().insert(WaterSource).insert(Position {
position: Vec2::new(-10.0, 0.0),
});
cmd.spawn((
WaterSource,
Position {
position: Vec2::new(10.0, 10.0),
},
));

cmd.spawn((
WaterSource,
Position {
position: Vec2::new(-10.0, 0.0),
},
));

// We use the Steps struct to essentially build a "MoveAndDrink" action by composing
// the MoveToWaterSource and Drink actions.
Expand All @@ -269,23 +275,23 @@ pub fn init_entities(mut cmd: Commands) {
.picker(FirstToScore { threshold: 0.8 })
.when(Thirsty, move_and_drink);

cmd.spawn()
.insert(Thirst::new(75.0, 2.0))
.insert(Position {
cmd.spawn((
Thirst::new(75.0, 2.0),
Position {
position: Vec2::new(0.0, 0.0),
})
.insert(thinker);
},
thinker,
));
}

fn main() {
// Once all that's done, we just add our systems and off we go!
App::new()
.insert_resource(LogSettings {
.add_plugins(DefaultPlugins.set(LogPlugin {
// Use `RUST_LOG=big_brain=trace,thirst=trace cargo run --example thirst --features=trace` to see extra tracing output.
filter: "big_brain=debug,sequence=debug".to_string(),
..Default::default()
})
.add_plugins(DefaultPlugins)
..default()
}))
.add_plugin(BigBrainPlugin)
.add_startup_system(init_entities)
.add_system(thirst_system)
Expand Down
14 changes: 7 additions & 7 deletions examples/thirst.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bevy::log::LogSettings;
use bevy::log::LogPlugin;
use bevy::prelude::*;
use bevy::utils::tracing::{debug, trace};
use big_brain::prelude::*;
Expand Down Expand Up @@ -136,7 +136,8 @@ pub fn thirsty_scorer_system(
// to have AI behavior should have one *or more* Thinkers attached to it.
pub fn init_entities(mut cmd: Commands) {
// Create the entity and throw the Thirst component in there. Nothing special here.
cmd.spawn().insert(Thirst::new(75.0, 2.0)).insert(
cmd.spawn((
Thirst::new(75.0, 2.0),
Thinker::build()
.label("My Thinker")
.picker(FirstToScore { threshold: 0.8 })
Expand All @@ -149,19 +150,18 @@ pub fn init_entities(mut cmd: Commands) {
per_second: 5.0,
},
),
);
));
}

fn main() {
// Once all that's done, we just add our systems and off we go!
App::new()
.insert_resource(LogSettings {
.add_plugins(DefaultPlugins.set(LogPlugin {
// Use `RUST_LOG=big_brain=trace,thirst=trace cargo run --example
// thirst --features=trace` to see extra tracing output.
filter: "big_brain=debug,thirst=debug".to_string(),
..Default::default()
})
.add_plugins(DefaultPlugins)
..default()
}))
.add_plugin(BigBrainPlugin)
.add_startup_system(init_entities)
.add_system(thirst_system)
Expand Down
2 changes: 1 addition & 1 deletion src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ pub fn spawn_action<T: ActionBuilder + ?Sized>(
cmd: &mut Commands,
actor: Entity,
) -> Entity {
let action_ent = Action(cmd.spawn().id());
let action_ent = Action(cmd.spawn_empty().id());
let span = ActionSpan::new(action_ent.entity(), ActionBuilder::label(builder));
let _guard = span.span().enter();
debug!("New Action spawned.");
Expand Down
2 changes: 1 addition & 1 deletion src/scorers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub fn spawn_scorer<T: ScorerBuilder + ?Sized>(
cmd: &mut Commands,
actor: Entity,
) -> Entity {
let scorer_ent = cmd.spawn().id();
let scorer_ent = cmd.spawn_empty().id();
let span = ScorerSpan::new(scorer_ent, ScorerBuilder::label(builder));
let _guard = span.span().enter();
debug!("New Scorer spawned.");
Expand Down
4 changes: 2 additions & 2 deletions tests/steps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn steps() {
}

fn setup(mut cmds: Commands) {
cmds.spawn().insert(
cmds.spawn_empty().insert(
Thinker::build()
.picker(pickers::FirstToScore::new(0.5))
.when(NoFailureScore, Steps::build().step(FailureAction))
Expand Down Expand Up @@ -127,7 +127,7 @@ fn failure_action(
}
}

#[derive(Default)]
#[derive(Default, Resource)]
struct GlobalState {
failure: bool,
}
Expand Down

0 comments on commit 8ce5cd1

Please sign in to comment.