feat: Integrate InteractionAction into Context Menu
This commit is contained in:
@@ -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<Entity, With<RootMarker>>,
|
||||
camera_query: Single<(&Camera, &GlobalTransform), With<Camera2d>>,
|
||||
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() {
|
||||
// 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| {
|
||||
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("<Interact>", 20.0, c),
|
||||
|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 => (),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user