From: Tomas Wenström Date: Sun, 7 Feb 2021 16:48:55 +0000 (+0100) Subject: Renamed Grid.cell_size -> scale X-Git-Url: http://www.dolda2000.com/gitweb/?p=kaka%2Frust-sdl-test.git;a=commitdiff_plain;h=d59c7f04922541543adb533ab07a26a781341777 Renamed Grid.cell_size -> scale --- diff --git a/src/common/geometry.rs b/src/common/geometry.rs index 5a115fd..e46f23e 100644 --- a/src/common/geometry.rs +++ b/src/common/geometry.rs @@ -76,6 +76,8 @@ point_op!(+, Add(add), AddAssign(add_assign), rhs = (T, T) => rhs.0, rhs.1); point_op!(-, Sub(sub), SubAssign(sub_assign), rhs = (T, T) => rhs.0, rhs.1); point_op!(*, Mul(mul), MulAssign(mul_assign), rhs = (T, T) => rhs.0, rhs.1); point_op!(/, Div(div), DivAssign(div_assign), rhs = (T, T) => rhs.0, rhs.1); +point_op!(*, Mul(mul), MulAssign(mul_assign), rhs = Dimension => rhs.width, rhs.height); +point_op!(/, Div(div), DivAssign(div_assign), rhs = Dimension => rhs.width, rhs.height); ////////// multiply point with scalar ////////////////////////////////////////// impl + Copy> Mul for Point { diff --git a/src/core/game.rs b/src/core/game.rs index e7eac23..b0d9968 100644 --- a/src/core/game.rs +++ b/src/core/game.rs @@ -173,8 +173,8 @@ impl Object for Character { fn update(&mut self, objects: &mut Objects, lvl: &Level, dt: Duration) -> ObjectState { let ctrl = self.ctrl.borrow(); - let x = (self.pos.x / lvl.grid.cell_size.width as f64).min(lvl.grid.size.width as f64 - 1.0).max(0.0) as usize; - let y = (self.pos.y / lvl.grid.cell_size.height as f64).min(lvl.grid.size.height as f64 - 1.0).max(0.0) as usize; + let x = (self.pos.x / lvl.grid.scale.width as f64).min(lvl.grid.size.width as f64 - 1.0).max(0.0) as usize; + let y = (self.pos.y / lvl.grid.scale.height as f64).min(lvl.grid.size.height as f64 - 1.0).max(0.0) as usize; self.vel += lvl.gravity; if lvl.grid.cells[x][y] { if self.vel.y > 0.0 && !(ctrl.mov.down() && ctrl.jump.is_pressed) { diff --git a/src/core/level/lvlgen.rs b/src/core/level/lvlgen.rs index 109337a..daf27e8 100644 --- a/src/core/level/lvlgen.rs +++ b/src/core/level/lvlgen.rs @@ -2,7 +2,7 @@ use common::{Point, Dimension}; use noise::{NoiseFn, OpenSimplex, Seedable}; use rand::Rng; use super::{Grid, Level, WallRegion}; -use {point, time_scope}; +use {point, dimen, time_scope}; ////////// LEVEL GENERATOR ///////////////////////////////////////////////////// @@ -26,13 +26,13 @@ impl LevelGenerator { dbg!(self); time_scope!("level generation"); - let cell_size = 20; - let (width, height) = (2560 / cell_size, 1440 / cell_size); + let scale = 20.0; + let size = dimen!((2560.0 / scale) as usize, (1440.0 / scale) as usize); let mut grid = Grid { - cell_size: (cell_size, cell_size).into(), - size: (width, height).into(), - cells: vec!(vec!(true; height); width), + scale: (scale, scale).into(), + cells: vec!(vec!(true; size.height); size.width), + size, }; // start with some noise @@ -147,7 +147,7 @@ impl LevelGenerator { } } Grid { - cell_size: (grid.cell_size.width / 2, grid.cell_size.height / 2).into(), + scale: (grid.scale.width / 2.0, grid.scale.height / 2.0).into(), size: (width, height).into(), cells } @@ -224,7 +224,7 @@ impl LevelGenerator { let mut walls = vec!(); for r in self.find_regions(&grid) { if r.value { - let outline = r.outline(&grid.cell_size); + let outline = r.outline(&grid.scale); let mut floats = outline.iter().map(|p| point!(p.x as f64, p.y as f64)).collect(); self.smooth_wall(&mut floats, self.wall_smooth_radius as isize); let wall = WallRegion::new(floats); @@ -266,7 +266,7 @@ impl Region { (min.0, min.1, 1 + max.0 - min.0, 1 + max.1 - min.1) } - pub fn outline(&self, scale: &Dimension) -> Vec> { + pub fn outline(&self, scale: &Dimension) -> Vec> { let rect = self.enclosing_rect(); let (ox, oy, w, h) = rect; let grid = self.grid(&rect); diff --git a/src/core/level/mod.rs b/src/core/level/mod.rs index ac13e4e..5df8ba6 100644 --- a/src/core/level/mod.rs +++ b/src/core/level/mod.rs @@ -14,15 +14,15 @@ pub use self::lvlgen::LevelGenerator; pub struct Level { pub gravity: Point, pub grid: Grid, - walls: Vec>, + walls: Vec, wall_grid: Grid>>, } impl Level { - pub fn new(gravity: Point, grid: Grid, mut walls: Vec>) -> Self { + pub fn new(gravity: Point, grid: Grid, mut walls: Vec) -> Self { let size = (2560, 1440); // TODO: get actual size from walls or something let wall_grid = Level::build_wall_grid(&mut walls, &size.into()); - dbg!(&wall_grid.cell_size); + dbg!(&wall_grid.scale); Level { gravity, grid, @@ -35,11 +35,11 @@ impl Level { 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); + //let cs = point!(scale.width as f64, scale.height as f64); let mut grid = Grid { cells: vec!(vec!(vec!(); size.height); size.width), size, - cell_size: dimen!(cs.x, cs.y), + scale: dimen!(cs.x as f64, cs.y as f64), }; for wall in walls { @@ -56,7 +56,7 @@ impl Level { pub fn render(&mut self, renderer: &mut Renderer, _sprites: &SpriteManager) { // original grid renderer.canvas().set_draw_color((64, 64, 64)); - let size = &self.grid.cell_size; + let size = &self.grid.scale; for x in 0..self.grid.size.width { for y in 0..self.grid.size.height { if self.grid.cells[x][y] { @@ -71,7 +71,7 @@ impl Level { // wall grid renderer.canvas().set_draw_color((0, 32, 0)); - let size = &self.wall_grid.cell_size; + let size = &self.wall_grid.scale; for x in 0..self.wall_grid.size.width { for y in 0..self.wall_grid.size.height { if !self.wall_grid.cells[x][y].is_empty() { @@ -125,7 +125,7 @@ pub enum IntersectResult<'a> { #[derive(Debug, Default)] pub struct Grid { pub size: Dimension, - pub cell_size: Dimension, + pub scale: Dimension, pub cells: Vec>, } @@ -154,8 +154,7 @@ impl Grid { /// Returns a list of grid coordinates that a line in world coordinates passes through. pub fn grid_coordinates_on_line(&self, p1: Point, p2: Point) -> Vec> { - let scale = (self.cell_size.width as f64, self.cell_size.height as f64); - supercover_line(p1 / scale, p2 / scale) + supercover_line(p1 / self.scale, p2 / self.scale) .iter() .map(|c| self.to_grid_coordinate(*c)) .flatten() diff --git a/src/teststate.rs b/src/teststate.rs index 0a66d82..5ea97bb 100644 --- a/src/teststate.rs +++ b/src/teststate.rs @@ -73,12 +73,12 @@ impl AppState for TestState { let grid = Grid { size: dimen!(10, 10), - cell_size: dimen!(30, 30), + scale: dimen!(30.0, 30.0), cells: vec!(vec!(false; 10); 10), }; let offset = point!(200, 200); - let size = grid.cell_size; + let size = grid.scale; for x in 0..grid.size.width { for y in 0..grid.size.height { let col = (32 + 32 * ((x + y) % 2)) as u8;