summaryrefslogtreecommitdiff
path: root/src/canvas.rs
blob: ead8636dd525877f59b68277f4c261ee5a5cf0bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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();
                } 
            }
        }
    }
}