feat: Integrate InteractionAction into Context Menu

This commit is contained in:
demenik
2025-12-02 12:54:28 +01:00
parent 1b169b00de
commit 58e8dda94a

View File

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