use std::marker::PhantomData;
use bevy::prelude::*;
use crate::items::ItemType;
use crate::items::inventory::{Container, ContainerComponent, PrimaryContainer};
#[derive(Debug, Clone)]
pub struct CraftingIngredient<I> {
pub item: I,
pub count: u32,
}
#[derive(Debug, Clone)]
pub struct CraftingRecipe<I> {
pub name: &'static str,
pub ingredients: Vec<CraftingIngredient<I>>,
pub outputs: Vec<CraftingIngredient<I>>,
}
#[derive(Resource, Debug, Clone, Default)]
pub struct CraftingRegistry<I: ItemType> {
pub recipes: Vec<CraftingRecipe<I>>,
}
impl<I: ItemType> CraftingRegistry<I> {
pub fn add(&mut self, recipe: CraftingRecipe<I>) {
self.recipes.push(recipe);
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CraftingError {
InvalidRecipe,
InsufficientIngredients,
InventoryFull,
}
pub fn can_craft<I: ItemType>(
registry: &CraftingRegistry<I>,
inventory: &impl Container<I>,
recipe_index: usize,
) -> bool {
let Some(recipe) = registry.recipes.get(recipe_index) else {
return false;
};
recipe
.ingredients
.iter()
.all(|ing| inventory.count_of(ing.item) >= ing.count)
}
pub fn try_craft<I: ItemType>(
registry: &CraftingRegistry<I>,
inventory: &mut impl Container<I>,
recipe_index: usize,
) -> Result<(), CraftingError> {
let recipe = registry
.recipes
.get(recipe_index)
.ok_or(CraftingError::InvalidRecipe)?;
for ing in &recipe.ingredients {
if inventory.count_of(ing.item) < ing.count {
return Err(CraftingError::InsufficientIngredients);
}
}
let free_slots = inventory.capacity() - inventory.iter_slots().filter(|s| s.is_some()).count();
let new_types = recipe
.outputs
.iter()
.filter(|out| inventory.count_of(out.item) == 0)
.count();
if new_types > free_slots {
return Err(CraftingError::InventoryFull);
}
for ing in &recipe.ingredients {
inventory.remove(ing.item, ing.count);
}
for out in &recipe.outputs {
inventory.add(out.item, out.count);
}
Ok(())
}
#[derive(Resource, Default)]
pub struct CraftingMenuState {
pub visible: bool,
rebuild: bool,
}
#[derive(Component)]
struct CraftingMenuRoot;
#[derive(Component)]
struct CraftingRecipeRow {
recipe_index: usize,
}
fn spawn_crafting_menu(mut commands: Commands) {
commands.spawn((
CraftingMenuRoot,
Node {
position_type: PositionType::Absolute,
right: Val::Px(10.0),
top: Val::Px(60.0),
width: Val::Px(340.0),
max_height: Val::Vh(75.0),
flex_direction: FlexDirection::Column,
padding: UiRect::all(Val::Px(10.0)),
row_gap: Val::Px(4.0),
overflow: Overflow::clip_y(),
..default()
},
BackgroundColor(Color::srgba(0.04, 0.04, 0.04, 0.88)),
Visibility::Hidden,
));
}
fn toggle_crafting_menu(keys: Res<ButtonInput<KeyCode>>, mut state: ResMut<CraftingMenuState>) {
if keys.just_pressed(KeyCode::KeyC) {
state.visible = !state.visible;
if state.visible {
state.rebuild = true;
}
}
if keys.just_pressed(KeyCode::Escape) && state.visible {
state.visible = false;
}
}
fn sync_crafting_visibility(
state: Res<CraftingMenuState>,
mut root_q: Query<&mut Visibility, With<CraftingMenuRoot>>,
) {
if !state.is_changed() {
return;
}
let Ok(mut vis) = root_q.single_mut() else {
return;
};
*vis = if state.visible {
Visibility::Visible
} else {
Visibility::Hidden
};
}
fn rebuild_crafting_menu<I: ItemType>(
mut commands: Commands,
mut state: ResMut<CraftingMenuState>,
registry: Res<CraftingRegistry<I>>,
root_q: Query<Entity, With<CraftingMenuRoot>>,
row_q: Query<Entity, With<CraftingRecipeRow>>,
) {
if !state.rebuild {
return;
}
state.rebuild = false;
for e in row_q.iter() {
commands.entity(e).despawn();
}
let Ok(root) = root_q.single() else {
return;
};
let header = commands
.spawn((
CraftingRecipeRow {
recipe_index: usize::MAX,
},
Node {
padding: UiRect::bottom(Val::Px(6.0)),
..default()
},
Text::new("-- Crafting (C to close) --"),
TextFont {
font_size: 15.0,
..default()
},
TextColor(Color::WHITE),
))
.id();
commands.entity(root).add_child(header);
if registry.recipes.is_empty() {
let empty = commands
.spawn((
CraftingRecipeRow {
recipe_index: usize::MAX,
},
Text::new("No recipes registered."),
TextFont {
font_size: 13.0,
..default()
},
TextColor(Color::srgb(0.55, 0.55, 0.55)),
))
.id();
commands.entity(root).add_child(empty);
return;
}
for (i, recipe) in registry.recipes.iter().enumerate() {
let ing_str = recipe
.ingredients
.iter()
.map(|ing| format!("{} ×{}", ing.item.name(), ing.count))
.collect::<Vec<_>>()
.join(", ");
let out_str = recipe
.outputs
.iter()
.map(|out| format!("{} ×{}", out.item.name(), out.count))
.collect::<Vec<_>>()
.join(", ");
let label = format!("[{}] {} > {}", recipe.name, ing_str, out_str);
let row = commands
.spawn((
CraftingRecipeRow { recipe_index: i },
Button,
Node {
padding: UiRect::all(Val::Px(6.0)),
width: Val::Percent(100.0),
..default()
},
BackgroundColor(Color::srgba(0.15, 0.15, 0.15, 0.90)),
Text::new(label),
TextFont {
font_size: 12.0,
..default()
},
TextColor(Color::srgb(0.70, 0.70, 0.70)),
))
.id();
commands.entity(root).add_child(row);
}
let footer = commands
.spawn((
CraftingRecipeRow {
recipe_index: usize::MAX,
},
Node {
padding: UiRect::top(Val::Px(6.0)),
..default()
},
Text::new("Green = craftable | click to craft"),
TextFont {
font_size: 11.0,
..default()
},
TextColor(Color::srgb(0.45, 0.45, 0.45)),
))
.id();
commands.entity(root).add_child(footer);
}
fn update_crafting_row_colors<I: ItemType>(
mut row_q: Query<(
&CraftingRecipeRow,
&mut BackgroundColor,
&mut TextColor,
&Interaction,
)>,
registry: Res<CraftingRegistry<I>>,
container_query: Query<&ContainerComponent<I>, With<PrimaryContainer>>,
state: Res<CraftingMenuState>,
) {
if !state.visible {
return;
}
let Ok(container) = container_query.single() else {
return;
};
for (row, mut bg, mut tc, interaction) in row_q.iter_mut() {
if row.recipe_index == usize::MAX {
continue;
}
let craftable = can_craft(®istry, &container.0, row.recipe_index);
let hovered = matches!(interaction, Interaction::Hovered | Interaction::Pressed);
*bg = match (craftable, hovered) {
(true, true) => BackgroundColor(Color::srgba(0.18, 0.50, 0.18, 0.95)),
(true, false) => BackgroundColor(Color::srgba(0.10, 0.28, 0.10, 0.90)),
(false, true) => BackgroundColor(Color::srgba(0.25, 0.25, 0.25, 0.90)),
(false, false) => BackgroundColor(Color::srgba(0.15, 0.15, 0.15, 0.90)),
};
*tc = if craftable {
TextColor(Color::srgb(0.82, 1.0, 0.82))
} else {
TextColor(Color::srgb(0.45, 0.45, 0.45))
};
}
}
fn handle_craft_click<I: ItemType>(
row_q: Query<(&CraftingRecipeRow, &Interaction), Changed<Interaction>>,
registry: Res<CraftingRegistry<I>>,
mut container_query: Query<&mut ContainerComponent<I>, With<PrimaryContainer>>,
state: Res<CraftingMenuState>,
) {
if !state.visible {
return;
}
let Ok(mut container) = container_query.single_mut() else {
return;
};
for (row, interaction) in row_q.iter() {
if row.recipe_index == usize::MAX {
continue;
}
if matches!(interaction, Interaction::Pressed) {
let _ = try_craft(®istry, &mut container.0, row.recipe_index);
}
}
}
pub struct CraftingPlugin<I: ItemType> {
_m: PhantomData<I>,
}
impl<I: ItemType> CraftingPlugin<I> {
pub fn new() -> Self {
Self { _m: PhantomData }
}
}
impl<I: ItemType> Default for CraftingPlugin<I> {
fn default() -> Self {
Self::new()
}
}
impl<I: ItemType> Plugin for CraftingPlugin<I> {
fn build(&self, app: &mut App) {
app.init_resource::<CraftingRegistry<I>>();
}
}
pub struct CraftingUiPlugin<I: ItemType> {
_m: PhantomData<I>,
}
impl<I: ItemType> CraftingUiPlugin<I> {
pub fn new() -> Self {
Self { _m: PhantomData }
}
}
impl<I: ItemType> Default for CraftingUiPlugin<I> {
fn default() -> Self {
Self::new()
}
}
impl<I: ItemType> Plugin for CraftingUiPlugin<I> {
fn build(&self, app: &mut App) {
app.init_resource::<CraftingMenuState>();
app.add_systems(Startup, spawn_crafting_menu);
app.add_systems(
Update,
(
toggle_crafting_menu,
sync_crafting_visibility,
rebuild_crafting_menu::<I>,
update_crafting_row_colors::<I>,
handle_craft_click::<I>,
)
.chain(),
);
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::items::{DefaultItem, Inventory};
fn make_registry() -> CraftingRegistry<DefaultItem> {
let mut r = CraftingRegistry::default();
r.add(CraftingRecipe {
name: "Crystal from Ore",
ingredients: vec![CraftingIngredient {
item: DefaultItem::IronOre,
count: 4,
}],
outputs: vec![CraftingIngredient {
item: DefaultItem::Crystal,
count: 1,
}],
});
r.add(CraftingRecipe {
name: "Rare from Ore+Crystal",
ingredients: vec![
CraftingIngredient {
item: DefaultItem::IronOre,
count: 2,
},
CraftingIngredient {
item: DefaultItem::Crystal,
count: 1,
},
],
outputs: vec![CraftingIngredient {
item: DefaultItem::RareMineral,
count: 1,
}],
});
r
}
#[test]
fn can_craft_true_when_enough_items() {
let registry = make_registry();
let mut inv = Inventory::new(9);
inv.add(DefaultItem::IronOre, 4);
assert!(can_craft(®istry, &inv, 0));
}
#[test]
fn can_craft_false_when_insufficient() {
let registry = make_registry();
let mut inv = Inventory::new(9);
inv.add(DefaultItem::IronOre, 3); assert!(!can_craft(®istry, &inv, 0));
}
#[test]
fn can_craft_false_for_invalid_index() {
let registry = make_registry();
let inv = Inventory::<DefaultItem>::new(9);
assert!(!can_craft(®istry, &inv, 99));
}
#[test]
fn try_craft_consumes_ingredients_and_produces_outputs() {
let registry = make_registry();
let mut inv = Inventory::new(9);
inv.add(DefaultItem::IronOre, 5);
let result = try_craft(®istry, &mut inv, 0);
assert_eq!(result, Ok(()));
assert_eq!(inv.count_of(DefaultItem::IronOre), 1); assert_eq!(inv.count_of(DefaultItem::Crystal), 1);
}
#[test]
fn try_craft_fails_with_insufficient_ingredients() {
let registry = make_registry();
let mut inv = Inventory::new(9);
inv.add(DefaultItem::IronOre, 2);
let result = try_craft(®istry, &mut inv, 0);
assert_eq!(result, Err(CraftingError::InsufficientIngredients));
assert_eq!(inv.count_of(DefaultItem::IronOre), 2);
}
#[test]
fn try_craft_fails_with_invalid_recipe() {
let registry = make_registry();
let mut inv = Inventory::new(9);
assert_eq!(
try_craft(®istry, &mut inv, 99),
Err(CraftingError::InvalidRecipe)
);
}
#[test]
fn try_craft_multi_ingredient_recipe() {
let registry = make_registry();
let mut inv = Inventory::new(9);
inv.add(DefaultItem::IronOre, 2);
inv.add(DefaultItem::Crystal, 1);
let result = try_craft(®istry, &mut inv, 1);
assert_eq!(result, Ok(()));
assert_eq!(inv.count_of(DefaultItem::IronOre), 0);
assert_eq!(inv.count_of(DefaultItem::Crystal), 0);
assert_eq!(inv.count_of(DefaultItem::RareMineral), 1);
}
#[test]
fn try_craft_inventory_full_returns_error() {
let registry = make_registry();
let mut inv = Inventory::new(1);
inv.add(DefaultItem::IronOre, 4);
let result = try_craft(®istry, &mut inv, 0);
assert_eq!(result, Err(CraftingError::InventoryFull));
assert_eq!(inv.count_of(DefaultItem::IronOre), 4);
}
}