X-Git-Url: http://www.dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fcore%2Flevel%2Fmod.rs;h=2a9073918a1eea528d14222923e9ec2c27288aeb;hb=60058b918569190f437fe996dfc79daf5a431b91;hp=3945776f998f6df729f3ea16e2f8c61671fa350a;hpb=1f42d724d84ed1c014ff40ccc91058472391be0c;p=kaka%2Frust-sdl-test.git diff --git a/src/core/level/mod.rs b/src/core/level/mod.rs index 3945776..2a90739 100644 --- a/src/core/level/mod.rs +++ b/src/core/level/mod.rs @@ -1,4 +1,4 @@ -use common::{Point, Dimension}; +use common::{Point, Dimension, Intersection}; use core::render::Renderer; use sprites::SpriteManager; use std::rc::Rc; @@ -96,6 +96,26 @@ impl Level { } } } + + pub fn intersect_walls(&self, p1: Point, p2: Point) -> IntersectResult { + let c = point!(p2.x as isize / self.wall_grid.cell_size.width as isize, p2.y as isize / self.wall_grid.cell_size.height as isize); + if let Some(walls) = self.wall_grid.at(c) { + for w in walls { + if let Intersection::Point(p) = Intersection::lines(p1, p2, w.p1, w.p2) { + let wall = Wall { + edge: Rc::clone(&w), + }; + return IntersectResult::Intersection(wall, p) + } + } + } + IntersectResult::None + } +} + +pub enum IntersectResult { + Intersection(Wall, Point), + None } ////////// GRID //////////////////////////////////////////////////////////////// @@ -107,6 +127,19 @@ pub struct Grid { pub cells: Vec>, } +impl Grid { + pub fn at(&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 + } + } +} + ////////// WALL REGION ///////////////////////////////////////////////////////// #[derive(Debug)] @@ -116,7 +149,7 @@ pub struct WallRegion { impl WallRegion { pub fn new(points: Vec>) -> Rc { - let mut edges = vec!(); + let mut edges = Vec::with_capacity(points.len()); for i in 0..points.len() { let edge = Rc::new(WallEdge { @@ -151,3 +184,10 @@ struct WallEdge { pub p1: Point, pub p2: Point, } + +////////// WALL //////////////////////////////////////////////////////////////// + +pub struct Wall { +// region: Rc, + edge: Rc, +}