Skip to content

Commit

Permalink
feat(bevy): Upgrade to bevy 0.15 (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
morgenthum authored Nov 30, 2024
1 parent ab0385d commit 868ebdb
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 67 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ homepage = "https://github.com/zkat/big-brain"
[workspace]

[dependencies]
bevy = { version = "0.14.0", default-features = false }
bevy = { version = "0.15.0", default-features = false }
big-brain-derive = { version = "=0.21.1", path = "./derive" }

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

[features]
Expand Down
4 changes: 2 additions & 2 deletions examples/custom_measure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub struct Pancakes(pub f32);
pub struct Waffles(pub f32);

pub fn eat_dessert(time: Res<Time>, mut pancakes: Query<(&mut Pancakes, &mut Waffles)>) {
let delta_t = time.delta_seconds();
let delta_t = time.delta_secs();

for (mut pancake, mut waffle) in pancakes.iter_mut() {
pancake.0 = (pancake.0 - delta_t).max(0.0);
Expand Down Expand Up @@ -101,7 +101,7 @@ fn eat_thing_action<
ActionState::Executing => {
debug!("You should {:?}", action_marker);

item.eat(time.delta_seconds() * 5.0);
item.eat(time.delta_secs() * 5.0);

// we should stop at some eating pancakes at some point, unfortunately
if item.get() > 80.0 {
Expand Down
87 changes: 37 additions & 50 deletions examples/farming_sim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub struct Fatigue {
/// Increases an entity's fatigue over time
pub fn fatigue_system(time: Res<Time>, mut fatigues: Query<&mut Fatigue>) {
for mut fatigue in &mut fatigues {
fatigue.level += fatigue.per_second * time.delta_seconds();
fatigue.level += fatigue.per_second * time.delta_secs();
if fatigue.level >= 100.0 {
fatigue.level = 100.0;
}
Expand Down Expand Up @@ -111,7 +111,7 @@ pub struct Sleep {
// the Sleep component's parameters.
fn sleep_action_system(
time: Res<Time>,
mut fatigues: Query<(&mut Fatigue, &Handle<StandardMaterial>)>,
mut fatigues: Query<(&mut Fatigue, &MeshMaterial3d<StandardMaterial>)>,
// Resource used to modify the appearance of the farmer.
mut materials: ResMut<Assets<StandardMaterial>>,
// We execute actions by querying for their associated Action Component
Expand All @@ -133,7 +133,7 @@ fn sleep_action_system(
}
ActionState::Executing => {
trace!("Sleeping...");
fatigue.level -= sleep.per_second * time.delta_seconds();
fatigue.level -= sleep.per_second * time.delta_secs();
materials.get_mut(material).unwrap().base_color = SLEEP_COLOR;

if fatigue.level <= sleep.until {
Expand Down Expand Up @@ -208,7 +208,7 @@ pub struct Farm {
// Farm component's parameters and changes the entity's appearance to indicate the farming action.
fn farm_action_system(
time: Res<Time>,
mut actors: Query<(&mut Inventory, &Handle<StandardMaterial>)>,
mut actors: Query<(&mut Inventory, &MeshMaterial3d<StandardMaterial>)>,
// Resource used to modify the appearance of the farmer.
mut materials: ResMut<Assets<StandardMaterial>>,
// Query to manage the state of the farming action.
Expand All @@ -225,7 +225,7 @@ fn farm_action_system(
}
ActionState::Executing => {
trace!("Farming...");
inventory.items += farm.per_second * time.delta_seconds();
inventory.items += farm.per_second * time.delta_secs();
materials.get_mut(material).unwrap().base_color = FARM_COLOR;

if inventory.items >= MAX_INVENTORY_ITEMS {
Expand Down Expand Up @@ -398,7 +398,7 @@ pub fn move_to_nearest_system<T: Component + std::fmt::Debug + Clone>(
if distance > MAX_DISTANCE {
trace!("Stepping closer.");

let step_size = time.delta_seconds() * move_to.speed;
let step_size = time.delta_secs() * move_to.speed;
let step = delta.normalize() * step_size.min(distance);

// We only care about moving in the XZ plane.
Expand Down Expand Up @@ -455,15 +455,15 @@ fn update_ui(
) {
for (inventory, fatigue) in &mut actor_query.iter() {
for mut text in &mut money_query {
text.sections[0].value = format!("Money: {}", inventory.money);
text.0 = format!("Money: {}", inventory.money);
}

for mut text in &mut fatigue_query {
text.sections[0].value = format!("Fatigue: {}", fatigue.level as u32);
text.0 = format!("Fatigue: {}", fatigue.level as u32);
}

for mut text in &mut inventory_query {
text.sections[0].value = format!("Inventory: {}", inventory.items as u32);
text.0 = format!("Inventory: {}", inventory.items as u32);
}
}
}
Expand All @@ -475,11 +475,10 @@ fn init_entities(
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(6.0, 6.0, 4.0)
.looking_at(Vec3::new(0.0, -1.0, 0.0), Vec3::Y),
..default()
});
commands.spawn((
Camera3d::default(),
Transform::from_xyz(6.0, 6.0, 4.0).looking_at(Vec3::new(0.0, -1.0, 0.0), Vec3::Y),
));

commands.insert_resource(AmbientLight {
color: Color::WHITE,
Expand All @@ -488,26 +487,20 @@ fn init_entities(

commands.spawn((
Name::new("Light"),
SpotLightBundle {
spot_light: SpotLight {
shadows_enabled: true,
intensity: 500_000.0,
range: 100.0,
..default()
},
transform: Transform::from_xyz(2.0, 10.0, 0.0).looking_at(Vec3::ZERO, Vec3::Y),
SpotLight {
shadows_enabled: true,
intensity: 500_000.0,
range: 100.0,
..default()
},
Transform::from_xyz(2.0, 10.0, 0.0).looking_at(Vec3::ZERO, Vec3::Y),
));

// Loading our scene here. Note we'll still need to add components to different parts
// of the gltf in order to query their positions. We do this through an observer further below.
commands.spawn((
Name::new("Town"),
SceneBundle {
scene: asset_server.load("models/town.glb#Scene0"),
..default()
},
SceneRoot(asset_server.load("models/town.glb#Scene0")),
));

// We'll use `Steps` to execute a sequence of actions.
Expand Down Expand Up @@ -538,16 +531,13 @@ fn init_entities(

commands.spawn((
Name::new("Farmer"),
PbrBundle {
mesh: meshes.add(Mesh::from(Capsule3d {
half_length: 0.15,
radius: 0.1,
..default()
})),
material: materials.add(DEFAULT_COLOR),
transform: Transform::from_xyz(0.0, 0.5, 0.0),
Mesh3d(meshes.add(Mesh::from(Capsule3d {
half_length: 0.15,
radius: 0.1,
..default()
},
}))),
MeshMaterial3d(materials.add(DEFAULT_COLOR)),
Transform::from_xyz(0.0, 0.5, 0.0),
Fatigue {
is_sleeping: false,
per_second: 4.0,
Expand All @@ -566,29 +556,26 @@ fn init_entities(
.when(SellNeedScorer, move_and_sell),
));

let style = TextStyle {
let font = TextFont {
font_size: 40.0,
..default()
};

// Our scoreboard.
commands
.spawn(NodeBundle {
style: Style {
width: Val::Percent(100.0),
height: Val::Percent(100.0),
flex_direction: FlexDirection::Column,
justify_content: JustifyContent::End,
align_items: AlignItems::FlexStart,
padding: UiRect::all(Val::Px(20.0)),
..default()
},
.spawn(Node {
width: Val::Percent(100.0),
height: Val::Percent(100.0),
flex_direction: FlexDirection::Column,
justify_content: JustifyContent::End,
align_items: AlignItems::FlexStart,
padding: UiRect::all(Val::Px(20.0)),
..default()
})
.with_children(|builder| {
builder.spawn((TextBundle::from_section("", style.clone()), MoneyText));
builder.spawn((TextBundle::from_section("", style.clone()), FatigueText));
builder.spawn((TextBundle::from_section("", style.clone()), InventoryText));
builder.spawn((Text::new(""), font.clone(), MoneyText));
builder.spawn((Text::new(""), font.clone(), FatigueText));
builder.spawn((Text::new(""), font.clone(), InventoryText));
});
}

Expand Down Expand Up @@ -640,7 +627,7 @@ fn main() {
.add_event::<SceneLoaded>()
.add_systems(Update, check_scene_loaded)
// This observer will attach components to entities in the scene based on their names.
.observe(
.add_observer(
|trigger: Trigger<SceneLoaded>,
query: Query<(Entity, &Name)>,
mut commands: Commands| {
Expand Down
6 changes: 3 additions & 3 deletions examples/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl Thirst {
/// Just a plain old Bevy system, big-brain is not involved yet.
pub fn thirst_system(time: Res<Time>, mut thirsts: Query<&mut Thirst>) {
for mut thirst in &mut thirsts {
thirst.thirst += thirst.per_second * time.delta_seconds();
thirst.thirst += thirst.per_second * time.delta_secs();

// Thirst is capped at 100.0
if thirst.thirst >= 100.0 {
Expand Down Expand Up @@ -104,7 +104,7 @@ fn move_to_water_source_action_system(
trace!("Stepping closer.");

// How far can we travel during this frame?
let step_size = time.delta_seconds() * move_to.speed;
let step_size = time.delta_secs() * move_to.speed;
// Travel towards the water-source position, but make sure to not overstep it.
let step = delta.normalize() * step_size.min(distance);

Expand Down Expand Up @@ -191,7 +191,7 @@ fn drink_action_system(

// Start reducing the thirst. Alternatively, you could send out some kind of
// DrinkFromSource event that indirectly decreases thirst.
thirst.thirst -= drink.per_second * time.delta_seconds();
thirst.thirst -= drink.per_second * time.delta_secs();

// Once we hit 0 thirst, we stop drinking and report success.
if thirst.thirst <= 0.0 {
Expand Down
6 changes: 3 additions & 3 deletions src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ impl ActionBuilder for StepsBuilder {
steps: self.steps.clone(),
steps_labels: self.steps_labels.clone(),
})
.push_children(&[child_action]);
.add_children(&[child_action]);
}
}
}
Expand Down Expand Up @@ -315,7 +315,7 @@ pub fn steps_system(
let step_ent = spawn_action(step_builder.as_ref(), &mut cmd, *actor);
#[cfg(feature = "trace")]
trace!("Spawned next step: {:?}", step_ent);
cmd.entity(seq_ent).push_children(&[step_ent]);
cmd.entity(seq_ent).add_children(&[step_ent]);
steps_action.active_ent = Action(step_ent);
}
}
Expand Down Expand Up @@ -396,7 +396,7 @@ impl ActionBuilder for ConcurrentlyBuilder {
.collect();
cmd.entity(action)
.insert(Name::new("Concurrent Action"))
.push_children(&children[..])
.add_children(&children[..])
.insert(Concurrently {
actions: children.into_iter().map(Action).collect(),
action_labels: self.action_labels.clone(),
Expand Down
2 changes: 1 addition & 1 deletion src/choices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl ChoiceBuilder {

pub fn build(&self, cmd: &mut Commands, actor: Entity, parent: Entity) -> Choice {
let scorer_ent = scorers::spawn_scorer(&*self.when, cmd, actor);
cmd.entity(parent).push_children(&[scorer_ent]);
cmd.entity(parent).add_children(&[scorer_ent]);
Choice {
scorer: Scorer(scorer_ent),
action_label: self.then.label().map(|s| s.into()),
Expand Down
12 changes: 6 additions & 6 deletions src/scorers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ impl ScorerBuilder for AllOrNothingBuilder {
.collect();
cmd.entity(scorer)
.insert(Score::default())
.push_children(&scorers[..])
.add_children(&scorers[..])
.insert(Name::new("Scorer"))
.insert(AllOrNothing {
threshold: self.threshold,
Expand Down Expand Up @@ -406,7 +406,7 @@ impl ScorerBuilder for SumOfScorersBuilder {
.map(|scorer| spawn_scorer(&**scorer, cmd, actor))
.collect();
cmd.entity(scorer)
.push_children(&scorers[..])
.add_children(&scorers[..])
.insert(SumOfScorers {
threshold: self.threshold,
scorers: scorers.into_iter().map(Scorer).collect(),
Expand Down Expand Up @@ -565,7 +565,7 @@ impl ScorerBuilder for ProductOfScorersBuilder {
.map(|scorer| spawn_scorer(&**scorer, cmd, actor))
.collect();
cmd.entity(scorer)
.push_children(&scorers[..])
.add_children(&scorers[..])
.insert(ProductOfScorers {
threshold: self.threshold,
use_compensation: self.use_compensation,
Expand Down Expand Up @@ -696,7 +696,7 @@ impl ScorerBuilder for WinningScorerBuilder {
.map(|scorer| spawn_scorer(&**scorer, cmd, actor))
.collect();
cmd.entity(scorer)
.push_children(&scorers[..])
.add_children(&scorers[..])
.insert(WinningScorer {
threshold: self.threshold,
scorers: scorers.into_iter().map(Scorer).collect(),
Expand Down Expand Up @@ -805,7 +805,7 @@ impl ScorerBuilder for EvaluatingScorerBuilder {
let inner_scorer = spawn_scorer(&*self.scorer, cmd, actor);
let scorers = [inner_scorer];
cmd.entity(scorer)
.push_children(&scorers[..])
.add_children(&scorers[..])
.insert(EvaluatingScorer {
evaluator: self.evaluator.clone(),
scorer: Scorer(inner_scorer),
Expand Down Expand Up @@ -978,7 +978,7 @@ impl ScorerBuilder for MeasuredScorerBuilder {
.map(|(scorer, _)| spawn_scorer(&**scorer, cmd, actor))
.collect();
cmd.entity(scorer)
.push_children(&scorers[..])
.add_children(&scorers[..])
.insert(MeasuredScorer {
threshold: self.threshold,
measure: self.measure.clone(),
Expand Down

0 comments on commit 868ebdb

Please sign in to comment.