voxel-framework 0.7.0

A reusable voxel engine framework built on Bevy
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
//! Crafting system — shapeless recipes, registry, and in-game crafting menu.
//!
//! # Data model
//!
//! [`CraftingRegistry<I>`] is a Bevy resource that holds a list of
//! [`CraftingRecipe<I>`] values. Each recipe describes a set of input
//! ingredients and output items. Recipes are *shapeless* — only item types and
//! counts matter, not arrangement in a grid.
//!
//! # Crafting logic
//!
//! [`try_craft`] is a pure function: it verifies that the inventory holds
//! enough of every ingredient, consumes them atomically, and produces the
//! outputs. [`can_craft`] performs the read-only check without side effects.
//!
//! # UI
//!
//! [`CraftingPlugin<I>`] adds a crafting menu toggled by the `C` key.
//! The menu lists every registered recipe and highlights those the player can
//! currently craft. Clicking a highlighted row invokes [`try_craft`].
//!
//! # Integration
//!
//! ```no_run
//! use bevy::prelude::*;
//! use voxel_framework::crafting::{CraftingPlugin, CraftingRecipe, CraftingIngredient, CraftingRegistry};
//! use voxel_framework::items::DefaultItem;
//!
//! App::new()
//!     .add_plugins(CraftingPlugin::<DefaultItem>::new())
//!     .add_systems(Startup, register_recipes);
//!
//! fn register_recipes(mut registry: ResMut<CraftingRegistry<DefaultItem>>) {
//!     registry.add(CraftingRecipe {
//!         name: "Crystal from Ore",
//!         ingredients: vec![CraftingIngredient { item: DefaultItem::IronOre, count: 4 }],
//!         outputs:     vec![CraftingIngredient { item: DefaultItem::Crystal,  count: 1 }],
//!     });
//! }
//! ```

use std::marker::PhantomData;

use bevy::prelude::*;

use crate::items::ItemType;
use crate::items::inventory::{Container, ContainerComponent, PrimaryContainer};

// ─── Data layer ───────────────────────────────────────────────────────────────

/// A single ingredient entry: the item type and the required or produced quantity.
#[derive(Debug, Clone)]
pub struct CraftingIngredient<I> {
    /// The item type consumed or produced.
    pub item: I,
    /// How many items are consumed or produced.
    pub count: u32,
}

/// A shapeless crafting recipe: consume [`ingredients`](CraftingRecipe::ingredients),
/// produce [`outputs`](CraftingRecipe::outputs).
///
/// "Shapeless" means arrangement in a crafting grid is not modelled — only item
/// types and counts matter.
#[derive(Debug, Clone)]
pub struct CraftingRecipe<I> {
    /// Short human-readable name shown in the crafting menu.
    pub name: &'static str,
    /// Items consumed from the player's inventory when this recipe is crafted.
    pub ingredients: Vec<CraftingIngredient<I>>,
    /// Items added to the player's inventory when this recipe is crafted.
    pub outputs: Vec<CraftingIngredient<I>>,
}

/// Registry of all crafting recipes available in the game.
///
/// Inserted automatically by [`CraftingPlugin`] as an empty resource.
/// Populate it from a startup system before the crafting menu is first opened.
#[derive(Resource, Debug, Clone, Default)]
pub struct CraftingRegistry<I: ItemType> {
    /// Ordered list of registered recipes. Indices are stable for the session.
    pub recipes: Vec<CraftingRecipe<I>>,
}

impl<I: ItemType> CraftingRegistry<I> {
    /// Appends a recipe to the registry.
    pub fn add(&mut self, recipe: CraftingRecipe<I>) {
        self.recipes.push(recipe);
    }
}

// ─── Pure crafting functions ──────────────────────────────────────────────────

/// Reason a craft attempt failed.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CraftingError {
    /// No recipe exists at the given index.
    InvalidRecipe,
    /// The inventory does not hold enough of one or more ingredients.
    InsufficientIngredients,
    /// The inventory does not have room for the produced outputs.
    InventoryFull,
}

/// Returns `true` if the container satisfies all ingredients of `recipe_index`.
///
/// Does **not** mutate the container.
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)
}

/// Attempts to craft recipe `recipe_index`, consuming ingredients and producing
/// outputs.
///
/// # Atomicity
///
/// All preconditions are checked before any mutation:
/// - The recipe must exist.
/// - The container must hold enough of every ingredient.
/// - The container must have enough free slots for output item types not yet
///   present.
///
/// Returns `Ok(())` on success and a [`CraftingError`] without modifying the
/// container on failure.
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)?;

    // Pre-check: enough ingredients?
    for ing in &recipe.ingredients {
        if inventory.count_of(ing.item) < ing.count {
            return Err(CraftingError::InsufficientIngredients);
        }
    }

    // Pre-check: free slots for new output types.
    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);
    }

    // Consume ingredients.
    for ing in &recipe.ingredients {
        inventory.remove(ing.item, ing.count);
    }

    // Produce outputs.
    for out in &recipe.outputs {
        inventory.add(out.item, out.count);
    }

    Ok(())
}

