Skip to main content

write_prefab_spawner/
write_prefab_spawner.rs

1use brdb::{AsBrdbValue, BrFsReader, Brick, Brz, IntoReader, World, WirePort, assets};
2use std::path::PathBuf;
3
4/// Builds a world where pressing a button spawns a prefab.
5///
6/// An interactive button's `bHeld` output is wired into a prefab-spawner
7/// gate's `Exec` input, so each press spawns a copy of an embedded
8/// single-brick prefab. Demonstrates `World::add_prefab` (content-addressed
9/// embedding) together with the wire API.
10fn main() -> Result<(), Box<dyn std::error::Error>> {
11    // 1. Build the prefab that gets spawned: a single red brick.
12    let mut prefab = World::new();
13    prefab.meta.bundle.name = "Spawned Brick".to_string();
14    prefab.bricks.push(Brick {
15        position: (0, 0, 6).into(),
16        color: (255, 0, 0).into(),
17        ..Default::default()
18    });
19    prefab.make_prefab();
20    let prefab_bytes = prefab.to_brz_vec()?;
21
22    // 2. Build the outer world holding the button + spawner.
23    let mut world = World::new();
24    // Registers the built-in component/port tables so the Button and
25    // PrefabSpawner component types and their wire ports resolve.
26    world.register_all_components();
27    world.meta.bundle.description = "Button-triggered prefab spawner".to_string();
28
29    // Embed the prefab; the returned path is what the spawner references.
30    let prefab_path = world.add_prefab(prefab_bytes);
31
32    // A pressable button (Component_Button on a 1x1 flat round brick). The
33    // crate exposes brick assets as constants; components it doesn't model as
34    // typed gates (like the button and spawner) are built from string names
35    // via LiteralComponent.
36    let (button, button_id) = Brick {
37        position: (0, 0, 2).into(),
38        color: (0, 255, 0).into(),
39        asset: assets::bricks::B_1X1F_ROUND,
40        ..Default::default()
41    }
42    .with_component(assets::LiteralComponent::new("Component_Button").with_data([(
43        "PromptCustomLabel",
44        Box::new("Spawn Brick".to_string()) as Box<dyn AsBrdbValue>,
45    )]))
46    .with_id_split();
47
48    // The prefab-spawner gate, pointed at the embedded prefab.
49    let (spawner, spawner_id) = Brick {
50        position: (15, 0, 1).into(),
51        color: (0, 0, 255).into(),
52        asset: assets::bricks::B_1X1_GATE_EXEC_PREFAB_SPAWNER,
53        ..Default::default()
54    }
55    .with_component(
56        assets::LiteralComponent::new("BrickComponentType_WireGraph_Exec_PrefabSpawner").with_data(
57            [(
58                "Prefab",
59                Box::new(prefab_path.clone()) as Box<dyn AsBrdbValue>,
60            )],
61        ),
62    )
63    .with_id_split();
64
65    world.add_bricks([button, spawner]);
66
67    // Wire the button's held signal into the spawner's Exec input, so a
68    // press fires the spawn.
69    world.add_wire_connection(
70        WirePort::new(button_id, "Component_Button", "bHeld"),
71        WirePort::new(
72            spawner_id,
73            "BrickComponentType_WireGraph_Exec_PrefabSpawner",
74            "Exec",
75        ),
76    );
77
78    // 3. Write it out.
79    let path = PathBuf::from("./example_prefab_spawner.brz");
80    world.write_brz(&path)?;
81    println!("wrote {}", path.display());
82
83    // Read it back to confirm the embedded prefab and wire survived.
84    let reader = Brz::open(&path)?.into_reader();
85    println!("embedded prefab: {}", prefab_path);
86    println!("prefab paths in archive: {:?}", reader.prefab_paths()?);
87    println!("{}", reader.get_fs()?.render());
88
89    Ok(())
90}