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

78 lines
1.9 KiB
Rust

use crate::prelude::*;
/// Markers for shop UI.
#[derive(Component)]
pub enum RootMarker {
Shop,
}
/// Buttons in the shop.
#[derive(Component)]
pub enum ButtonType {
ShopOpen,
ShopBuyItem(ShopOffer),
}
/// An item available for purchase.
#[derive(Clone)]
pub struct ShopOffer {
pub item: ItemStack,
pub cost: u32,
}
impl ShopOffer {
/// Generates a list of all current offers.
pub fn list_all(game_config: &GameConfig, tile_count: u32) -> Vec<ShopOffer> {
let mut offers = Vec::new();
for seed in &game_config.berry_seeds {
offers.push(ShopOffer {
item: ItemStack {
item_type: ItemType::BerrySeed {
name: seed.name.clone(),
},
amount: 1,
},
cost: seed.cost,
});
}
let mut shovel_cost = game_config.shovel_base_price as f32;
for _ in 0..=tile_count {
shovel_cost = shovel_cost + (game_config.shovel_rate * shovel_cost);
shovel_cost = shovel_cost.ceil();
}
offers.push(ShopOffer {
item: ItemStack {
item_type: ItemType::Shovel,
amount: 1,
},
cost: shovel_cost as u32,
});
offers
}
/// Attempts to purchase the offer.
pub fn buy(
&self,
inventory: &mut Inventory,
commands: &mut Commands,
items: &mut Query<&mut ItemStack>,
) -> bool {
// Try to remove cost (berries)
if inventory.update_item_stack(commands, items, ItemType::Berry, -(self.cost as i32)) {
inventory.update_item_stack(
commands,
items,
self.item.item_type.clone(),
self.item.amount as i32,
);
true
} else {
false // Not enough berries
}
}
}