// ─── UI state ────────────────────────────────────────────────────────────────

/// Runtime state of the crafting menu.
#[derive(Resource, Default)]
pub struct CraftingMenuState {
    /// Whether the crafting menu is currently visible.
    pub visible: bool,
    /// Dirty flag: set to `true` to trigger a full UI rebuild on next frame.
    rebuild: bool,
}

// ─── UI markers ───────────────────────────────────────────────────────────────

/// Marker component on the root crafting menu panel.
#[derive(Component)]
struct CraftingMenuRoot;

/// Marker component on each recipe row in the crafting menu.
///
/// `usize::MAX` is used as a sentinel for header/footer rows that are not
/// interactive recipes.
#[derive(Component)]
struct CraftingRecipeRow {
    recipe_index: usize,
}

// ─── Systems ──────────────────────────────────────────────────────────────────

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;
        }
    }
    // ESC closes crafting menu if open (without consuming the event — pause menu
    // handles its own ESC separately).
    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;

    // Despawn stale rows.
    for e in row_q.iter() {
        commands.entity(e).despawn();
    }

    let Ok(root) = root_q.single() else {
        return;
    };

    // Header.
    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;
    }

    // One row per recipe.
    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);
    }

    // Footer hint.
    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(&registry, &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(&registry, &mut container.0, row.recipe_index);
        }
    }
}

// ─── Plugins ──────────────────────────────────────────────────────────────────

/// Core crafting plugin — registers [`CraftingRegistry<I>`] only.
///
/// No UI, no input, no systems. Use alongside [`CraftingUiPlugin<I>`] when you
/// want the built-in `C`-key crafting menu, or provide your own UI by reading
/// [`CraftingRegistry<I>`] and calling [`can_craft`] / [`try_craft`] directly.
///
/// Mirrors the [`crate::world::MiningPlugin`] / [`crate::world::MiningInputPlugin`] split pattern.
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>>();
    }
}

/// Batteries-included crafting UI plugin.
///
/// Adds [`CraftingMenuState`], spawns the in-game crafting panel, and wires the
/// `C`-key toggle, recipe highlighting, and click-to-craft. Requires
/// [`CraftingPlugin<I>`] to have been added first (for the registry).
///
/// # Example
///
/// ```no_run
/// use bevy::prelude::*;
/// use voxel_framework::crafting::{CraftingPlugin, CraftingUiPlugin, CraftingRecipe, CraftingIngredient};
/// use voxel_framework::items::DefaultItem;
///
/// App::new()
///     .add_plugins(CraftingPlugin::<DefaultItem>::new())
///     .add_plugins(CraftingUiPlugin::<DefaultItem>::new())
///     .add_systems(Startup, register_recipes);
///
/// fn register_recipes(mut registry: ResMut<voxel_framework::crafting::CraftingRegistry<DefaultItem>>) {
///     registry.add(CraftingRecipe {
///         name: "Iron Block",
///         ingredients: vec![CraftingIngredient { item: DefaultItem::IronOre, count: 9 }],
///         outputs:     vec![CraftingIngredient { item: DefaultItem::Crystal,  count: 1 }],
///     });
/// }
/// ```
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(),
        );
    }
}

// ─── Tests ────────────────────────────────────────────────────────────────────

#[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(&registry, &inv, 0));
    }

    #[test]
    fn can_craft_false_when_insufficient() {
        let registry = make_registry();
        let mut inv = Inventory::new(9);
        inv.add(DefaultItem::IronOre, 3); // need 4
        assert!(!can_craft(&registry, &inv, 0));
    }

    #[test]
    fn can_craft_false_for_invalid_index() {
        let registry = make_registry();
        let inv = Inventory::<DefaultItem>::new(9);
        assert!(!can_craft(&registry, &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(&registry, &mut inv, 0);
        assert_eq!(result, Ok(()));
        assert_eq!(inv.count_of(DefaultItem::IronOre), 1); // 5 - 4
        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(&registry, &mut inv, 0);
        assert_eq!(result, Err(CraftingError::InsufficientIngredients));
        // Inventory unchanged.
        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(&registry, &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(&registry, &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();
        // Fill all slots with RockFragment so no room for Crystal output.
        let mut inv = Inventory::new(1);
        inv.add(DefaultItem::IronOre, 4); // occupies the 1 slot

        // Now inventory is full (1 slot used).
        // Crystal is a new type — needs a free slot, but none available.
        let result = try_craft(&registry, &mut inv, 0);
        assert_eq!(result, Err(CraftingError::InventoryFull));
        // Ingredients not consumed.
        assert_eq!(inv.count_of(DefaultItem::IronOre), 4);
    }
}