docs: add documentation for inventory and shop systems (#65)

This commit is contained in:
demenik
2025-12-10 18:13:22 +01:00
parent e5713fdb94
commit 392b93d47b
8 changed files with 22 additions and 0 deletions

View File

@@ -1,6 +1,7 @@
use crate::features::config::components::BerrySeedConfig; use crate::features::config::components::BerrySeedConfig;
use crate::prelude::*; use crate::prelude::*;
/// Types of items available in the game.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Hash)] #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Hash)]
pub enum ItemType { pub enum ItemType {
Berry, Berry,
@@ -102,18 +103,21 @@ impl ItemType {
} }
} }
/// A stack of items of a specific type.
#[derive(Component, Serialize, Deserialize, Clone, Debug)] #[derive(Component, Serialize, Deserialize, Clone, Debug)]
pub struct ItemStack { pub struct ItemStack {
pub item_type: ItemType, pub item_type: ItemType,
pub amount: u32, pub amount: u32,
} }
/// Resource containing all items owned by the player.
#[derive(Resource, Default, Serialize, Deserialize)] #[derive(Resource, Default, Serialize, Deserialize)]
pub struct Inventory { pub struct Inventory {
pub items: Vec<Entity>, pub items: Vec<Entity>,
} }
impl Inventory { impl Inventory {
/// Checks if the inventory contains a specific item type.
pub fn has_item_type(&self, items_query: &Query<&ItemStack>, item_type: ItemType) -> bool { pub fn has_item_type(&self, items_query: &Query<&ItemStack>, item_type: ItemType) -> bool {
self.items.iter().any(|&entity| { self.items.iter().any(|&entity| {
if let Ok(stack) = items_query.get(entity) { if let Ok(stack) = items_query.get(entity) {
@@ -124,6 +128,7 @@ impl Inventory {
}) })
} }
/// Adds or removes items from the inventory.
pub fn update_item_stack( pub fn update_item_stack(
&mut self, &mut self,
commands: &mut Commands, commands: &mut Commands,
@@ -193,11 +198,13 @@ impl Inventory {
} }
} }
/// Markers for inventory UI root nodes.
#[derive(Component)] #[derive(Component)]
pub enum RootMarker { pub enum RootMarker {
Inventory, Inventory,
} }
/// Markers for inventory-related buttons.
#[derive(Component)] #[derive(Component)]
pub enum ButtonType { pub enum ButtonType {
InventoryOpen, InventoryOpen,

View File

@@ -5,6 +5,7 @@ use components::*;
pub mod components; pub mod components;
pub mod ui; pub mod ui;
/// Plugin for the inventory system, including storage and UI.
pub struct InventoryPlugin; pub struct InventoryPlugin;
impl Plugin for InventoryPlugin { impl Plugin for InventoryPlugin {
@@ -18,6 +19,7 @@ impl Plugin for InventoryPlugin {
} }
} }
/// Handles inventory button interactions.
fn buttons( fn buttons(
mut commands: Commands, mut commands: Commands,
mut interaction_query: Query<(&Interaction, &ButtonType), (Changed<Interaction>, With<Button>)>, mut interaction_query: Query<(&Interaction, &ButtonType), (Changed<Interaction>, With<Button>)>,
@@ -37,6 +39,7 @@ fn buttons(
} }
} }
/// Debug system to add/remove berries with arrow keys.
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
fn debug_modify_berries( fn debug_modify_berries(
mut commands: Commands, mut commands: Commands,

View File

@@ -2,6 +2,7 @@ use super::super::components::RootMarker;
use crate::prelude::GameConfig; use crate::prelude::GameConfig;
use crate::{features::inventory::ui::list_itemstack, prelude::*}; use crate::{features::inventory::ui::list_itemstack, prelude::*};
/// Spawns the inventory popup.
pub fn open_inventory( pub fn open_inventory(
commands: &mut Commands, commands: &mut Commands,
items: Query<&ItemStack>, items: Query<&ItemStack>,

View File

@@ -1,5 +1,6 @@
use crate::prelude::*; use crate::prelude::*;
/// Creates a UI bundle for a single item stack in the inventory list.
pub fn list_itemstack( pub fn list_itemstack(
itemstack: &ItemStack, itemstack: &ItemStack,
game_config: &GameConfig, game_config: &GameConfig,

View File

@@ -1,16 +1,19 @@
use crate::prelude::*; use crate::prelude::*;
/// Markers for shop UI.
#[derive(Component)] #[derive(Component)]
pub enum RootMarker { pub enum RootMarker {
Shop, Shop,
} }
/// Buttons in the shop.
#[derive(Component)] #[derive(Component)]
pub enum ButtonType { pub enum ButtonType {
ShopOpen, ShopOpen,
ShopBuyItem(ShopOffer), ShopBuyItem(ShopOffer),
} }
/// An item available for purchase.
#[derive(Clone)] #[derive(Clone)]
pub struct ShopOffer { pub struct ShopOffer {
pub item: ItemStack, pub item: ItemStack,
@@ -18,6 +21,7 @@ pub struct ShopOffer {
} }
impl ShopOffer { impl ShopOffer {
/// Generates a list of all current offers.
pub fn list_all(game_config: &GameConfig, tile_count: u32) -> Vec<ShopOffer> { pub fn list_all(game_config: &GameConfig, tile_count: u32) -> Vec<ShopOffer> {
let mut offers = Vec::new(); let mut offers = Vec::new();
@@ -50,6 +54,7 @@ impl ShopOffer {
offers offers
} }
/// Attempts to purchase the offer.
pub fn buy( pub fn buy(
&self, &self,
inventory: &mut Inventory, inventory: &mut Inventory,

View File

@@ -5,6 +5,7 @@ use ui::open_shop;
pub mod components; pub mod components;
pub mod ui; pub mod ui;
/// Plugin for the in-game shop.
pub struct ShopPlugin; pub struct ShopPlugin;
impl Plugin for ShopPlugin { impl Plugin for ShopPlugin {
@@ -13,6 +14,7 @@ impl Plugin for ShopPlugin {
} }
} }
/// Handles shop button interactions.
fn buttons( fn buttons(
mut commands: Commands, mut commands: Commands,
mut interaction_query: Query<(&Interaction, &ButtonType), (Changed<Interaction>, With<Button>)>, mut interaction_query: Query<(&Interaction, &ButtonType), (Changed<Interaction>, With<Button>)>,

View File

@@ -1,6 +1,7 @@
use super::super::components::*; use super::super::components::*;
use crate::{features::inventory::ui::item::list_itemstack, prelude::*}; use crate::{features::inventory::ui::item::list_itemstack, prelude::*};
/// Creates the UI bundle for a shop offer.
pub fn shop_offer( pub fn shop_offer(
offer: &ShopOffer, offer: &ShopOffer,
game_config: &GameConfig, game_config: &GameConfig,
@@ -26,6 +27,7 @@ pub fn shop_offer(
) )
} }
/// Creates the UI bundle for displaying a price.
pub fn shop_price( pub fn shop_price(
price: u32, price: u32,
asset_server: &Res<AssetServer>, asset_server: &Res<AssetServer>,

View File

@@ -1,6 +1,7 @@
use super::super::components::*; use super::super::components::*;
use crate::{features::shop::ui::shop_offer, prelude::*}; use crate::{features::shop::ui::shop_offer, prelude::*};
/// Spawns the shop popup.
pub fn open_shop( pub fn open_shop(
commands: &mut Commands, commands: &mut Commands,
game_config: &GameConfig, game_config: &GameConfig,