From: Tomas Wenström Date: Sun, 7 Feb 2021 16:29:12 +0000 (+0100) Subject: Refactor walls X-Git-Url: http://git.dolda2000.com/gitweb/?a=commitdiff_plain;h=d01df1fc6e3bfafd385b2533e6155ddd8714fcfb;p=kaka%2Frust-sdl-test.git Refactor walls --- diff --git a/src/core/level/lvlgen.rs b/src/core/level/lvlgen.rs index c2a02b5..109337a 100644 --- a/src/core/level/lvlgen.rs +++ b/src/core/level/lvlgen.rs @@ -1,5 +1,4 @@ use common::{Point, Dimension}; -use std::rc::Rc; use noise::{NoiseFn, OpenSimplex, Seedable}; use rand::Rng; use super::{Grid, Level, WallRegion}; @@ -221,7 +220,7 @@ impl LevelGenerator { } } - fn find_walls(&self, grid: &Grid) -> Vec> { + fn find_walls(&self, grid: &Grid) -> Vec { let mut walls = vec!(); for r in self.find_regions(&grid) { if r.value { diff --git a/src/core/level/mod.rs b/src/core/level/mod.rs index 488cae1..ac13e4e 100644 --- a/src/core/level/mod.rs +++ b/src/core/level/mod.rs @@ -32,7 +32,7 @@ impl Level { } /// Creates a grid of wall edges for fast lookup - fn build_wall_grid(walls: &mut Vec>, lvlsize: &Dimension) -> Grid>> { + fn build_wall_grid(walls: &mut Vec, lvlsize: &Dimension) -> Grid>> { let size = dimen!(lvlsize.width / 20, lvlsize.height / 20); // TODO: make sure all walls fit within the grid bounds let cs = point!(lvlsize.width / size.width, lvlsize.height / size.height); //let cs = point!(cell_size.width as f64, cell_size.height as f64); @@ -104,7 +104,7 @@ impl Level { if let Intersection::Point(p) = Intersection::lines(p1, p2, w.p1, w.p2) { let wall = Wall { region: &self.walls[w.region], - edge: Rc::clone(&w), + edge: w, }; return IntersectResult::Intersection(wall, p) } @@ -115,8 +115,8 @@ impl Level { } } -pub enum IntersectResult { - Intersection(Wall, Point), +pub enum IntersectResult<'a> { + Intersection(Wall<'a>, Point), None } @@ -171,46 +171,74 @@ pub struct WallRegion { } impl WallRegion { - pub fn new(points: Vec>) -> Rc { + pub fn new(points: Vec>) -> Self { + let index: RegionIndex = 0; // use as param let mut edges = Vec::with_capacity(points.len()); for i in 0..points.len() { let edge = Rc::new(WallEdge { - index: i, + region: index, + id: i, p1: points[i], p2: points[(i + 1) % points.len()], }); edges.push(edge); } - Rc::new(WallRegion { edges }) + WallRegion { edges } } - #[allow(dead_code)] - fn next(&self, index: usize) -> Rc { - let index = (index + 1) % self.edges.len(); - Rc::clone(&self.edges[index]) - } - - #[allow(dead_code)] - fn previous(&self, index: usize) -> Rc { - let index = (index + self.edges.len() + 1) % self.edges.len(); - Rc::clone(&self.edges[index]) - } + // #[allow(dead_code)] + // fn next(&self, index: EdgeIndex) -> Rc { + // let index = (index + 1) % self.edges.len(); + // Rc::clone(&self.edges[index]) + // } + + // #[allow(dead_code)] + // fn previous(&self, index: EdgeIndex) -> Rc { + // let index = (index + self.edges.len() + 1) % self.edges.len(); + // Rc::clone(&self.edges[index]) + // } } ////////// WALL EDGE /////////////////////////////////////////////////////////// +type RegionIndex = usize; +type EdgeIndex = usize; + #[derive(Debug, Default)] struct WallEdge { - index: usize, + region: RegionIndex, + id: EdgeIndex, pub p1: Point, pub p2: Point, } ////////// WALL //////////////////////////////////////////////////////////////// -pub struct Wall { -// region: Rc, - edge: Rc, +/// kommer det här att fungera ifall nÃ¥t objekt ska spara en referens till Wall? +/// kanske istället ska lägga Vec i en Rc och skicka med en klon av den, samt id:n till regionen och väggen? +pub struct Wall<'a> { + region: &'a WallRegion, + edge: &'a WallEdge, +} + +impl<'a> Wall<'a> { + pub fn next(&self) -> Wall<'a> { + let next = (self.edge.id + 1) % self.region.edges.len(); + let edge = &self.region.edges[next]; + Wall { + region: self.region, + edge, + } + } + + pub fn previous(&self) -> Wall<'a> { + let prev = (self.edge.id + self.region.edges.len() - 1) % self.region.edges.len(); + let edge = &self.region.edges[prev]; + Wall { + region: self.region, + edge, + } + } }