diff options
Diffstat (limited to 'src/map.rs')
-rw-r--r-- | src/map.rs | 47 |
1 files changed, 24 insertions, 23 deletions
@@ -1,18 +1,18 @@ use ggez::filesystem::File; use ggez::graphics::spritebatch::SpriteBatch; use ggez::nalgebra::Point2; +use std::collections::HashMap; use xml::reader::XmlEvent::Characters; use crate::constants; use crate::entity::Operable; use crate::layer::Layer; -use crate::tileset::Tileset; +use crate::tileset::{Tile, Tileset}; use crate::xmlelements::XMLElements; #[derive(Clone)] pub struct Map { - width: usize, - height: usize, + dimensions: (usize, usize), layers: Vec<Layer>, spawns: Vec<(String, Point2<f32>)>, } @@ -35,23 +35,25 @@ impl Map { pub fn new(file: File, tileset: &Tileset) -> Map { let elements = XMLElements::new(file); - let width = elements - .get_element_attribute("map", "width") - .unwrap() - .parse() - .unwrap(); - let height = elements - .get_element_attribute("map", "height") - .unwrap() - .parse() - .unwrap(); + let dimensions = ( + elements + .get_element_attribute("map", "width") + .unwrap() + .parse() + .unwrap(), + elements + .get_element_attribute("map", "height") + .unwrap() + .parse() + .unwrap(), + ); let layers: Vec<Layer> = elements .events .iter() .filter_map(|e| { if let Characters(text) = e { - Some(Layer::new(text, tileset, width, height)) + Some(Layer::new(text, tileset, dimensions)) } else { None } @@ -62,23 +64,22 @@ impl Map { Map { layers, - width, - height, + dimensions, spawns, } } fn get_spawn_points( layers: &[Layer], - spawn_tiles: Vec<(String, usize)>, + spawn_tiles: HashMap<usize, Tile>, ) -> Vec<(String, Point2<f32>)> { let mut spawn_points = Vec::new(); for layer in layers.iter() { - for tile in layer.tiles.iter() { - for spawn_tile in spawn_tiles.iter() { - if spawn_tile.1 == tile.id { - spawn_points.push((spawn_tile.0.clone(), tile.destination)); + for cell in layer.cells.iter() { + for (id, tile) in spawn_tiles.iter() { + if id == &cell.id { + spawn_points.push((tile.property.spawn.clone().unwrap(), cell.destination)); } } } @@ -93,8 +94,8 @@ impl Map { 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, + (constants::TILE_WIDTH * constants::TILE_SCALE) * self.dimensions.0 as f32, + (constants::TILE_HEIGHT * constants::TILE_SCALE) * self.dimensions.1 as f32, ) } } |