summaryrefslogtreecommitdiff
path: root/src/map.rs
blob: 57b39f58946f12cd1498599fe3717a22755dfd71 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use ggez::filesystem::File;
use ggez::graphics::{spritebatch::SpriteBatch, DrawParam};
use ggez::nalgebra::{Point2, Vector2};
use xml::reader::XmlEvent::Characters;

use crate::constants;
use crate::tileset::Tileset;
use crate::xmlelements::XMLElements;

pub struct Map {
    pub width: usize,
    pub height: usize,
    pub layers: Vec<Layer>,
}

impl Map {
    pub fn new(file: File) -> Map {
        let elements = XMLElements::new(file);

        let width = elements.get_element_attribute("map", "width").unwrap();
        let height = elements.get_element_attribute("map", "height").unwrap();
        let layers = elements
            .events
            .iter()
            .filter_map(|e| {
                if let Characters(text) = e {
                    Some(Layer::new(text.to_string()))
                } else {
                    None
                }
            })
            .collect();

        Map {
            layers,
            width,
            height,
        }
    }

    pub fn draw(&self, spritebatch: &mut SpriteBatch, tileset: &Tileset) {
        for layer in self.layers.iter() {
            for x in 0..self.width {
                for y in 0..self.height {
                    let draw_param = DrawParam::default()
                        .src(tileset.tiles[layer.data[x + (y * self.height)]])
                        .dest(Point2::new(
                            constants::TILE_WIDTH * constants::TILE_SCALE * x as f32,
                            constants::TILE_HEIGHT * constants::TILE_SCALE * y as f32,
                        ))
                        .scale(Vector2::new(constants::TILE_SCALE, constants::TILE_SCALE));

                    spritebatch.add(draw_param);
                }
            }
        }
    }

    pub fn get_dimensions(&self) -> (f32, f32) {
        (
            (constants::TILE_WIDTH * constants::TILE_SCALE) * self.width as f32,
            (constants::TILE_HEIGHT * constants::TILE_SCALE) * self.height as f32,
        )
    }
}

pub struct Layer {
    pub data: Vec<usize>,
}

impl Layer {
    pub fn new(text: String) -> Layer {
        Layer {
            data: text
                .replace("\n", "")
                .split(',')
                .map(|s| s.parse().unwrap())
                .collect(),
        }
    }
}