diff --git a/src/features/pom/ui/context_menu.rs b/src/features/pom/ui/context_menu.rs index 1253f0b..4d4eb4e 100644 --- a/src/features/pom/ui/context_menu.rs +++ b/src/features/pom/ui/context_menu.rs @@ -1,3 +1,4 @@ +use crate::features::pom::actions::InteractionAction; use crate::features::ui::utils::ui_blocks; use crate::prelude::*; use bevy::window::PrimaryWindow; @@ -9,7 +10,11 @@ pub enum RootMarker { #[derive(Component)] pub enum ButtonType { - Interact { x: u32, y: u32 }, + Interact { + x: u32, + y: u32, + action: InteractionAction, + }, Cancel, } @@ -19,6 +24,11 @@ pub fn spawn_context_menu( root_query: Query>, camera_query: Single<(&Camera, &GlobalTransform), With>, config: Res, + grid: Res, + tile_query: Query<&TileState>, + inventory: Res, + item_query: Query<&ItemStack>, + game_config: Res, ) { for message in tile_click_messages.read() { // Despawn existing menu @@ -37,6 +47,15 @@ pub fn spawn_context_menu( let (camera, camera_transform) = *camera_query; if let Ok(screen_pos) = camera.world_to_viewport(camera_transform, world_pos) { + let Ok(tile_entity) = grid.get_tile((message.x, message.y)) else { + return; + }; + let Ok(tile_state) = tile_query.get(tile_entity) else { + return; + }; + dbg!(tile_state); + let options = InteractionAction::list_options(tile_state, &inventory, item_query); + commands .spawn(( Node { @@ -53,18 +72,21 @@ pub fn spawn_context_menu( GlobalTransform::default(), )) .with_children(|parent| { - parent.spawn(button( - ButtonType::Interact { - x: message.x, - y: message.y, - }, - ButtonVariant::Primary, - Node { - padding: UiRect::all(px(5)), - ..default() - }, - |c| text("", 20.0, c), - )); + for option in options { + parent.spawn(button( + ButtonType::Interact { + x: message.x, + y: message.y, + action: option.clone(), + }, + ButtonVariant::Primary, + Node { + padding: UiRect::all(px(5)), + ..default() + }, + |c| text(option.clone().get_name(&game_config), 20.0, c), // TODO: add sprite + )); + } parent.spawn(button( ButtonType::Cancel, @@ -109,8 +131,12 @@ pub fn buttons( for (interaction, button_type) in button_query.iter_mut() { if *interaction == Interaction::Pressed { match button_type { - ButtonType::Interact { x, y } => { - interact_messages.write(InteractStartMessage { x: *x, y: *y }); + ButtonType::Interact { x, y, action } => { + interact_messages.write(InteractStartMessage { + x: *x, + y: *y, + action: action.clone(), + }); } ButtonType::Cancel => (), }