rust/landgen/src/outline_template_based/template_based.rs
author unC0Rr
Mon, 13 Feb 2023 11:00:12 +0100
branchtransitional_engine
changeset 15921 5f00829c55ec
parent 15916 rust/landgen/src/template_based.rs@e82de0410da5
child 15922 da6b67f13c12
permissions -rw-r--r--
Refactor landgen package structure

use super::{
    outline::OutlinePoints, outline_template::OutlineTemplate};
use crate::{LandGenerationParameters,
    LandGenerator,
};
use land2d::Land2D;

pub struct TemplatedLandGenerator {
    outline_template: OutlineTemplate,
}

impl TemplatedLandGenerator {
    pub fn new(outline_template: OutlineTemplate) -> Self {
        Self { outline_template }
    }
}

impl LandGenerator for TemplatedLandGenerator {
    fn generate_land<T: Copy + PartialEq + Default, I: Iterator<Item = u32>>(
        &self,
        parameters: &LandGenerationParameters<T>,
        random_numbers: &mut I,
    ) -> Land2D<T> {
        let mut land = Land2D::new(&self.outline_template.size, parameters.basic);

        let mut points = OutlinePoints::from_outline_template(
            &self.outline_template,
            land.play_box(),
            land.size().size(),
            random_numbers,
        );

        // mirror
        if self.outline_template.can_mirror {
            if let Some(b) = random_numbers.next() {
                if b & 1 != 0 {
                    points.mirror();
                }
            }
        }

        // flip
        if self.outline_template.can_flip {
            if let Some(b) = random_numbers.next() {
                if b & 1 != 0 {
                    points.flip();
                }
            }
        }

        if !parameters.skip_distort {
            points.distort(parameters.distance_divisor, random_numbers);
        }

        if !parameters.skip_bezier {
            points.bezierize(5);
        }

        points.draw(&mut land, parameters.zero);

        for p in &points.fill_points {
            land.fill(*p, parameters.zero, parameters.zero)
        }

        points.draw(&mut land, parameters.basic);

        land
    }
}