Files
pomomon-garden/src/features/shop/mod.rs

50 lines
1.5 KiB
Rust

use crate::prelude::*;
use components::*;
use ui::open_shop;
pub mod components;
pub mod ui;
pub struct ShopPlugin;
impl Plugin for ShopPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Update, buttons.run_if(in_state(AppState::GameScreen)));
}
}
fn buttons(
mut commands: Commands,
mut interaction_query: Query<(&Interaction, &ButtonType), (Changed<Interaction>, With<Button>)>,
root_query: Query<(Entity, &RootMarker)>,
game_config: Res<GameConfig>,
asset_server: Res<AssetServer>,
mut inventory: ResMut<Inventory>,
mut items: Query<&mut ItemStack>,
grid: Res<Grid>,
tile_query: Query<&TileState>,
) {
for (interaction, button_type) in &mut interaction_query {
match *interaction {
Interaction::Pressed => match button_type {
ButtonType::ShopOpen => {
open_shop(&mut commands, &game_config, &asset_server, &grid, &tile_query);
}
ButtonType::ShopBuyItem(offer) => {
if offer.buy(&mut inventory, &mut commands, &mut items) {
// Item bought, exit the menu
for (entity, root) in root_query.iter() {
match *root {
RootMarker::Shop => commands.entity(entity).despawn(),
}
}
} else {
// Error (e.g. not enough berries)
}
}
},
_ => {}
}
}
}