2023-09-28 12:10:45 +00:00
|
|
|
use super::Player;
|
2023-10-13 10:53:26 +00:00
|
|
|
use bevy::prelude::*;
|
2023-11-11 21:58:00 +00:00
|
|
|
use bevy_gltf_blueprints::GltfBlueprintsSet;
|
2023-09-28 12:10:45 +00:00
|
|
|
|
2023-10-13 10:53:26 +00:00
|
|
|
#[derive(Component, Reflect, Default, Debug)]
|
2023-09-28 12:10:45 +00:00
|
|
|
#[reflect(Component)]
|
|
|
|
pub struct Pickable;
|
|
|
|
|
2023-10-13 10:53:26 +00:00
|
|
|
// very simple, crude picking (as in picking up objects) implementation
|
2023-09-28 12:10:45 +00:00
|
|
|
|
|
|
|
pub fn picking(
|
|
|
|
players: Query<&GlobalTransform, With<Player>>,
|
|
|
|
pickables: Query<(Entity, &GlobalTransform), With<Pickable>>,
|
2023-10-13 10:53:26 +00:00
|
|
|
mut commands: Commands,
|
|
|
|
) {
|
|
|
|
for player_transforms in players.iter() {
|
|
|
|
for (pickable, pickable_transforms) in pickables.iter() {
|
|
|
|
let distance = player_transforms
|
|
|
|
.translation()
|
|
|
|
.distance(pickable_transforms.translation());
|
2023-09-28 12:10:45 +00:00
|
|
|
if distance < 2.5 {
|
|
|
|
commands.entity(pickable).despawn_recursive();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct PickingPlugin;
|
|
|
|
impl Plugin for PickingPlugin {
|
2023-10-13 10:53:26 +00:00
|
|
|
fn build(&self, app: &mut App) {
|
2023-11-11 21:58:00 +00:00
|
|
|
app.register_type::<Pickable>()
|
|
|
|
.add_systems(Update, (picking.after(GltfBlueprintsSet::AfterSpawn),));
|
2023-10-13 10:53:26 +00:00
|
|
|
}
|
2023-09-28 12:10:45 +00:00
|
|
|
}
|