Skip to content

Commit

Permalink
feat: pickup collectable
Browse files Browse the repository at this point in the history
  • Loading branch information
mrchantey committed Nov 12, 2024
1 parent d66d06f commit 26c4cb6
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 5 deletions.
1 change: 1 addition & 0 deletions crates/beet_sim/examples/sim_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ fn agent(mut commands: Commands, stat_map: Res<StatMap>) {
Emoji::new("1F600"), //😀
Transform::from_xyz(0., 1., 0.),
MaxSpeed::default(),
Collector,
))
.with_children(|parent| {
let total_children = 4;
Expand Down
1 change: 1 addition & 0 deletions crates/beet_sim/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![feature(let_chains)]
pub mod behavior;
pub mod plugins;
#[cfg(feature = "render")]
Expand Down
29 changes: 24 additions & 5 deletions crates/beet_sim/src/stats/collectable_stat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,43 @@ impl Default for CollectableStat {
fn default() -> Self { Self { radius: 0.5 } }
}

impl CollectableStat {}


pub fn pickup_collectable(
mut commands: Commands,
stat_map: Res<StatMap>,
collectors: Query<&GlobalTransform, With<Collector>>,
query: Populated<(&GlobalTransform, &CollectableStat, &StatId, &StatValue)>,
children: Query<&Children>,
mut stats: Query<(&mut StatId, &mut StatValue)>,
collectors: Query<(Entity, &GlobalTransform), With<Collector>>,
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())
> rad_sq
{
continue;
}
todo!("pickup_collectable");
apply_stats(
&mut commands,
&stat_map,
collector_entity,
collectable_entity,
&children,
&mut stats,
);
commands.entity(collectable_entity).try_despawn_recursive();
}
}
}
49 changes: 49 additions & 0 deletions crates/beet_sim/src/stats/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<StatMap>,
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::<Vec<_>>();

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);
}
}
}

0 comments on commit 26c4cb6

Please sign in to comment.