Skip to main content

PrefabTransform

Struct PrefabTransform 

Source
pub struct PrefabTransform {
    pub rotation: u8,
    pub mirror_h: bool,
    pub mirror_v: bool,
}

Fields§

§rotation: u8§mirror_h: bool§mirror_v: bool

Implementations§

Source§

impl PrefabTransform

Source

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        placement_mode: terrain_forge::algorithms::PrefabPlacementMode::Overwrite,
107        tags: None,
108    };
109
110    let placer = PrefabPlacer::new(config, library.clone());
111    let mut grid = Grid::new(30, 25);
112    placer.generate(&mut grid, 54321);
113
114    let floor_count = grid.count(|t| t.is_floor());
115    println!(
116        "   Generated {}x{} grid with {} floor tiles",
117        grid.width(),
118        grid.height(),
119        floor_count
120    );
121
122    print_grid(&grid);
123
124    // Step 6: JSON serialization example
125    println!("\n6. JSON Serialization:");
126    match library.save_to_json("prefab_library.json") {
127        Ok(()) => {
128            println!("   ✅ Saved library to prefab_library.json");
129
130            // Try to load it back
131            match PrefabLibrary::load_from_json("prefab_library.json") {
132                Ok(loaded_library) => {
133                    println!("   ✅ Successfully loaded library back");
134                    println!("   Loaded {} prefabs", loaded_library.get_prefabs().len());
135                }
136                Err(e) => println!("   ❌ Failed to load: {}", e),
137            }
138        }
139        Err(e) => println!("   ❌ Failed to save: {}", e),
140    }
141
142    // Step 7: Performance comparison
143    println!("\n7. Performance Comparison:");
144
145    // Simple generation
146    let simple_config = PrefabConfig {
147        max_prefabs: 10,
148        min_spacing: 2,
149        allow_rotation: false,
150        allow_mirroring: false,
151        weighted_selection: false,
152        placement_mode: terrain_forge::algorithms::PrefabPlacementMode::Overwrite,
153        tags: None,
154    };
155
156    let start = std::time::Instant::now();
157    let simple_placer = PrefabPlacer::new(simple_config, library.clone());
158    let mut simple_grid = Grid::new(40, 30);
159    simple_placer.generate(&mut simple_grid, 98765);
160    let simple_time = start.elapsed();
161
162    // Advanced generation
163    let advanced_config = PrefabConfig {
164        max_prefabs: 10,
165        min_spacing: 2,
166        allow_rotation: true,
167        allow_mirroring: true,
168        weighted_selection: true,
169        placement_mode: terrain_forge::algorithms::PrefabPlacementMode::Overwrite,
170        tags: None,
171    };
172
173    let start = std::time::Instant::now();
174    let advanced_placer = PrefabPlacer::new(advanced_config, library);
175    let mut advanced_grid = Grid::new(40, 30);
176    advanced_placer.generate(&mut advanced_grid, 98765);
177    let advanced_time = start.elapsed();
178
179    println!("   Simple generation: {:?}", simple_time);
180    println!("   Advanced generation: {:?}", advanced_time);
181    println!(
182        "   Overhead: {:.1}x",
183        advanced_time.as_nanos() as f32 / simple_time.as_nanos() as f32
184    );
185
186    println!("\n✅ Advanced prefab system demo complete!");
187    println!("   - JSON serialization for persistent libraries");
188    println!("   - Weighted selection for balanced generation");
189    println!("   - Transformations for variety and reuse");
190    println!("   - Tag-based organization for targeted selection");
191}
Source

pub fn random( rng: &mut Rng, allow_rotation: bool, allow_mirroring: bool, ) -> Self

Trait Implementations§

Source§

impl Clone for PrefabTransform

Source§

fn clone(&self) -> PrefabTransform

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PrefabTransform

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for PrefabTransform

Source§

fn default() -> PrefabTransform

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V