From 26c4cb62ba498cc449d8be4132f8d8d2136a55b3 Mon Sep 17 00:00:00 2001 From: Peter Hayman Date: Tue, 12 Nov 2024 15:15:27 +1100 Subject: [PATCH] feat: pickup collectable --- crates/beet_sim/examples/sim_test.rs | 1 + crates/beet_sim/src/lib.rs | 1 + crates/beet_sim/src/stats/collectable_stat.rs | 29 +++++++++-- crates/beet_sim/src/stats/collector.rs | 49 +++++++++++++++++++ 4 files changed, 75 insertions(+), 5 deletions(-) diff --git a/crates/beet_sim/examples/sim_test.rs b/crates/beet_sim/examples/sim_test.rs index ab699729..23ae130c 100644 --- a/crates/beet_sim/examples/sim_test.rs +++ b/crates/beet_sim/examples/sim_test.rs @@ -57,6 +57,7 @@ fn agent(mut commands: Commands, stat_map: Res) { Emoji::new("1F600"), //😀 Transform::from_xyz(0., 1., 0.), MaxSpeed::default(), + Collector, )) .with_children(|parent| { let total_children = 4; diff --git a/crates/beet_sim/src/lib.rs b/crates/beet_sim/src/lib.rs index 774ed60f..29ae4620 100644 --- a/crates/beet_sim/src/lib.rs +++ b/crates/beet_sim/src/lib.rs @@ -1,3 +1,4 @@ +#![feature(let_chains)] pub mod behavior; pub mod plugins; #[cfg(feature = "render")] diff --git a/crates/beet_sim/src/stats/collectable_stat.rs b/crates/beet_sim/src/stats/collectable_stat.rs index 4fd7d298..6e04836c 100644 --- a/crates/beet_sim/src/stats/collectable_stat.rs +++ b/crates/beet_sim/src/stats/collectable_stat.rs @@ -20,16 +20,27 @@ impl Default for CollectableStat { fn default() -> Self { Self { radius: 0.5 } } } +impl CollectableStat {} + pub fn pickup_collectable( mut commands: Commands, stat_map: Res, - collectors: Query<&GlobalTransform, With>, - query: Populated<(&GlobalTransform, &CollectableStat, &StatId, &StatValue)>, + children: Query<&Children>, + mut stats: Query<(&mut StatId, &mut StatValue)>, + collectors: Query<(Entity, &GlobalTransform), With>, + query: Populated<(Entity, &GlobalTransform, &CollectableStat)>, ) { - for (transform, collectable_stat, stat_id, stat_value) in query.iter() { + for ( + collectable_entity, + 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() { + for (collector_entity, collector_transform) in collectors.iter() { if transform .translation() .distance_squared(collector_transform.translation()) @@ -37,7 +48,15 @@ pub fn pickup_collectable( { continue; } - todo!("pickup_collectable"); + apply_stats( + &mut commands, + &stat_map, + collector_entity, + collectable_entity, + &children, + &mut stats, + ); + commands.entity(collectable_entity).try_despawn_recursive(); } } } diff --git a/crates/beet_sim/src/stats/collector.rs b/crates/beet_sim/src/stats/collector.rs index c02f4c24..d8d648cf 100644 --- a/crates/beet_sim/src/stats/collector.rs +++ b/crates/beet_sim/src/stats/collector.rs @@ -6,3 +6,52 @@ use bevy::prelude::*; #[derive(Default, Component, Reflect)] #[reflect(Default, Component)] pub struct Collector; + + + +impl Collector { + pub fn apply() {} +} + + + + +pub fn apply_stats( + commands: &mut Commands, + stat_map: &Res, + collector_entity: Entity, + collectable_entity: Entity, + children: &Query<&Children>, + stats: &mut Query<(&mut StatId, &mut StatValue)>, +) { + let stats_to_apply = children + .iter_descendants(collectable_entity) + .filter_map(|child| { + stats.get(child).ok().map(|(id, value)| (*id, *value)) + }) + .collect::>(); + + for (stat_id, stat_value) in stats_to_apply { + if let Ok((collector_stat_id, mut collector_stat_value)) = + stats.get_mut(collector_entity) + && stat_id == *collector_stat_id + { + **collector_stat_value += *stat_value; + } else { + let stat_entry = stat_map + .get(&stat_id) + .expect(format!("StatId not found: {:?}", stat_id).as_str()); + + + let new_stat = commands + .spawn(( + Name::new(stat_entry.name.clone()), + stat_id.clone(), + stat_value.clone(), + orbital_child(0, 0), //TODO reposition all children + )) + .id(); + commands.entity(collector_entity).add_child(new_stat); + } + } +}