Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to bevy 0.14.0 #101

Merged
merged 4 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@ homepage = "https://github.com/zkat/big-brain"
[workspace]

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

[dev-dependencies]
bevy = { version = "0.13.2", default-features = true }
bevy = { version = "0.14.0", default-features = true }
rand = { version = "0.8.5", features = ["small_rng"] }
bevy-scene-hook = "10.0.0"

[features]
trace = []
103 changes: 78 additions & 25 deletions examples/farming_sim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
//! - When not tired, find the farm field and harvest items over time
//! - When inventory is full, find the market and sell items for money

use bevy::{log::LogPlugin, prelude::*};
use bevy_scene_hook::{HookPlugin, HookedSceneBundle, SceneHook};
use bevy::scene::SceneInstance;
use bevy::{color::palettes::css, log::LogPlugin, prelude::*};
use big_brain::prelude::*;
use big_brain_derive::ActionBuilder;

const DEFAULT_COLOR: Color = Color::BLACK;
const SLEEP_COLOR: Color = Color::RED;
const FARM_COLOR: Color = Color::BLUE;
const DEFAULT_COLOR: Color = Color::Srgba(css::BLACK);
const SLEEP_COLOR: Color = Color::Srgba(css::RED);
const FARM_COLOR: Color = Color::Srgba(css::BLUE);
const MAX_DISTANCE: f32 = 0.1;
const MAX_INVENTORY_ITEMS: f32 = 20.0;
const WORK_NEED_SCORE: f32 = 0.6;
Expand Down Expand Up @@ -386,8 +386,10 @@ pub fn move_to_nearest_system<T: Component + std::fmt::Debug + Clone>(
let delta_b = *b - actor_transform.translation;
delta_a.length().partial_cmp(&delta_b.length()).unwrap()
})
.unwrap()
.1;
.and_then(|t| Some(t.1));
let Some(goal_transform) = goal_transform else {
continue;
};
let delta = goal_transform.translation - actor_transform.translation;
let distance = delta.xz().length();

Expand Down Expand Up @@ -498,24 +500,13 @@ fn init_entities(
},
));

// We use a HookedSceneBundle to attach a SceneHook to the entity. This hook
// will be called when the entity is spawned, and will allow us to insert
// additional components into the spawned entities.
// 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"),
HookedSceneBundle {
scene: SceneBundle {
scene: asset_server.load("models/town.glb#Scene0"),
..default()
},
hook: SceneHook::new(|entity, cmds| {
match entity.get::<Name>().map(|t| t.as_str()) {
Some("Farm_Marker") => cmds.insert(Field),
Some("Market_Marker") => cmds.insert(Market),
Some("House_Marker") => cmds.insert(House),
_ => cmds,
};
}),
SceneBundle {
scene: asset_server.load("models/town.glb#Scene0"),
..default()
},
));

Expand Down Expand Up @@ -601,16 +592,78 @@ fn init_entities(
});
}

// ================================================================================
// Scene Loading 🏗️
// ================================================================================

// Define a custom event for our scene loading
#[derive(Event)]
struct SceneLoaded {
/// The entities in this scene
entities: Vec<Entity>,
}

// Define a marker component to indicate what entities we've already processed
#[derive(Component)]
struct SceneProcessed;

// System to check if a scene has finished loading
fn check_scene_loaded(
mut commands: Commands,
query: Query<(Entity, &SceneInstance), Without<SceneProcessed>>,
scene_spawner: Res<SceneSpawner>,
) {
for (entity, instance) in query.iter() {
if scene_spawner.instance_is_ready(**instance) {
commands.entity(entity).insert(SceneProcessed);

let entities = scene_spawner
.iter_instance_entities(**instance)
.chain(std::iter::once(entity));

commands.trigger(SceneLoaded {
entities: entities.collect(),
});
}
}
}

fn main() {
App::new()
.add_plugins(DefaultPlugins.set(LogPlugin {
level: bevy::log::Level::WARN,
// Use `RUST_LOG=big_brain=trace,farming_sim=trace cargo run --example
// farming_sim --features=trace` to see extra tracing output.
filter: "big_brain=debug,farming_sim=debug".to_string(),
update_subscriber: None,
custom_layer: |_| None,
}))
.add_plugins(HookPlugin)
.add_event::<SceneLoaded>()
.add_systems(Update, check_scene_loaded)
// This observer will attach components to entities in the scene based on their names.
.observe(
|trigger: Trigger<SceneLoaded>,
query: Query<(Entity, &Name)>,
mut commands: Commands| {
for entity in trigger.event().entities.iter() {
if let Ok((entity, name)) = query.get(*entity) {
let mut entity_commands = commands.entity(entity);

match name.as_str() {
"Farm_Marker" => {
entity_commands.insert(Field);
}
"Market_Marker" => {
entity_commands.insert(Market);
}
"House_Marker" => {
entity_commands.insert(House);
}
_ => (),
}
}
}
},
)
.add_plugins(BigBrainPlugin::new(PreUpdate))
.add_systems(Startup, init_entities)
.add_systems(Update, (fatigue_system, update_ui))
Expand Down
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,10 @@ pub mod prelude {
};
}

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

/// Core [`Plugin`] for Big Brain behavior. Required for any of the
/// [`Thinker`](thinker::Thinker)-related magic to work.
Expand Down
2 changes: 1 addition & 1 deletion tests/steps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ fn exit_action(
*state = ActionState::Executing;
}
if *state == ActionState::Executing {
app_exit_events.send(AppExit);
app_exit_events.send(AppExit::Success);
}
}
}
Expand Down
Loading