pub struct Level {
pub gravity: Point<f64>,
pub grid: Grid<bool>,
- walls: Vec<WallRegion>,
+ walls: Vec<Rc<WallRegion>>,
wall_grid: Grid<Vec<Rc<WallEdge>>>,
}
Level {
gravity,
grid,
- walls,
+ walls: walls.into_iter().map(|i| Rc::new(i)).collect(),
wall_grid,
}
}
for w in &self.wall_grid.cells[c.x][c.y] {
if let Intersection::Point(p) = Intersection::lines(p1, p2, w.p1, w.p2) {
let wall = Wall {
- region: &self.walls[w.region],
- edge: w,
+ region: Rc::clone(&self.walls[w.region]),
+ edge: Rc::clone(w),
};
return IntersectResult::Intersection(wall, p)
}
}
}
-pub enum IntersectResult<'a> {
- Intersection(Wall<'a>, Point<f64>),
+pub enum IntersectResult {
+ Intersection(Wall, Point<f64>),
None
}
}
impl<T> Grid<T> {
- pub fn at<C>(&self, c: C) -> Option<&T>
- where C: Into<(isize, isize)>
- {
- let c = c.into();
- if c.0 >= 0 && c.0 < self.size.width as isize && c.1 >= 0 && c.1 < self.size.height as isize {
- Some(&self.cells[c.0 as usize][c.1 as usize])
- } else {
- None
- }
- }
+ // pub fn at<C>(&self, c: C) -> Option<&T>
+ // where C: Into<(isize, isize)>
+ // {
+ // let c = c.into();
+ // if c.0 >= 0 && c.0 < self.size.width as isize && c.1 >= 0 && c.1 < self.size.height as isize {
+ // Some(&self.cells[c.0 as usize][c.1 as usize])
+ // } else {
+ // None
+ // }
+ // }
pub fn to_grid_coordinate<C>(&self, c: C) -> Option<Point<usize>>
where C: Into<(isize, isize)>
////////// WALL REGION /////////////////////////////////////////////////////////
-#[derive(Debug)]
+#[derive(Debug, Default)]
pub struct WallRegion {
edges: Vec<Rc<WallEdge>>,
}
WallRegion { edges }
}
- // #[allow(dead_code)]
- // fn next(&self, index: EdgeIndex) -> Rc<WallEdge> {
- // let index = (index + 1) % self.edges.len();
- // Rc::clone(&self.edges[index])
- // }
+ 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])
- // }
+ fn previous(&self, index: EdgeIndex) -> Rc<WallEdge> {
+ let index = (index + self.edges.len() + 1) % self.edges.len();
+ Rc::clone(&self.edges[index])
+ }
}
////////// WALL EDGE ///////////////////////////////////////////////////////////
////////// WALL ////////////////////////////////////////////////////////////////
-/// 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,
+pub struct Wall {
+ region: Rc<WallRegion>,
+ edge: Rc<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];
+impl Wall {
+ #[allow(dead_code)]
+ pub fn next(self) -> Wall {
Wall {
+ edge: self.region.next(self.edge.id),
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];
+ #[allow(dead_code)]
+ pub fn previous(self) -> Wall {
Wall {
+ edge: self.region.previous(self.edge.id),
region: self.region,
- edge,
}
}