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<T> => rhs.width, rhs.height);
+point_op!(/, Div(div), DivAssign(div_assign), rhs = Dimension<T> => rhs.width, rhs.height);
////////// multiply point with scalar //////////////////////////////////////////
impl<T: Mul<Output = T> + Copy> Mul<T> for Point<T> {
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) {
use noise::{NoiseFn, OpenSimplex, Seedable};
use rand::Rng;
use super::{Grid, Level, WallRegion};
-use {point, time_scope};
+use {point, dimen, time_scope};
////////// LEVEL GENERATOR /////////////////////////////////////////////////////
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
}
}
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
}
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);
(min.0, min.1, 1 + max.0 - min.0, 1 + max.1 - min.1)
}
- pub fn outline(&self, scale: &Dimension<usize>) -> Vec<Point<isize>> {
+ pub fn outline(&self, scale: &Dimension<f64>) -> Vec<Point<isize>> {
let rect = self.enclosing_rect();
let (ox, oy, w, h) = rect;
let grid = self.grid(&rect);
pub struct Level {
pub gravity: Point<f64>,
pub grid: Grid<bool>,
- walls: Vec<Rc<WallRegion>>,
+ walls: Vec<WallRegion>,
wall_grid: Grid<Vec<Rc<WallEdge>>>,
}
impl Level {
- pub fn new(gravity: Point<f64>, grid: Grid<bool>, mut walls: Vec<Rc<WallRegion>>) -> Self {
+ pub fn new(gravity: Point<f64>, grid: Grid<bool>, mut walls: Vec<WallRegion>) -> 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,
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);
+ //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 {
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] {
// 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() {
#[derive(Debug, Default)]
pub struct Grid<T> {
pub size: Dimension<usize>,
- pub cell_size: Dimension<usize>,
+ pub scale: Dimension<f64>,
pub cells: Vec<Vec<T>>,
}
/// Returns a list of grid coordinates that a line in world coordinates passes through.
pub fn grid_coordinates_on_line(&self, p1: Point<f64>, p2: Point<f64>) -> Vec<Point<usize>> {
- 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()
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;