From a70e59fdf8f428c3319ad3aa26dac3adee9c82b6 Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Fri, 2 Feb 2024 11:35:18 -0600 Subject: [PATCH] Can drop statues --- src/player.rs | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/src/player.rs b/src/player.rs index f60fbb9..9ec7c06 100644 --- a/src/player.rs +++ b/src/player.rs @@ -44,25 +44,24 @@ fn controls( mut commands: Commands, mut query: Query<(&mut TextureAtlasSprite, &mut Heading, Entity), With>, input: Res>, + mouse_input: Res>, assets: Res, q_windows: Query<&Window, With>, + q_camera: Query<(&Camera, &GlobalTransform), With>, ) { let (mut sprite, mut heading, player_entity) = query.single_mut(); + let (camera, camera_transform) = q_camera.single(); **heading = Vec2::ZERO; - for key in input.get_just_pressed() { - match key { - KeyCode::Space => { - let child = commands - .spawn(AudioSourceBundle { - source: assets.sounds.meow.clone(), - settings: PlaybackSettings::DESPAWN.with_spatial(false), - }) - .id(); - commands.entity(player_entity).push_children(&[child]); - } - KeyCode::Key1 => { - if let Some(position) = q_windows.single().cursor_position() { - let spos = position.extend(0.); + for button in mouse_input.get_just_pressed() { + match button { + MouseButton::Left => { + if let Some(world_position) = q_windows + .single() + .cursor_position() + .and_then(|cursor| camera.viewport_to_world(camera_transform, cursor)) + .map(|ray| ray.origin.truncate()) + { + let spos = world_position.extend(0.); commands.spawn(( Statue, SpriteSheetBundle { @@ -77,6 +76,20 @@ fn controls( _ => {} } } + for key in input.get_just_pressed() { + match key { + KeyCode::Space => { + let child = commands + .spawn(AudioSourceBundle { + source: assets.sounds.meow.clone(), + settings: PlaybackSettings::DESPAWN.with_spatial(false), + }) + .id(); + commands.entity(player_entity).push_children(&[child]); + } + _ => {} + } + } for key in input.get_pressed() { match key { KeyCode::A | KeyCode::Left => {