rust/landgen/src/template_based.rs
author unC0Rr
Fri, 02 Nov 2018 14:29:24 +0100
changeset 14099 bf40b5f938b0
parent 14090 abb42ba345b6
child 14116 b2feb190e4bc
permissions -rw-r--r--
- Add methods to work with Rect as box - Refactor, tests, fixes

use integral_geometry::{Point, Size};
use land2d::Land2D;
use LandGenerationParameters;
use LandGenerator;

use outline::OutlinePoints;
use outline_template::OutlineTemplate;


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, 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(), random_numbers);

        // mirror
        if self.outline_template.can_mirror {
            if let Some(b) = random_numbers.next() {
                if b & 1 != 0 {
                    points
                        .iter_mut()
                        .for_each(|p| p.x = land.width() as i32 - 1 - p.x);
                }
            }
        }

        // flip
        if self.outline_template.can_flip {
            if let Some(b) = random_numbers.next() {
                if b & 1 != 0 {
                    points
                        .iter_mut()
                        .for_each(|p| p.y = land.height() as i32 - 1 - p.y);
                }
            }
        }

        points.distort(random_numbers);

        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
    }
}