From a0780eb1d0344c97094bac7e894cb4d59a0b3167 Mon Sep 17 00:00:00 2001 From: Peter Hayman Date: Sat, 9 Nov 2024 17:05:38 +1100 Subject: [PATCH] wip: collectables --- crates/beet_sim/examples/sim_test.rs | 15 ++++---- .../beet_sim/src/plugins/beet_sim_plugin.rs | 2 +- crates/beet_sim/src/stats/collectable_stat.rs | 37 +++++++++++++++++-- crates/beet_sim/src/stats/collector.rs | 8 ++++ crates/beet_sim/src/stats/mod.rs | 3 ++ 5 files changed, 54 insertions(+), 11 deletions(-) create mode 100644 crates/beet_sim/src/stats/collector.rs diff --git a/crates/beet_sim/examples/sim_test.rs b/crates/beet_sim/examples/sim_test.rs index 21465588..ab699729 100644 --- a/crates/beet_sim/examples/sim_test.rs +++ b/crates/beet_sim/examples/sim_test.rs @@ -131,11 +131,12 @@ fn kids_crying(mut commands: Commands, stat_map: Res) { Emoji::new("1F476"), //👶 Transform::from_xyz(0., -1., 0.), MaxSpeed::default(), + CollectableStat::default(), )) .with_children(|parent| { let agent = parent.parent_entity(); parent.spawn(( - Name::new("Behavior"), + Name::new("Seek Agent"), Emoji::new("1F5FA"), //🗺️ orbital_child(0, 2), TargetEntity(agent), @@ -149,7 +150,7 @@ fn kids_crying(mut commands: Commands, stat_map: Res) { orbital_child(1, 2), stat_map.get_id_by_name(STRESS).unwrap(), StatValue::new(0.1), - CollectableStat::default(), + StatProvider::default(), )); }); } @@ -166,14 +167,14 @@ fn alcohol(mut commands: Commands, stat_map: Res) { orbital_child(0, 2), stat_map.get_id_by_name(STRESS).unwrap(), StatValue::new(-0.1), - CollectableStat::default(), + StatProvider::default(), )) .with_child(( Name::new(SELF_CONTROL), orbital_child(1, 2), stat_map.get_id_by_name(SELF_CONTROL).unwrap(), StatValue::new(-0.1), - CollectableStat::default(), + StatProvider::default(), )); } @@ -182,7 +183,7 @@ fn short_stroll(mut commands: Commands, stat_map: Res) { .spawn(( Name::new("Short Stroll"), Emoji::new("1F6B6"), //🚶 - CollectableStat::default(), + ZoneStat::default(), Transform::from_xyz(3., -1.5, 0.), )) .with_child(( @@ -190,13 +191,13 @@ fn short_stroll(mut commands: Commands, stat_map: Res) { orbital_child(0, 2), stat_map.get_id_by_name(STRESS).unwrap(), StatValue::new(-0.1), - ZoneStat::default(), + StatProvider::default(), )) .with_child(( Name::new(SELF_CONTROL), orbital_child(1, 2), stat_map.get_id_by_name(SELF_CONTROL).unwrap(), StatValue::new(0.1), - ZoneStat::default(), + StatProvider::default(), )); } diff --git a/crates/beet_sim/src/plugins/beet_sim_plugin.rs b/crates/beet_sim/src/plugins/beet_sim_plugin.rs index 25f780f3..bf8705b5 100644 --- a/crates/beet_sim/src/plugins/beet_sim_plugin.rs +++ b/crates/beet_sim/src/plugins/beet_sim_plugin.rs @@ -16,6 +16,6 @@ impl Plugin for BeetSimPlugin { FindStatSteerTarget, )>::default(), )) - .add_systems(Update, render_valency); + .add_systems(Update, (render_valency, pickup_collectable)); } } diff --git a/crates/beet_sim/src/stats/collectable_stat.rs b/crates/beet_sim/src/stats/collectable_stat.rs index 70b0e3fa..3db23fac 100644 --- a/crates/beet_sim/src/stats/collectable_stat.rs +++ b/crates/beet_sim/src/stats/collectable_stat.rs @@ -1,11 +1,42 @@ +use crate::prelude::*; use bevy::prelude::*; -//// Superset of [`CollectableStat`], [`ZoneStat`] etc +//// Added to children of Collectables and Zones for consideration in [`FindStatSteerTarget`] #[derive(Default, Component, Reflect)] #[reflect(Default, Component)] pub struct StatProvider; -#[derive(Default, Component, Reflect)] + +/// Add this as a child of an entity to make it collectable. +/// This will be able to be picked up. +#[derive(Component, Reflect)] #[reflect(Default, Component)] #[require(StatProvider)] -pub struct CollectableStat {} +pub struct CollectableStat { + pub radius: f32, +} + +impl Default for CollectableStat { + fn default() -> Self { Self { radius: 0.5 } } +} + + +pub fn pickup_collectable( + mut commands: Commands, + stat_map: Res, + collectors: Query<&GlobalTransform, With>, + query: Populated<(&GlobalTransform, &CollectableStat, &StatId, &StatValue)>, +) { + for (transform, collectable_stat, stat_id, stat_value) in query.iter() { + let rad_sq = collectable_stat.radius * collectable_stat.radius; + for collector_transform in collectors.iter() { + if transform + .translation() + .distance_squared(collector_transform.translation()) + > rad_sq + { + continue; + } + } + } +} diff --git a/crates/beet_sim/src/stats/collector.rs b/crates/beet_sim/src/stats/collector.rs new file mode 100644 index 00000000..c02f4c24 --- /dev/null +++ b/crates/beet_sim/src/stats/collector.rs @@ -0,0 +1,8 @@ +use crate::prelude::*; +use bevy::prelude::*; + + +/// Marker to indicate that an entity is a collector. +#[derive(Default, Component, Reflect)] +#[reflect(Default, Component)] +pub struct Collector; diff --git a/crates/beet_sim/src/stats/mod.rs b/crates/beet_sim/src/stats/mod.rs index 1783deee..14dddd66 100644 --- a/crates/beet_sim/src/stats/mod.rs +++ b/crates/beet_sim/src/stats/mod.rs @@ -1,6 +1,9 @@ pub mod collectable_stat; #[allow(unused_imports)] pub use self::collectable_stat::*; +pub mod collector; +#[allow(unused_imports)] +pub use self::collector::*; pub mod find_stat_steer_target; #[allow(unused_imports)] pub use self::find_stat_steer_target::*;