pub struct PrefabTransform {
pub rotation: u8,
pub mirror_h: bool,
pub mirror_v: bool,
}Fields§
§rotation: u8§mirror_h: bool§mirror_v: boolImplementations§
Source§impl PrefabTransform
impl PrefabTransform
Sourcepub fn apply(&self, prefab: &Prefab) -> Prefab
pub fn apply(&self, prefab: &Prefab) -> Prefab
Examples found in repository?
examples/advanced_prefabs.rs (line 90)
10fn main() {
11 println!("=== Advanced Prefab System Demo ===\n");
12
13 // Step 1: Create prefab library programmatically
14 println!("1. Creating Prefab Library:");
15 let library = create_sample_library();
16
17 println!(
18 " Created library with {} prefabs:",
19 library.get_prefabs().len()
20 );
21 for prefab in library.get_prefabs() {
22 println!(
23 " - {} ({}x{}, weight: {:.1}, tags: {:?})",
24 prefab.name, prefab.width, prefab.height, prefab.weight, prefab.tags
25 );
26 }
27
28 // Step 2: Demonstrate weighted selection
29 println!("\n2. Weighted Selection Test:");
30 let mut rng = Rng::new(12345);
31 let mut selection_counts = std::collections::HashMap::new();
32
33 for _ in 0..100 {
34 if let Some(prefab) = library.select_weighted(&mut rng, None) {
35 *selection_counts.entry(prefab.name.clone()).or_insert(0) += 1;
36 }
37 }
38
39 println!(" Selection frequency (100 trials):");
40 for (name, count) in &selection_counts {
41 println!(" {}: {} times", name, count);
42 }
43
44 // Step 3: Tag-based selection
45 println!("\n3. Tag-based Selection:");
46 let room_prefabs = library.get_by_tag("room");
47 let corridor_prefabs = library.get_by_tag("corridor");
48
49 println!(" Room prefabs: {}", room_prefabs.len());
50 for prefab in &room_prefabs {
51 println!(" - {}", prefab.name);
52 }
53
54 println!(" Corridor prefabs: {}", corridor_prefabs.len());
55 for prefab in &corridor_prefabs {
56 println!(" - {}", prefab.name);
57 }
58
59 // Step 4: Transformation examples
60 println!("\n4. Prefab Transformations:");
61 if let Some(base_prefab) = library.get_prefabs().first() {
62 println!(
63 " Base prefab '{}' ({}x{}):",
64 base_prefab.name, base_prefab.width, base_prefab.height
65 );
66 print_prefab_pattern(base_prefab);
67
68 // Rotation
69 let rotated = base_prefab.rotated();
70 println!(
71 " After 90° rotation ({}x{}):",
72 rotated.width, rotated.height
73 );
74 print_prefab_pattern(&rotated);
75
76 // Horizontal mirror
77 let mirrored = base_prefab.mirrored_horizontal();
78 println!(
79 " After horizontal mirror ({}x{}):",
80 mirrored.width, mirrored.height
81 );
82 print_prefab_pattern(&mirrored);
83
84 // Combined transformation
85 let transform = PrefabTransform {
86 rotation: 1,
87 mirror_h: true,
88 mirror_v: false,
89 };
90 let transformed = transform.apply(base_prefab);
91 println!(
92 " After rotation + mirror ({}x{}):",
93 transformed.width, transformed.height
94 );
95 print_prefab_pattern(&transformed);
96 }
97
98 // Step 5: Generation with advanced prefabs
99 println!("\n5. Generation with Advanced Prefabs:");
100 let config = PrefabConfig {
101 max_prefabs: 5,
102 min_spacing: 3,
103 allow_rotation: true,
104 allow_mirroring: true,
105 weighted_selection: true,
106 };
107
108 let placer = PrefabPlacer::new(config, library.clone());
109 let mut grid = Grid::new(30, 25);
110 placer.generate(&mut grid, 54321);
111
112 let floor_count = grid.count(|t| t.is_floor());
113 println!(
114 " Generated {}x{} grid with {} floor tiles",
115 grid.width(),
116 grid.height(),
117 floor_count
118 );
119
120 print_grid(&grid);
121
122 // Step 6: JSON serialization example
123 println!("\n6. JSON Serialization:");
124 match library.save_to_json("prefab_library.json") {
125 Ok(()) => {
126 println!(" ✅ Saved library to prefab_library.json");
127
128 // Try to load it back
129 match PrefabLibrary::load_from_json("prefab_library.json") {
130 Ok(loaded_library) => {
131 println!(" ✅ Successfully loaded library back");
132 println!(" Loaded {} prefabs", loaded_library.get_prefabs().len());
133 }
134 Err(e) => println!(" ❌ Failed to load: {}", e),
135 }
136 }
137 Err(e) => println!(" ❌ Failed to save: {}", e),
138 }
139
140 // Step 7: Performance comparison
141 println!("\n7. Performance Comparison:");
142
143 // Simple generation
144 let simple_config = PrefabConfig {
145 max_prefabs: 10,
146 min_spacing: 2,
147 allow_rotation: false,
148 allow_mirroring: false,
149 weighted_selection: false,
150 };
151
152 let start = std::time::Instant::now();
153 let simple_placer = PrefabPlacer::new(simple_config, library.clone());
154 let mut simple_grid = Grid::new(40, 30);
155 simple_placer.generate(&mut simple_grid, 98765);
156 let simple_time = start.elapsed();
157
158 // Advanced generation
159 let advanced_config = PrefabConfig {
160 max_prefabs: 10,
161 min_spacing: 2,
162 allow_rotation: true,
163 allow_mirroring: true,
164 weighted_selection: true,
165 };
166
167 let start = std::time::Instant::now();
168 let advanced_placer = PrefabPlacer::new(advanced_config, library);
169 let mut advanced_grid = Grid::new(40, 30);
170 advanced_placer.generate(&mut advanced_grid, 98765);
171 let advanced_time = start.elapsed();
172
173 println!(" Simple generation: {:?}", simple_time);
174 println!(" Advanced generation: {:?}", advanced_time);
175 println!(
176 " Overhead: {:.1}x",
177 advanced_time.as_nanos() as f32 / simple_time.as_nanos() as f32
178 );
179
180 println!("\n✅ Advanced prefab system demo complete!");
181 println!(" - JSON serialization for persistent libraries");
182 println!(" - Weighted selection for balanced generation");
183 println!(" - Transformations for variety and reuse");
184 println!(" - Tag-based organization for targeted selection");
185}pub fn random( rng: &mut Rng, allow_rotation: bool, allow_mirroring: bool, ) -> Self
Trait Implementations§
Source§impl Clone for PrefabTransform
impl Clone for PrefabTransform
Source§fn clone(&self) -> PrefabTransform
fn clone(&self) -> PrefabTransform
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for PrefabTransform
impl Debug for PrefabTransform
Source§impl Default for PrefabTransform
impl Default for PrefabTransform
Source§fn default() -> PrefabTransform
fn default() -> PrefabTransform
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl Freeze for PrefabTransform
impl RefUnwindSafe for PrefabTransform
impl Send for PrefabTransform
impl Sync for PrefabTransform
impl Unpin for PrefabTransform
impl UnwindSafe for PrefabTransform
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more