use stdweb::web::CanvasRenderingContext2d; use crate::{Level, Tile}; use crate::Direction; const SCALE_X: f64 = 8.0; const SCALE_Y: f64 = 4.0; pub fn render_level(canvas: &mut CanvasRenderingContext2d, level: &Level) { let region = &level.region; let x_offset = 20.0; let y_offset = 20.0; for y in 0..region.height() { for x in 0..region.width() { let old_width = canvas.get_line_width(); canvas.set_line_width(2.0); canvas.set_fill_style_color("lightgrey"); let x_f = x as f64; let y_f = y as f64; let x_l = x_offset + (x_f * SCALE_X); let x_r = x_offset + (x_f + 1.0) * SCALE_X; let y_t = y_offset + (y_f * SCALE_Y); let y_b = y_offset + (y_f + 1.0) * SCALE_Y; let tile = region.get(x, y).unwrap(); if let Some(tile) = tile { canvas.fill_rect(x_l, y_t, x_r - x_l, y_b - y_t); if tile.connections & Direction::UP == 0 { canvas.begin_path(); canvas.move_to(x_l, y_t); canvas.line_to(x_r, y_t); canvas.stroke(); } if tile.connections & Direction::RIGHT == 0 { canvas.begin_path(); canvas.move_to(x_r, y_t); canvas.line_to(x_r, y_b); canvas.stroke(); } if tile.connections & Direction::DOWN == 0 { canvas.begin_path(); canvas.move_to(x_l, y_b); canvas.line_to(x_r, y_b); canvas.stroke(); } if tile.connections & Direction::LEFT == 0 { canvas.begin_path(); canvas.move_to(x_l, y_t); canvas.line_to(x_l, y_b); canvas.stroke(); } } } } }