}
/// Creates a grid of wall edges for fast lookup
- fn build_wall_grid(walls: &mut Vec<Rc<WallRegion>>, lvlsize: &Dimension<usize>) -> Grid<Vec<Rc<WallEdge>>> {
+ fn build_wall_grid(walls: &mut Vec<WallRegion>, lvlsize: &Dimension<usize>) -> Grid<Vec<Rc<WallEdge>>> {
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);
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)
}
}
}
-pub enum IntersectResult {
- Intersection(Wall, Point<f64>),
+pub enum IntersectResult<'a> {
+ Intersection(Wall<'a>, Point<f64>),
None
}
}
impl WallRegion {
- pub fn new(points: Vec<Point<f64>>) -> Rc<Self> {
+ pub fn new(points: Vec<Point<f64>>) -> 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<WallEdge> {
- let index = (index + 1) % self.edges.len();
- Rc::clone(&self.edges[index])
- }
-
- #[allow(dead_code)]
- fn previous(&self, index: usize) -> Rc<WallEdge> {
- let index = (index + self.edges.len() + 1) % self.edges.len();
- Rc::clone(&self.edges[index])
- }
+ // #[allow(dead_code)]
+ // fn next(&self, index: EdgeIndex) -> Rc<WallEdge> {
+ // let index = (index + 1) % self.edges.len();
+ // Rc::clone(&self.edges[index])
+ // }
+
+ // #[allow(dead_code)]
+ // fn previous(&self, index: EdgeIndex) -> Rc<WallEdge> {
+ // 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<f64>,
pub p2: Point<f64>,
}
////////// WALL ////////////////////////////////////////////////////////////////
-pub struct Wall {
-// region: Rc<WallRegion>,
- edge: Rc<WallEdge>,
+/// kommer det här att fungera ifall nåt objekt ska spara en referens till Wall?
+/// kanske istället ska lägga Vec<WallRegion> i en Rc<Walls> 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,
+ }
+ }
}